Qt 人工鼠标点击无法正常工作
小玩具应用程序可以在这里找到: http://gist.github.com/517445
我正在尝试发送人工鼠标事件到小部件,我使用 QApplication::sendEvent 为此,接下来我检查 ev.isAccepted() ,它返回 False,甚至更糟!我发送事件的小部件不处理它(它是日历,没有选择日期),我怀疑它是否收到它,因为我可以看到 mouseEventPressed 如何在父小部件上启动。
Qt 代码:
#include "calendar.h"
Calendar::Calendar(QWidget *parent) :
QWidget(parent)
{
qCal = new QCalendarWidget;
qBtn = new QPushButton(tr("Press me"));
connect(qBtn, SIGNAL(clicked()), this, SLOT(testCustomClick()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(qCal);
layout->addWidget(qBtn);
setLayout(layout);
qDebug() << "Date:" << QDate::currentDate();
}
Calendar::~Calendar()
{
}
void Calendar::testCustomClick()
{
QMouseEvent qm2(QEvent::MouseButtonPress, QPoint(qCal->width()/2,
qCal->height()/2), Qt::LeftButton , Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(qCal, &qm2);
//this one is always False
qDebug() << "isAccepted: " << qm2.isAccepted();
}
void Calendar::mousePressEvent(QMouseEvent* ev)
{
//this is executed even for QMouseEvent which sended to qCal =((
//if we click on qCal with real mouse it is not executed
qDebug() << "mouse event: " << ev << "x=" << ev->x() <<" y=" << ev->y();
QWidget::mousePressEvent(ev);
}
根据源代码 QApplication::sendEvent 调用 widget->event(),对于 QCalendarWidget,最终位于 QAbstractScrollArea 返回对于每个与鼠标相关的事件为 false。
如果我是对的,那么我如何模拟鼠标点击和按键呢?
Small toy app can be found here: http://gist.github.com/517445
I am trying to send artificial mouse event to widget and I use QApplication::sendEvent for that, next I check ev.isAccepted() and it returns False, even worse! Widget I've sent event to doesnt handle it (it is calendar widged and no date is picked) and I am in doubt that it even receives it, because I can see how mouseEventPressed is fired up on parent widget.
Qt Code:
#include "calendar.h"
Calendar::Calendar(QWidget *parent) :
QWidget(parent)
{
qCal = new QCalendarWidget;
qBtn = new QPushButton(tr("Press me"));
connect(qBtn, SIGNAL(clicked()), this, SLOT(testCustomClick()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(qCal);
layout->addWidget(qBtn);
setLayout(layout);
qDebug() << "Date:" << QDate::currentDate();
}
Calendar::~Calendar()
{
}
void Calendar::testCustomClick()
{
QMouseEvent qm2(QEvent::MouseButtonPress, QPoint(qCal->width()/2,
qCal->height()/2), Qt::LeftButton , Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(qCal, &qm2);
//this one is always False
qDebug() << "isAccepted: " << qm2.isAccepted();
}
void Calendar::mousePressEvent(QMouseEvent* ev)
{
//this is executed even for QMouseEvent which sended to qCal =((
//if we click on qCal with real mouse it is not executed
qDebug() << "mouse event: " << ev << "x=" << ev->x() <<" y=" << ev->y();
QWidget::mousePressEvent(ev);
}
According to source code QApplication::sendEvent calls widget->event() which for QCalendarWidget is ended up in QAbstractScrollArea which returns false for every mouse related event.
If I am right, then how can I emulate mouse clicks and keypresses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是将事件发送到光标下的确切小部件,而不是其父级。
}
Solution is to send event to exact widget under cursor, not to its parents.
}