【问题标题】:How set options for a QInputDialog如何为 QInputDialog 设置选项
【发布时间】:2017-10-19 08:45:51
【问题描述】:

我正在尝试为我的QInputDialog 设置一些选项。但是如果我打电话给getText,这些设置就没有效果了。

如何更改从getText弹出的窗口的外观?

import sys
from PyQt5 import QtWidgets, QtCore


class Mywidget(QtWidgets.QWidget):
    def __init__(self):
        super(Mywidget, self).__init__()
        self.setFixedSize(800, 600)

    def mousePressEvent(self, event):
        self.opendialog()

    def opendialog(self):
        inp = QtWidgets.QInputDialog()

        ##### SOME SETTINGS
        inp.setInputMode(QtWidgets.QInputDialog.TextInput)
        inp.setFixedSize(400, 200)
        inp.setOption(QtWidgets.QInputDialog.UsePlainTextEditForTextInput)
        p = inp.palette()
        p.setColor(inp.backgroundRole(), QtCore.Qt.red)
        inp.setPalette(p)
        #####

        text, ok = inp.getText(w, 'title', 'description')
        if ok:
            print(text)
        else:
            print('cancel')

if __name__ == '__main__':
    qApp = QtWidgets.QApplication(sys.argv)
    w = Mywidget()
    w.show()
    sys.exit(qApp.exec_())

【问题讨论】:

    标签: python pyqt pyqt5 qinputdialog


    【解决方案1】:

    get* 方法都是静态的,这意味着它们可以在没有QInputDialog 类的实例的情况下调用。 Qt 为这些方法创建了一个 internal 对话框实例,因此您的设置将被忽略。

    要让您的示例正常工作,您需要设置更多选项,然后显式显示对话框:

    def opendialog(self):
        inp = QtWidgets.QInputDialog(self)
    
        ##### SOME SETTINGS
        inp.setInputMode(QtWidgets.QInputDialog.TextInput)
        inp.setFixedSize(400, 200)
        inp.setOption(QtWidgets.QInputDialog.UsePlainTextEditForTextInput)
        p = inp.palette()
        p.setColor(inp.backgroundRole(), QtCore.Qt.red)
        inp.setPalette(p)
    
        inp.setWindowTitle('title')
        inp.setLabelText('description')
        #####
    
        if inp.exec_() == QtWidgets.QDialog.Accepted:
            print(inp.textValue())
        else:
            print('cancel')
    
        inp.deleteLater()
    

    所以现在你或多或少地重新实现了 getText 所做的一切。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      相关资源
      最近更新 更多