qt让我怀疑我的理智和存在。我不知道为什么在我编写的一个程序中工作的代码在我编写的另一个程序中不工作。以下代码在两个程序中都是相同的。在p1中,只允许单击左键,它就可以正常工作。在p2中,它是完全相同的,除了左键单击代码是做了一些不同的事情。
在p2中,我让它检查左键单击条件,如果是真的,则执行代码。好吧,当我左键或右键单击时,它不会执行代码。如果我将条件更改为检查右键单击并返回(如果为真),则左键单击正常工作,但右键单击不会返回。如果删除这些条件,左键和右键单击都会运行代码。
我正在失去理智,因为像这样愚蠢的事情一直在发生,我不知道为什么即使我做的每件事都和其他程序一样有效(我写的)。
编辑:它似乎忽略了if签入mouserelease函数,并对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();
}
}