3
Qt让我质疑我的理智和存在。我不知道为什么在我编写的一个程序中运行的代码在我编写的另一个程序中不起作用。以下代码在两个程序中都是相同的。在P1中,只有左键点击才能正常工作。在P2中它是完全一样的,除了左键点击代码是做了不同的事情。Qt鼠标点击检测一直不能工作
在P2中,我检查了左键单击条件并执行代码,如果它是真的。那么,当我离开或右键点击时,它不会执行代码。如果我更改条件以检查右键单击并返回true,则左键单击正常工作,但右键单击不会返回。如果我删除条件,左右点击运行代码。
我迷失了我的想法,因为像这样的愚蠢的东西一直在发生,我不知道为什么即使我和其他工作的程序(我写的)一样。
编辑:它似乎忽略了mouseRelease函数中的if-check并为mousePress和mouseMove正常工作。
P1(此程序工作正是我想要它):
void GLWidget::mousePressEvent(QMouseEvent *event)
{
clickOn = event->pos();
clickOff = event->pos();
// right mouse button
if (event->buttons() & Qt::RightButton){
return;
}
// rest of left-click code here
}
/*************************************/
void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
clickOff = event->pos();
// right mouse button shouldn't do anything
if (event->buttons() & Qt::RightButton)
return;
// rest of left click code here
}
/*************************************/
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
clickOff = event->pos();
// do it only if left mouse button is down
if (event->buttons() & Qt::LeftButton) {
// left click code
updateGL();
} else if(event->buttons() & Qt::RightButton){
// right mouse button code
}
}
P2(结构类似于P1,但工作不正常):
void GLWidget::mousePressEvent(QMouseEvent *event)
{
clickOn = event->pos();
clickOff = event->pos();
// do it only if left mouse button is down
if (event->buttons() & Qt::LeftButton) {
// left click code
}
}
void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
clickOff = event->pos();
// do it only if left mouse button is down
if (event->buttons() & Qt::LeftButton) {
// left click code
}
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
clickOff = event->pos();
clickDiff = clickOff - clickOn;
// do it only if left mouse button is down
if (event->buttons() & Qt::LeftButton) {
// left click code
updateGL();
}
}
啊,那工作!谢谢!我认为P1可能有足够的检查,如果它没有在特定的条件下完成,它就不会执行左键单击代码。 – alex 2010-08-10 22:08:13