English 中文(简体)
当我派遣或张贴QMouseEvent时,在QPushbutton的阵地,没有发出被点击的信号。
原标题:When I send or post a QMouseEvent, at the position of a QPushbutton, its clicked() signal is not emitted

我试图设计一个吉卡,以便使用直截面镜加强现实应用。 想法是,利用kin子跟踪探测的手,通过姿态控制申请。

这个问题并不涉及姿态,因为我的评估的这一部分工作是罚款的。

在我的申请中,每当进行点击姿态时,就希望模拟点火。 为了做到这一点,我发出了两场事件,一次是空洞的,一次是空洞的,一次是空洞的,因为通常的点击也是一系列新闻和释放。

The whole thing works fine on a QWebView. In the browser window, i can "click" on links.

But for some reason i cannot "click" on a QPushButton. The clicked() signal is not emitted. I have a short example to illustrate my problem:

First the main function:

int main(int argc, char **argv){
    QApplication app( argc, argv );
    QWebViewButtons mainapp( 0, Qt::Window );
    app.setActiveWindow( &mainapp );
    mainapp.show();
    mainapp.setApplication( &app ); //this is a setter to also get access to the main application in the widget
    return app.exec();
}

This is my own widget:

QWebViewButtons::QWebViewButtons(QWidget* parent, Qt::WindowFlags flags ): QWidget(parent, flags ){
    this->m_ui.setupUi( this );
    QObject::connect( this->m_ui.pushButton, SIGNAL(clicked(bool)), this, SLOT( buttonClicked(bool) ) );
}

void QWebViewButtons::mousePressEvent( QMouseEvent* event ){
    printf("mouse click, %d, %d
", event->pos().x(), event->pos().y() );
}

void QWebViewButtons::buttonClicked( bool clicked ){
    printf("slot button clicked
");    
}

void QWebViewButtons::keyPressEvent( QKeyEvent* event ){
    printf("Key pressed
"); 
    QPoint pos( this->width()/2, this->height()/2 );
    QPoint global = this->mapToGlobal(pos);
    QWidget *w = this->m_app->widgetAt(global);
    //printf("widget under point of click: %s", w);
    QApplication::sendEvent( w, new QMouseEvent( QEvent::MouseButtonPress, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier ) );
    QApplication::sendEvent( w, new QMouseEvent( QEvent::MouseButtonRelease, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier ) );
}

I followed the suggestion here: Qt Artificial mouse clicks doesnt work properly and send to send my mouse events directly to the QPushButton. But that didn t help. I also tried to send the mouse events to "this", or the main app.

Now I am running out of ideas. What I want to have is, that the buttonClicked() slot is called if i press any key. But i only is called if i click the button with my mouse. How can i accomplish this? Or is my basic idea completely false?

Thanks for your ideas.

最佳回答

Ok, the trick was to really send the click to the EXACT widget at the desired click position. In my application i had some trouble because of semi transparent widgets lying over each other.

So the widgetAt(global) command wouldnt help me. But you can use childAt(global) instead. Of course you need to know from which widget you want to find the child.

That did the trick for me. :-)

问题回答

So, do I get that right? When doing your "click-gesture" you come into keyPressEvent? If so, you can check whether the "click" has been done above the button and explicitly call the button s clicked() signal. But that s not what you want?

And what exactly is QWebViewButtons? The area the user does his gestures in?

Have you debugged into the keyPressEvent to see if your sendEvent has the correct widget (w)? I can t see why the widget should not recieve the event...

并且记住,在新陈词和通过传票发送时,永远不会删除。 在使用传票时,你就应该打上你的活动。

或许应该看看一下这一深层,在那里建议使用一种软射线器:。 ick lick 使用Qt的轮船 但是,我可以想象,你不希望测试职能能够发挥trick的作用。





相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签