0

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 按钮的值以及如何显示对话框的任何想法?

4

2 回答 2

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
于 2013-12-04T03:53:39.313 回答
0

我对为什么要继承 QInputDialog 有点模糊,但这很容易。

如果想知道用户是接受还是拒绝对话,可以直接使用accepted()andrejected()方法。这些都是从 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()
于 2013-08-15T00:43:46.410 回答