- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
Qt 让我质疑我的理智和存在。我不知道为什么在我编写的一个程序中有效的代码在我编写的另一个程序中不起作用。以下代码在两个程序中是相同的。在 P1 中,它仅允许左键单击即可正常工作。在 P2 中,它完全相同,除了左键单击代码做了一些不同的事情。
在 P2 中,我让它检查左键单击条件,如果为真,则执行代码。好吧,当我左键或右键单击时,它不会执行代码。如果我更改条件以检查右键单击并返回如果为真,则左键单击正常,但右键单击不会返回。如果我删除条件,左键和右键单击都会运行代码。
我正在失去理智,因为像这样愚蠢的事情一直在发生,我不知道为什么,尽管我做的每件事都和其他有效的程序(我写的)一样。
编辑:它似乎忽略了 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();
}
}
最佳答案
来自 QMouseEvent::buttons() documentation :
For mouse release events this excludes the button that caused the event.
所以解决方案是使用 QMouseEvent::button() 代替:
void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
clickOff = event->pos();
// do it only if left mouse button is down
if (event->button() == Qt::LeftButton) {
// left click code
}
}
关于c++ - Qt鼠标点击检测一直不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451658/
我有一个在 Android 市场上相当流行的应用程序,它允许数以万计的用户按下一个按钮并向它发出语音命令。然后我就可以做很多不同的事情,比如给他们提供当前的天气预报等等...... 无论如何,我的应用
令人惊讶的是,标题基本上解释了它。我们有一个我们的客户制作的页面,我们正在重新创建该页面。 页面高度会一直增加,直到(我假设是这样)浏览器达到它的极限。我已经尝试过 Firebug 和 W3 验证器,
我是 react-native 的新手,试图创建我自己的组件,但它一直显示一个空屏幕。 这是我的组件代码 class BoxComponent extends Component { cons
我正在为我的 PHP 元素创建一个非常简单的博客,但遇到了一个简单的问题。我无法让我的页眉图像一直 float 。我有一个横幅,左边有一些文字,我有一个 1px 的切片,在可以选择的任何分辨率的宽度上
为什么我可以在另一个 Controller 的 View 中访问一个 Controller 的辅助方法?有没有办法在不破解/修补 Rails 的情况下禁用它? 最佳答案 @George Schreib
我正在使用带有最新 ADT 插件的 Eclipse Kepler SP2。每隔一分钟 Eclipse 就会说“为 Android 4.4.2 加载数据”并阻止我想做的一切。我在不同的文件夹中有几个 E
我是一名优秀的程序员,十分优秀!