使窗口大小适合内容 QT4

时间:2021-06-22 14:58:32

标签: python pyqt qmainwindow

我正在开发一个允许通过 TCP 连接到计算机的应用程序。每个新连接在 QVBoxLayout 中添加一个自定义小部件。这个 QVBoxLayout 位于 QGridLayout 的 QMainWindow 的 centralWidget 内。 当连接关闭时,关联的小部件将从 QVBoxLayout 中删除。这是问题:当我的自定义小部件从布局中删除时,窗口没有以所需的方式调整大小。 我将自定义小部件的大小策略设置为固定

首先,在启动时,窗口如下所示:

enter image description here

然后,我添加我的小部件:

enter image description here

然后,我删除小部件:

enter image description here

窗口的高度没有更新到最小值。我想检索第一张图片的外观。 onPythonAboutToClose() 是继承自 QMainWindow 的类的成员函数。

请在下面找到重现问题的最小示例:

ma​​in.py :

from PyQt4.QtGui import QApplication
from UI import UI
import sys

if __name__ == '__main__':

    app = QApplication(sys.argv)


    ui = UI()
    ui.show()

    app.exec_()

UI.py

from PyQt4 import QtCore
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QWidget, QGridLayout, QMainWindow, QLineEdit, QPushButton, QLabel, QSizePolicy, QLayout, \
    QVBoxLayout

from PythonContextWidget import PythonContextWidget


class UI(QMainWindow):

    def __init__(self, parent = None):
        super(UI, self).__init__(parent)


        self.setWindowTitle("Watcher")
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.Tool | QtCore.Qt.MSWindowsFixedSizeDialogHint)


        # the Qt.Tool flag clears the quit on close flag that exits the event loop
        # We have to set it again manually
        self.setAttribute(Qt.WA_QuitOnClose)

        self.setMinimumWidth(300)
        self.setFixedWidth(300)


        self.mainLayout = QGridLayout(parent)

        self.connectButton = QPushButton("Connect", parent)
        self.connectButton.setToolTip("Connect to Python")
        self.connectButton.clicked.connect(self.onClickAddButton)

        self.labelConnectionStatus = QLabel(parent)
        self.labelConnectionStatus.setVisible(False)

        self.lineEditIP = QLineEdit(parent)
        self.lineEditIP.setMaxLength(15)
        self.lineEditIP.setText("")

        self.pythonWidgetContainer = QVBoxLayout(parent)

        self.mainLayout.addWidget(self.lineEditIP, 0, 0)
        self.mainLayout.addWidget(self.connectButton, 0, 1)
        self.mainLayout.addWidget(self.labelConnectionStatus, 1, 0)
        self.mainLayout.addLayout(self.pythonWidgetContainer, 2, 0, 1, 2)



        centralWidget = QWidget(parent)
        centralWidget.setLayout(self.mainLayout)
        self.setCentralWidget(centralWidget)

        self.listPythonWidget = []
        self.listPythonClient = []

        self.pythonWidgetContainer.setSizeConstraint(QLayout.SetFixedSize)


    def onClickAddButton(self):
        """
            Add a PythonContextWidget to QVBoxLayout
        :return: 
        """
        pythonAutomationContextWidget = PythonContextWidget(self.parent())
        pythonAutomationContextWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.pythonWidgetContainer.addWidget(pythonAutomationContextWidget)
        self.listPythonWidget.append(pythonAutomationContextWidget)



    def removePythonContextWidget(self, idx):

        # remove the widget from its container
        self.pythonWidgetContainer.removeWidget(self.listPythonWidget[idx])
        self.listPythonWidget[idx].deleteLater()
        self.listPythonWidget.pop(idx)
        self.listPythonClient.pop(idx)


        self.adjustSize()

PythonContextWidget.py

from PyQt4.QtGui import QGridLayout, QLabel, QProgressBar, QFrame, QPushButton


class PythonContextWidget(QFrame):

    def __init__(self, parent = None):
        super(PythonContextWidget, self).__init__(parent)

        self.mainLayout = QGridLayout(parent)

        self.txtComputer = QLabel(parent)
        self.txtComputer.setText("IP:")

        self.txtPlan = QLabel(parent)
        self.txtPlan.setText("Plan:")

        self.txtStatus = QLabel(parent)
        self.txtStatus.setText("Status:")

        self.txtExecutionTime = QLabel(parent)
        self.txtExecutionTime.setText("Execution time:")

        self.progressBar = QProgressBar(parent)


        self.btnRemove = QPushButton("x", parent)
        self.btnRemove.clicked.connect(self.deleteLater)

        self.mainLayout.addWidget(self.txtComputer, 0, 0)
        self.mainLayout.addWidget(self.btnRemove, 0, 1)
        self.mainLayout.addWidget(self.txtStatus, 1, 0)
        self.mainLayout.addWidget(self.txtPlan, 2, 0)
        self.mainLayout.addWidget(self.txtExecutionTime, 3, 0)
        self.mainLayout.addWidget(self.progressBar, self.mainLayout.rowCount(), 0)

        self.setLayout(self.mainLayout)

        self.applyStyle()

    def applyStyle(self):
        style = ".PythonContextWidget{background: rgba(51, 187, 255, 50);" \
                "border: 1px solid #33bbff;" \
                "border-radius:4px;}"
        self.setStyleSheet(style)

删除小部件的按钮连接到小部件的deleteLater()。这是删除按钮,但布局的大小似乎没有改变

你有什么想法吗? 谢谢!

0 个答案:

没有答案