Results 1 to 17 of 17

Thread: Qtest does not emit signal

  1. #1
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qtest does not emit signal

    I want to simulate mouse click in a application. I use QT::mouseclick and QT::mousemove to click in different positions. when I run code, position of my pushbutton was clicked but the related SLOT doesn't run. Doesn't QTest emit signal? How do I simulate mouse click with signal?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    QTest is a namespace and doesn't have any signals.

    If a widget has a signal and you provide the input it needs to emit it, then it will do so. It doesn't know whether it is in an application or running in a test.

    So if you don't get a slot called, then there is either a problem with the signal/slot connection, in which case you can try QSignalSpy to verify this, or the widget has not received the events that it needs to trigger the emit, in which case you either have to debug how the correct event sequence is or find another way to trigger, e.g. in case of a button QAbstractButton::click().

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    NIK-VAJ (18th February 2016)

  4. #3
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    thanks. I try click() and it was OK but could I find a way that doesn't need the name of pushbuttons or other elements? I want to use only x and y position to simulate fake click and get signal of events.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    When you used the QTest::mouseClick() method, did you use the one taking a QWidget or the one taking a QWindow?

    Cheers,
    _

  6. #5
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    I use this code:

    QTest::mouseMove(this,mapToGlobal(QPoint(x,y)));
    QTest::mouseClick(this, Qt::LeftButton,0, mapToGlobal(QPoint(x,y)),-1);

    "this" is mainwindow. my code shoud be independent of name of widgets like pushbutton.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    mapToGlobal doesn't make much sense unless you have ensured that the windows is positioned at 0,0 and thus global/local coordinates being the same.

    In either case, you likely need the overload that takes a QWindow, the one that takes a QWidget delivers mouse event to that specific widget.

    Cheers,
    _

  8. #7
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    thanks for your quick replies
    even when I use qwidget in mousclick function, it doesn't emit a signal!

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    Quote Originally Posted by NIK-VAJ View Post
    thanks for your quick replies
    even when I use qwidget in mousclick function, it doesn't emit a signal!
    So you changed the code to send the clicks to the button and it does not emit the signal?
    Can you show the code you have now?

    Cheers,
    _

  10. #9
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    if (event->button()==Qt::RightButton){
    qDebug() << Qt::RightButton << pos();
    QPoint Mpush = pos();
    QTest::mouseMove(this->m_button,Mpush);
    QTest::mouseClick(this->m_button, Qt::LeftButton,0,Mpush,-1);}
    in this code, when I Rightclicked on button, the position of cursor places in Mpush and then lift click is simulated. it doesn't emit pushbutton(m_button) signal.
    Last edited by NIK-VAJ; 18th February 2016 at 16:48.

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    So you are trying to simulate a mouse click on "m_button", at x=751, y=327 in "m_button".

    Are you certain that the button is really that large?

    Is it important for your button where it is clicked?
    If not you could just omit the position and it gets clicked in its center.

    Cheers,
    _

  12. #11
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    pos() is not position of cursor in the screen?
    and how can I find center of poshbutton?

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    Quote Originally Posted by NIK-VAJ View Post
    pos() is not position of cursor in the screen?
    Well, I assume that "this" in your code is a QWidget derived class, which makes pos() most likely this function: QWidget::pos().

    Of course if "this" is a class of your own and you have implemented a pos() function that returns the cursor on screen, than that is what it will return.
    In that case you need to make sure to map it to the button's coordinate system.

    Quote Originally Posted by scgrant327 View Post
    and how can I find center of poshbutton?
    button->rect().center(), but why do you need that?

    Cheers,
    _

  14. #13
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    I use this line of code and it emits signal
    QTest::mouseClick(this->m_button, Qt::LeftButton,0,m_button->rect().center(),-1);

  15. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    Clicking the center is the default behavior if you don't specify a position, so you could just call

    Qt Code:
    1. QTest::mouseClick(this->m_button, Qt::LeftButton);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  16. The following user says thank you to anda_skoa for this useful post:

    NIK-VAJ (18th February 2016)

  17. #15
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    I have another question. which function give me position of a widget in main window?

  18. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    pos() gives you the position relative to its parent.
    The parent's mapToGlobal() can map that to a global positon, the main window's mapFromGlobal can turn that into a position within main window.

    Cheers,
    _

  19. #17
    Join Date
    Feb 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qtest does not emit signal

    thank you very much

Similar Threads

  1. Seems that my emit doesn't emit the signal
    By roseicollis in forum Newbie
    Replies: 2
    Last Post: 19th January 2015, 16:05
  2. Replies: 2
    Last Post: 3rd May 2011, 20:22
  3. Replies: 3
    Last Post: 21st December 2010, 12:31
  4. how to know which button emit the signal?
    By coder1985 in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2008, 14:26
  5. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.