mousePressEvent 未正确绑定?

发布于 2024-10-02 06:17:56 字数 1315 浏览 4 评论 0原文

为了熟悉 Qt 的图形视图,我在 Qt 中实现了一个简单的棋盘。目前还没有棋子。

我使用矩形来表示每个图块,因此我创建了 BoardTile 类(继承自 QGraphicsRectItem),以便我可以定义 mousePressEvent

这是我与鼠标单击关联的一些测试代码:

void BoardTile::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    QMessageBox mesg;
    std::stringstream mesgText;
    mesgText << "Clicked tile (" << this->row_id << ", " << this->col_id << ").";
    mesg.setText(QString::fromStdString(mesgText.str()));
    mesg.exec();
}

当我单击第二行的第一个图块时,我收到以下消息:

点击图块 (1, 0)

然后,当我单击任何其他图块时,我会收到完全相同的消息。消息的内容取决于我首先单击的图块。这是为什么呢?我是否错误地绑定了 mousePressEvent

完整代码 http://www.box.net/shared/4m6nrvuxa4(更新1)< /em>

更新 2:我注意到,如果我将 event->ignore(); 放在 mesg.exec() 之后,它就可以工作美好的。我知道这不是一个解决方案(因为它可能会导致各种奇怪的行为),但我确实想知道为什么这有效。这是否揭示了我在实施过程中可能犯的任何重要错误?

更新 3:有人告诉我应该尝试将 mousePressEvent 更改为 mouseReleaseEvent。奇怪的是,这有效。据我所知,这两个事件之间的唯一区别是,第一个事件在您按下鼠标按钮时触发,而后者在您释放该按钮时触发。那么为什么 mouseReleaseEvent 会触发所需的行为,而 mousePressEvent 却不会呢?

To familiarize myself with Qt's graphics view, I'm implementing a simple chess board in Qt. There are no chess pieces, for now.

I use rectangles to represent every tile, so I made the BoardTile class (which inherits from QGraphicsRectItem) so that I can define a mousePressEvent.

Here's a little test code that I associated with the mouse click:

void BoardTile::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    QMessageBox mesg;
    std::stringstream mesgText;
    mesgText << "Clicked tile (" << this->row_id << ", " << this->col_id << ").";
    mesg.setText(QString::fromStdString(mesgText.str()));
    mesg.exec();
}

When I click the first tile on the second row, I get the following message:

Clicked tile (1, 0)

Then, when I click any other tile, I get the exact same message. The contents of the message depends on whichever tile I clicked first. Why is this? Did I bind the mousePressEvent incorrectly?

Full code
http://www.box.net/shared/4m6nrvuxa4 (update 1)

Update 2: I noticed that if I put event->ignore(); after mesg.exec(), it works fine. I know it's not a solution (because it will probably lead to all kinds of weird behavior), but I do wanna know why that does work. Does this reveal anything crucial about any possible mistakes I've made in my implementation?

Update 3: Someone told me I should try changing mousePressEvent to mouseReleaseEvent. Oddly enough, that works. As far as I know, the only difference between the two events is that the first is triggered when you press the mouse button and the latter when you release that button. So why does mouseReleaseEvent trigger the desired behavior and mousePressEvent doesn't?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2024-10-09 06:17:56

当您执行 this->x_id 时,您将获得 RECT 的 x 位置,而不是鼠标单击。您必须执行从事件中获取的 event->::pos() 操作。文档位于此处

When you are doing this->x_id you are getting the x position of the RECT, not your mouse click. You have to do event->::pos() taken from the event. The docs are here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文