Testing a QInputDialog with QTest

430 views
Skip to first unread message

Erkan Özgür Yılmaz

unread,
Nov 22, 2011, 11:53:24 AM11/22/11
to python_inside_maya
Hi everybody,

I'm re-writing our asset management system with all its Qt interfaces, and this time I used TDD practices. I'm stuck at testing QInputDialog. I can test a lot of other UI elements (for example, to test if they are enabled or showing the correct item etc.).

The problem is when I create the dialog everything stops working and is waiting for the UI to close. I've tried using threading an created a thread to call the QInputDialog in and created another one to test it but because there is no way to reach the dialog it self I can not test if it is shown and has the correct data in it.

So is there a way to reach the QInputDialog from the dialog it self?

Thanks...

E.Ozgur Yilmaz
Lead Technical Director
eoyilmaz.blogspot.com

Justin Israel

unread,
Nov 22, 2011, 12:35:55 PM11/22/11
to python_in...@googlegroups.com
How are you calling you QInputDialog?
My guess is that you are using the static convenience methods (or exec_) which cause your dialog to become modal, and thus block the main application.
What you probably want to do is:

self.d = QInputDialog(self)
self.d.setModal(False)
self.d.show()

At this point you can either decide to set a connection to a slot to handle when the window is closed,
or subclass it and overload your own accept() method.

# setting a connection
self.d.accepted.connect(handleInputMethod)

def handleInputMethod(self):
print self.d.textValue()

The static methods are good for when you don't need any special access or functionality from the dialog, as it creates it behind the scenes, blocks, and just returns the value and whether it was accepted or rejected.




Erkan Özgür Yılmaz

unread,
Nov 22, 2011, 4:38:50 PM11/22/11
to python_in...@googlegroups.com
Thanks Justin, it really helped me:

Just because all the examples were showing it like that, I was using:

asset_name, ok = QtGui.QInputDialog.getText(
    self,
    "Enter new asset name",
    "Asset name:"
)

to create a modal dialog. But as you suggested, using this:

input_dialog = QtGui.QInputDialog(self)
asset_name, ok = input_dialog.getText(
    self,
    "Enter new asset name",
    "Asset name:"
)

now I'm able to use the "input_dialog" to reach the input dialog. It was so simple.

Thank you very much...


E.Ozgur Yilmaz
Lead Technical Director
eoyilmaz.blogspot.com



Reply all
Reply to author
Forward
0 new messages