QT -鼠标pressevent调用3次时,双击
QT - mousePressEvent called 3 times when double-clicking
我已经创建了一个类GraphicPoint,它继承了QObject和QGraphicsEllipseItem。
. h文件class GraphicPoint : public QObject, public QGraphicsEllipseItem
{
Q_OBJECT
public:
GraphicPoint(qreal x, qreal y, qreal width, qreal height, QWidget *parent = nullptr);
signals:
void clicked();
private:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};
. cpp文件GraphicPoint::GraphicPoint(qreal x, qreal y, qreal width, qreal height, QWidget *parent) :
QObject(parent),
QGraphicsEllipseItem(x, y, width, height, nullptr)
{
}
void GraphicPoint::mousePressEvent(QGraphicsSceneMouseEvent *event){
event->accept();
emit clicked();
}
在主窗口中,我创建了一个视图和一个场景,并将点添加到场景中。快速单击按钮两次调用mousePressEvent 3次,而稍微慢一点的操作只调用2次。下面是主窗口构造函数
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
n = 0;
view = new QGraphicsView(this);
scene = new QGraphicsScene(this);
view->setScene(scene);
testPoint = new GraphicPoint(0, 0, 10, 10, this);
connect(testPoint, &GraphicPoint::clicked, [this](){
n++;
qDebug()<<n;
});
scene->addItem(testPoint);
setCentralWidget(view);
}
快速点击2次后,QDebug输出3行数字1,2,3。
发生这种情况的原因是什么?是否有一些默认的双击事件调用额外的mousePressEvent?
你的问题的原因是无效的QGraphicsItem::mouseDoubleClickEvent,默认情况下调用mousePressEvent()
。为了解决这个问题,只需重载mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event )
并将其保留为空。
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- C++,Qt:仅在双击后编辑qtablewidget项目
- 双击更改 mfc 中列表控件中的行的颜色
- C++MFC编辑框处理双击
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- 双击滑块手柄时如何发出信号
- 有没有办法在循环中的条件至少满足一次时运行语句?
- 在使用 c++ 的 win32 api 中,我想跟踪双击和三次单击等
- 处理WM_POINTER消息时,如何检测双击/点击
- 可执行文件无法在双击执行时创建新文件
- 双击数字时包括符号
- Qt 当列表小部件项被单击两次时崩溃
- wxWidgets / wxStyledTextCtrl - 双击时突出显示所有出现的情况
- 双击Qt选项卡时重命名
- 当我双击并粘贴变量时如何防止重构?
- QT -鼠标pressevent调用3次时,双击
- Qt, QSplitter.处理鼠标在拆分器下时的双击
- 我能区分单次和双击系统托盘图标吗?
- 当用户双击qtablewidget标题时,c++ QT4检测