PyQt5-调整大小(限制最大大小)输入对话框

时间:2019-05-03 10:11:33

标签: python pyqt pyqt5 qinputdialog

我读到以下内容:How to resize QInputDialog, PyQt,但它对我没有用,因为它似乎与PyQt4有关

这是我的代码段:

    def ImportURL(self):                     #URL dialog aufrufen
        InputDialog = QtWidgets.QInputDialog(self)
        i, okPressed = InputDialog.getText(self, "Import website", "Site to import:", QtWidgets.QLineEdit.Normal, "https://de.wikipedia.org/wiki/Wikipedia:Hauptseite")

        if okPressed:
            self.getWebsite(i)

我尝试在第二行添加.setFixedSize。我尝试在第2行和第3行之间添加InputDialog.setFixedSite(self)。没有任何作用,它要么崩溃要么创建第二个空窗口。我在这里俯瞰吗?

1 个答案:

答案 0 :(得分:1)

在另一个问题的答案中,没有解释问题的原因,因此在我的答案中将尝试涵盖尽可能多的内容

说明:

getText()方法是一个静态方法,这意味着不使用对象,如果在内部使用了该对象,但该对象不可访问。因此,您创建的InputDialog并不是您所显示的,因此您可以使用以下代码进行检查,因为您将看到2个窗口:

def ImportURL(self):
    InputDialog = QtWidgets.QInputDialog(self)
    InputDialog.show()
    i, okPressed = InputDialog.getText(self, "Import website", "Site to import:", QtWidgets.QLineEdit.Normal, "https://de.wikipedia.org/wiki/Wikipedia:Hauptseite")

    if okPressed:
        self.getWebsite(i)

解决方案:

因此有以下解决方案:

  • 利用您作为父代传递给self的优势,可以使用findChildren获取对象:
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        button = QtWidgets.QPushButton(
            "Open QInputDialog", clicked=self.ImportURL
        )
        vlay = QtWidgets.QVBoxLayout(self)
        vlay.addWidget(button)

    @QtCore.pyqtSlot()
    def ImportURL(self):
        QtCore.QTimer.singleShot(0, self.after_show)
        i, okPressed = QtWidgets.QInputDialog.getText(
            self,
            "Import website",
            "Site to import:",
            QtWidgets.QLineEdit.Normal,
            "https://de.wikipedia.org/wiki/Wikipedia:Hauptseite",
        )
        if okPressed:
            # self.getWebsite(i)
            print(i)

    @QtCore.pyqtSlot()
    def after_show(self):
        size = QtCore.QSize(500, 100)
        for d in self.findChildren(QtWidgets.QInputDialog):
            if d.isVisible():
                d.resize(size)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
  1. 不要使用getText()方法,而是创建一个实现相同逻辑的对象:
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        button = QtWidgets.QPushButton(
            "Open QInputDialog", clicked=self.ImportURL
        )
        vlay = QtWidgets.QVBoxLayout(self)
        vlay.addWidget(button)

    @QtCore.pyqtSlot()
    def ImportURL(self):
        dialog = QtWidgets.QInputDialog(self)
        dialog.resize(QtCore.QSize(500, 100))
        dialog.setWindowTitle("Import website")
        dialog.setLabelText("Site to Import")
        dialog.setTextValue(
            "https://de.wikipedia.org/wiki/Wikipedia:Hauptseite"
        )
        dialog.setTextEchoMode(QtWidgets.QLineEdit.Normal)
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            i = dialog.textValue()
            print(i)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

更新

QInputDialog的布局将QLayout :: SetMinAndMaxSize设置为sizeConstraint,因此固定大小将不起作用,诀窍是将其更改为QLayout :: SetDefaultConstraint:

from functools import partial

# ...

@QtCore.pyqtSlot()
def ImportURL(self):
    dialog = QtWidgets.QInputDialog(self)
    dialog.setWindowTitle("Import website")
    dialog.setLabelText("Site to Import")
    dialog.setTextValue(
        "https://de.wikipedia.org/wiki/Wikipedia:Hauptseite"
    )
    dialog.setTextEchoMode(QtWidgets.QLineEdit.Normal)
    wrapper = partial(self.on_timeout, dialog)
    QtCore.QTimer.singleShot(0, wrapper)
    if dialog.exec_() == QtWidgets.QDialog.Accepted:
        i = dialog.textValue()
        print(i)

def on_timeout(self, dialog):
    lay = dialog.layout()
    lay.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
    dialog.setFixedSize(QtCore.QSize(500, 100))