Python 子类化QInputDialog.getItems
QInputDialog.getItems是一个静态方法,其“构造函数”是: 我想将其子类化,但我无法找到以下方法:Python 子类化QInputDialog.getItems,python,pyqt,Python,Pyqt,QInputDialog.getItems是一个静态方法,其“构造函数”是: 我想将其子类化,但我无法找到以下方法: 显示对话框 如果按下或未按下ok按钮,则返回True或False 我尝试过类似的方法,但并不十分成功: from PyQt4 import QtGui class DialogPerso(QtGui.QInputDialog): def __init__(self): super(DialogPerso, self).__init__()
- 显示对话框
- 如果按下或未按下ok按钮,则返回True或False
from PyQt4 import QtGui
class DialogPerso(QtGui.QInputDialog):
def __init__(self):
super(DialogPerso, self).__init__()
def getItem(parent, title, label, items, current = 0, editable = True, flags = 0):
string = "prout"
print(parent)
print(title)
print(label)
print(items)
return string, QtGui.QInputDialog.result()
getItem = staticmethod(getItem)
我现在只能返回字符串。你知道如何获取ok按钮的值,以及如何显示对话框吗?我不太清楚为什么要将QInputDialog子类化,但这很简单 如果您想知道用户是否接受或拒绝了该对话框,可以直接使用
accepted()
和rejected()
方法。这些都是从QDialog继承来的,这可能是您错过它们的原因。幸运的是,通过子类化QInputDialog,这些也将被类继承:
d = DialogPerso(**args) #args set elsewhere
if d.rejected():
print "The user didn't hit 'OK'
elif d.accepted():
print "The user say 'OK', and entered %s" % d.result()
不确定这是否真的值得做,但下面的内容或多或少等同于C++原件:
class DialogPerso(QtGui.QInputDialog):
@staticmethod
def getItem(parent, title, label, items,
current=0, editable=True, flags=0, hints=0):
if 0 <= current < len(items):
text = items[current]
elif items:
text = items[0]
else:
text = ''
dialog = QtGui.QInputDialog(
parent, QtCore.Qt.WindowFlags(flags))
dialog.setWindowTitle(title)
dialog.setLabelText(label)
dialog.setComboBoxItems(items)
dialog.setTextValue(text)
dialog.setComboBoxEditable(editable)
dialog.setInputMethodHints(QtCore.Qt.InputMethodHints(hints))
if dialog.exec_() == QtGui.QDialog.Accepted:
return dialog.textValue(), True
return text, False
类对话框perso(QtGui.QInputDialog):
@静力学方法
def getItem(父项、标题、标签、项、,
当前=0,可编辑=True,标志=0,提示=0):
如果0,我不知道你在问什么。那不是一个构造器;你可能是指方法的签名。你说“如何显示对话框”是什么意思?在你发布的代码中,你没有创建任何对话框(你可能应该这样做)。我编辑了我的帖子,向你展示了我试图子类化的整个类。是的,我不创建任何对话框,因为我不知道如何创建。该方法是静态的,因此参数中没有“self”。我无法执行self.show(),因此,您必须创建一个新对话框。执行dialog=DialogPerso(..)
,然后使用dialog.exec()
显示它。好的,谢谢,我现在有了对话框。但是在getItem()中,我还必须返回用户输入:如果他按下“ok”或“cancel”。我怎样才能得到这个值?顺便问一下,exec_uz()和show()之间有什么区别?
class DialogPerso(QtGui.QInputDialog):
@staticmethod
def getItem(parent, title, label, items,
current=0, editable=True, flags=0, hints=0):
if 0 <= current < len(items):
text = items[current]
elif items:
text = items[0]
else:
text = ''
dialog = QtGui.QInputDialog(
parent, QtCore.Qt.WindowFlags(flags))
dialog.setWindowTitle(title)
dialog.setLabelText(label)
dialog.setComboBoxItems(items)
dialog.setTextValue(text)
dialog.setComboBoxEditable(editable)
dialog.setInputMethodHints(QtCore.Qt.InputMethodHints(hints))
if dialog.exec_() == QtGui.QDialog.Accepted:
return dialog.textValue(), True
return text, False