Qt显示带文本输入的消息框

时间:2011-12-14 07:06:21

标签: qt pyqt

有没有简单明了的方法在Qt4(实际上是PyQt4)中显示一个消息框,支持文本输入而不是从预定义列表中选择一个按钮?我可以(而且已经部分地)通过编写一个自定义类来完成这个,只是为了显示这样的对话框但是不是更清洁的方式吗?

编辑:感谢Luca Carlon,我得到了它的工作。但是,如果其他人需要这个,我将在下面发布工作PyQt4代码

from PyQt4.QtGui import QInputDialog
#This code works only inside a method of a widget or window as self must refer to a
#valid widget or window to get the correct modality, although we can give None instead
(text,truth)=QInputDialog.getText(self,"Get text","User name",QLineEdit.Normal,"NoName")
if truth:
    #The user has accepted the edit, he/she has clicked OK
    print text
else:
    #The user has not accepted the edit, he/she has clicked Cancel
    print "No change"

1 个答案:

答案 0 :(得分:6)

使用QInputDialog。静态方法getText可能就足够了。