子类化QInputDialog.getItems

时间:2013-08-12 18:16:26

标签: python pyqt

QInputDialog.getItems是一个静态方法,它的“构造函数”是:

(QString, bool ok) QInputDialog.getItem (QWidget parent, QString title, QString label, QStringList list, int current = 0, bool editable = True, Qt.WindowFlags flags = 0)

我想将其子类化,但我无法找到方法:

  • 显示对话框
  • 如果按下确定按钮,则返回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按钮的值以及如何显示对话框的任何想法?

2 个答案:

答案 0 :(得分:1)

不确定这是否真的值得做,但以下内容或多或少等同于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

答案 1 :(得分:0)

对于你想要继承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()