Results 1 to 6 of 6

Thread: Problem with QTest and QApplication

  1. #1
    Join Date
    Jan 2012
    Posts
    7
    Qt products
    Platforms
    Unix/X11 Windows

    Default Problem with QTest and QApplication

    I've run into a problem writing a test (in PyQt4). I need to test if a shortcut key, but it seems that QTest.keyClick() only works if there is a running QApplication. The example below shows the problem. Is this supposed to happen? All the examples I seen on the internet just instantiate a QApplication, but don't call exec on it.

    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui
    3. from PyQt4.QtCore import Qt
    4. from PyQt4.QtTest import QTest
    5.  
    6. # Create a sample widget
    7. class Widget(QtGui.QWidget):
    8. def __init__(self):
    9. super().__init__()
    10.  
    11. # Clicking the mouse on the widget will set the test keyClick, to make sure that the shortcut works.
    12. def mousePressEvent(self, *a, **k):
    13. QTest.keyClick(self, Qt.Key_C, Qt.ControlModifier)
    14. super().mousePressEvent(*a, **k)
    15.  
    16. # Slot to receive the keyClick signal
    17. def slot():
    18. print('triggered')
    19.  
    20. if __name__ == '__main__':
    21. app = QtGui.QApplication(sys.argv)
    22. w = Widget()
    23. w.show()
    24. act = QtGui.QAction('test', w)
    25. act.setShortcut(QtGui.QKeySequence('Ctrl+C'))
    26. act.setShortcutContext(Qt.WidgetWithChildrenShortcut)
    27. w.addAction(act)
    28. w.setFocusPolicy(Qt.StrongFocus)
    29. act.triggered.connect(slot)
    30.  
    31. # This does nothing, but it should call slot().
    32. QTest.keyClick(w, Qt.Key_C, Qt.ControlModifier)
    33.  
    34. # After this, the widget shows and clicking on it will trigger call the test.
    35. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QTest and QApplication

    Yes, this is the correct behaviour.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    7
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTest and QApplication

    But then how do you write a unit test (in python) for this?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QTest and QApplication

    You need a running event loop. The language doesn't matter here. The code you posted has an incorrect construction in the first place, read the docs for QTestLib --- you need a class inheriting QObject and you need to impement each test as a private slot of that class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Posts
    7
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTest and QApplication

    I see that from Qt's docs, but it isn't really clear from the PyQt4 docs, which imply that python's unittest framework should be used rather that Qt's QTestLib. Also, PyQt4 doesn't provide QTest::qExec(). Maybe I just need another cup of coffee, but it looks like the language does matter in this case...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QTest and QApplication

    If PyQt doesn't provide qExec then you simply can't use the framework or you need to provide your own wrapper to qExec.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 1st August 2011, 06:30
  2. QApplication::Session ID problem
    By baluk in forum Newbie
    Replies: 2
    Last Post: 9th December 2010, 08:55
  3. Replies: 0
    Last Post: 25th November 2009, 15:07
  4. Problem with QApplication::applicationVersion()
    By Tondog in forum Qt Programming
    Replies: 1
    Last Post: 9th July 2009, 10:25
  5. QApplication shut down problem
    By gsmiko in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2009, 06:58

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.