Qt 人工鼠标点击无法正常工作

发布于 2024-09-14 09:40:39 字数 1688 浏览 1 评论 0原文

小玩具应用程序可以在这里找到: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2024-09-21 09:40:39

解决方案是将事件发送到光标下的确切小部件,而不是其父级。

void Calendar::testCustomClick()
{
   QPoint pos(qCal->width()/2,qCal->height()/2);
   QWidget *w = qApp->widgetAt(qCal->mapToGlobal(pos));
   qDebug() << "widget under point of click: " << w;

   {
   QMouseEvent qm2(QEvent::MouseButtonPress, pos, Qt::LeftButton , Qt::LeftButton,    Qt::NoModifier);
   QApplication::sendEvent(w, &qm2);
   }
   {
   QMouseEvent qm2(QEvent::MouseButtonRelease, pos, Qt::LeftButton , Qt::LeftButton,    Qt::NoModifier);
   QApplication::sendEvent(w, &qm2);
   }

}

Solution is to send event to exact widget under cursor, not to its parents.

void Calendar::testCustomClick()
{
   QPoint pos(qCal->width()/2,qCal->height()/2);
   QWidget *w = qApp->widgetAt(qCal->mapToGlobal(pos));
   qDebug() << "widget under point of click: " << w;

   {
   QMouseEvent qm2(QEvent::MouseButtonPress, pos, Qt::LeftButton , Qt::LeftButton,    Qt::NoModifier);
   QApplication::sendEvent(w, &qm2);
   }
   {
   QMouseEvent qm2(QEvent::MouseButtonRelease, pos, Qt::LeftButton , Qt::LeftButton,    Qt::NoModifier);
   QApplication::sendEvent(w, &qm2);
   }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文