0

我正在尝试实现一个能够从用户那里获取参数的窗口。在这个窗口中,用户应该给出一些行(有 N 个参数集)。从这个数字我想QTextEdit为每个N参数集自动生成集。所以从现在开始我有这个代码

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from PyQt4 import QtGui
import sys
import numpy as np

class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()

        # General Window
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QVBoxLayout()

        # parameter2
        self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
        self.horizontalGroupBox_Param2.setMaximumHeight(80)
        self.Param2_layout = QtGui.QVBoxLayout()
        grid2 = QtGui.QGridLayout()
        self.Param2_layout.addLayout(grid2)
        self.Param2_Label = QtGui.QLabel('Name')
        self.Param2 = QtGui.QComboBox()
        self.Param2.addItems(["a","b","c"])
        self.Param2.setMinimumWidth(100)
        # self.Param2_layout.addWidget(self.Param2)
        self.Param2_min_Label = QtGui.QLabel('min')
        self.Param2_min_Edit = QtGui.QTextEdit('1')
        self.Param2_min_Edit.setMaximumWidth(40)
        self.Param2_max_Label = QtGui.QLabel('max')
        self.Param2_max_Edit = QtGui.QTextEdit('5')
        self.Param2_max_Edit.setMaximumWidth(40)
        self.Param2_step_Label = QtGui.QLabel('step')
        self.Param2_step_Edit = QtGui.QTextEdit('1')
        self.Param2_step_Edit.setMaximumWidth(40)
        grid2.addWidget(self.Param2_Label, 0, 0)
        grid2.addWidget(self.Param2, 1, 0)
        grid2.addWidget(self.Param2_min_Label, 0, 1)
        grid2.addWidget(self.Param2_min_Edit, 1, 1)
        grid2.addWidget(self.Param2_max_Label, 0, 2)
        grid2.addWidget(self.Param2_max_Edit, 1, 2)
        grid2.addWidget(self.Param2_step_Label, 0, 3)
        grid2.addWidget(self.Param2_step_Edit, 1, 3)
        self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)

          #action button
        self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
        self.horizontalGroupBox_Actions.setMaximumHeight(100)
        self.layout_Actions = QtGui.QHBoxLayout()
        self.Button_Run=QtGui.QPushButton('Run')
        self.nbparam = QtGui.QTextEdit('2')
        self.nbparam.setMaximumHeight(30)
        self.nbparam.setMaximumWidth(50)
        self.layout_Actions.addWidget(self.Button_Run)
        self.layout_Actions.addWidget(self.nbparam)
        self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)

        # set layout

        self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
        self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

        self.Button_Run.clicked.connect(self.Runclick)

    def Runclick(self):

         # parameter2
        self.horizontalGroupBox_Param = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
        nbparam = int(self.nbparam.toPlainText())
        self.Param_layout = QtGui.QVBoxLayout()
        grid2 = QtGui.QGridLayout()
        self.Param_layout.addLayout(grid2)
        self.Param_Label = QtGui.QLabel('Names')
        self.Param_min_Label = QtGui.QLabel('mins')
        self.Param_max_Label = QtGui.QLabel('maxs')
        self.Param_step_Label = QtGui.QLabel('steps')
        grid2.addWidget(self.Param_Label, 0, 0)
        grid2.addWidget(self.Param_min_Label, 0, 1)
        grid2.addWidget(self.Param_max_Label, 0, 2)
        grid2.addWidget(self.Param_step_Label, 0, 3)

        self.listofedit = []
        text=["a","b","c"]
        for i in np.arange(nbparam):
            print(i)
            self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('10'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
            self.listofedit[i][0].addItems(text)
            self.listofedit[i][0].setMinimumWidth(100)
            self.listofedit[i][1].setMaximumWidth(40)
            self.listofedit[i][2].setMaximumWidth(40)
            self.listofedit[i][3].setMaximumWidth(40)
            grid2.addWidget(self.listofedit[i][0], i+1, 0)
            grid2.addWidget(self.listofedit[i][1], i+1, 1)
            grid2.addWidget(self.listofedit[i][2], i+1, 2)
            grid2.addWidget(self.listofedit[i][3], i+1, 3)

        self.horizontalGroupBox_Param.setLayout(self.Param_layout)
        # while self.horizontalGroupBox_Param2.layout().count():
        #     self.horizontalGroupBox_Param2.layout().itemAt(0).widget().deleteLater()
        self.horizontalGroupBox_Param2.layout().clear()
        self.horizontalGroupBox_Param2.setLayout(self.Param_layout)
        return

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.show()
    sys.exit(app.exec_())

这产生了我这个窗口:

在此处输入图像描述

所以我想要的是当我点击运行时(右上角的数字是行数N),我在窗口下部修改参数集的数量。所以在这种情况下,我应该有两行输入两个名称,两个最小值,两个最大值和两个步长值。我开始更新窗口,但我很难找到正确的代码放入 def Runclick(self):

我已经编写了代码来生成正确数量的参数集行,但我无法用它们更新我的窗口。

我显然在最后一部分写了一些东西:

self.horizontalGroupBox_Param.setLayout(self.Param_layout)
# while self.horizontalGroupBox_Param2.layout().count():
# self.horizontalGroupBox_Param2.layout().itemAt(0).widget().deleteLater() 
self.horizontalGroupBox_Param2.setLayout(self.Param_layout)

我认为我没有以正确的方式处理事情。因此,如果有人对此有解决方案,我将不胜感激

更新

嗨@jgorosdev,感谢您的回答。虽然,它并不完全符合我想要的。在你给我的代码中,它可以正常工作,因为参数集总是在窗口底部更新,但在我更高级的软件中,后面还有其他一些 QGroupBox。例如,如果我添加一个类似部分的控制台:

 # -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from PyQt4 import QtGui

import sys
import numpy as np

class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()

        # General Window
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QVBoxLayout()

        #action button
        self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
        self.horizontalGroupBox_Actions.setMaximumHeight(100)
        self.layout_Actions = QtGui.QHBoxLayout()
        self.Button_Run=QtGui.QPushButton('Run')
        self.nbparam = QtGui.QTextEdit('3')
        self.nbparam.setMaximumHeight(30)
        self.nbparam.setMaximumWidth(50)
        self.layout_Actions.addWidget(self.Button_Run)
        self.layout_Actions.addWidget(self.nbparam)
        self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)

        self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)
        self.updateWidget = None

        # Param2 layout
        self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
        self.Param2_layout = QtGui.QVBoxLayout()
        self.Button_Run.clicked.connect(self.UpdateLayout)
        self.UpdateLayout()

        # console
        self.horizontalGroupBox_Console2 = QtGui.QGroupBox("Console ")
        self.layout_Consol_Label2 = QtGui.QVBoxLayout()
        self.Consol_Label2 = QtGui.QTextEdit('Console :')
        self.layout_Consol_Label2.addWidget(self.Consol_Label2)
        self.horizontalGroupBox_Console2.setLayout(self.layout_Consol_Label2)
        self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Console2)

        # parameter2
        # From now on everything well be created inside a function that updates the layout
    def UpdateLayout(self):
        if (self.updateWidget == None):
            self.updateWidget = QtGui.QWidget()
        else:
            self.mainHBOX_param_scene.removeWidget(self.updateWidget)
            self.updateWidget.deleteLater()
            self.updateWidget = None

        cnt =  self.mainHBOX_param_scene.layout().count()
        print("Widget count:" +str(cnt)+ "")
        nbparam = int(self.nbparam.toPlainText())
        #self.horizontalGroupBox_Param2.setMaximumHeight(150)
        grid2 = QtGui.QGridLayout()
        self.Param2_Label = QtGui.QLabel('Name')
        self.Param2 = QtGui.QComboBox()
        self.Param2.addItems(["a","b","c"])
        self.Param2.setMinimumWidth(100)


        # self.Param2_layout.addWidget(self.Param2)
        self.Param2_min_Label = QtGui.QLabel('min')
        self.Param2_max_Label = QtGui.QLabel('max')
        self.Param2_step_Label = QtGui.QLabel('step')

        grid2.addWidget(self.Param2_Label, 0, 0)
        grid2.addWidget(self.Param2_min_Label, 0, 1)
        grid2.addWidget(self.Param2_max_Label, 0, 2)
        grid2.addWidget(self.Param2_step_Label, 0, 3)

        self.listofedit = []
        text=["a","b","c"]
        for i in np.arange(nbparam):
            print(i)
            self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('20'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
            self.listofedit[i][0].addItems(text)
            self.listofedit[i][0].setMinimumWidth(100)
            self.listofedit[i][1].setMaximumWidth(40)
            self.listofedit[i][1].setMaximumHeight(40)
            self.listofedit[i][2].setMaximumWidth(40)
            self.listofedit[i][2].setMaximumHeight(40)
            self.listofedit[i][3].setMaximumWidth(40)
            self.listofedit[i][3].setMaximumHeight(40)
            grid2.addWidget(self.listofedit[i][0], i+1, 0)
            grid2.addWidget(self.listofedit[i][1], i+1, 1)
            grid2.addWidget(self.listofedit[i][2], i+1, 2)
            grid2.addWidget(self.listofedit[i][3], i+1, 3)

        self.updateWidget = QtGui.QWidget()
        self.updateWidget.setLayout(grid2)
        #self.updateWidget.setMaximumHeight(nbparam*60)
        self.Param2_layout.addWidget(self.updateWidget)
        self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
        self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
        return

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.show()
    sys.exit(app.exec_())

我一开始就有这个窗口:

在此处输入图像描述

这是我想要的,但我在单击“运行”按钮后得到了这张图片:

在此处输入图像描述

所以控制台和参数集是倒置的。我想知道是否可以在不重新构建所有布局的情况下替换部分布局?

4

1 回答 1

2

我对代码做了一些小的改动,现在运行良好。

您在代码中创建了两次底部分组框,我认为这是一种更好的方法。我已经在UpdateLayout函数中打包了所有内容。

您可能需要重新检查调整 qt 大小的策略以正确调整主窗口的大小。

带有 UPDATE的代码:

 # -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from PyQt4 import QtGui

import sys
import numpy as np

class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()

        # General Window
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QVBoxLayout()

        #action button
        self.horizontalGroupBox_Actions = QtGui.QGroupBox("Actions")
        self.horizontalGroupBox_Actions.setMaximumHeight(100)
        self.layout_Actions = QtGui.QHBoxLayout()
        self.Button_Run=QtGui.QPushButton('Run')
        self.nbparam = QtGui.QTextEdit('3')
        self.nbparam.setMaximumHeight(30)
        self.nbparam.setMaximumWidth(50)
        self.layout_Actions.addWidget(self.Button_Run)
        self.layout_Actions.addWidget(self.nbparam)
        self.horizontalGroupBox_Actions.setLayout(self.layout_Actions)

        self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Actions)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)
        self.updateWidget = None

        # Param2 layout
        self.horizontalGroupBox_Param2 = QtGui.QGroupBox("Set range Parameter 2 for matrix method")
        self.Param2_layout = QtGui.QVBoxLayout()
        self.Button_Run.clicked.connect(self.UpdateLayout)
        self.UpdateLayout()

        # console
        self.horizontalGroupBox_Console2 = QtGui.QGroupBox("Console ")
        self.layout_Consol_Label2 = QtGui.QVBoxLayout()
        self.Consol_Label2 = QtGui.QTextEdit('Console :')
        self.layout_Consol_Label2.addWidget(self.Consol_Label2)
        self.horizontalGroupBox_Console2.setLayout(self.layout_Consol_Label2)
        self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Console2)

        # parameter2
        # From now on everything well be created inside a function that updates the layout
    def UpdateLayout(self):
        if (self.updateWidget == None):
            self.updateWidget = QtGui.QWidget()
        else:
            self.mainHBOX_param_scene.removeWidget(self.updateWidget)
            self.updateWidget.deleteLater()
            self.updateWidget = None

        cnt =  self.mainHBOX_param_scene.layout().count()
        print("Widget count:" +str(cnt)+ "")
        nbparam = int(self.nbparam.toPlainText())
        #self.horizontalGroupBox_Param2.setMaximumHeight(150)
        grid2 = QtGui.QGridLayout()
        self.Param2_Label = QtGui.QLabel('Name')
        self.Param2 = QtGui.QComboBox()
        self.Param2.addItems(["a","b","c"])
        self.Param2.setMinimumWidth(100)


        # self.Param2_layout.addWidget(self.Param2)
        self.Param2_min_Label = QtGui.QLabel('min')
        self.Param2_max_Label = QtGui.QLabel('max')
        self.Param2_step_Label = QtGui.QLabel('step')

        grid2.addWidget(self.Param2_Label, 0, 0)
        grid2.addWidget(self.Param2_min_Label, 0, 1)
        grid2.addWidget(self.Param2_max_Label, 0, 2)
        grid2.addWidget(self.Param2_step_Label, 0, 3)

        self.listofedit = []
        text=["a","b","c"]
        for i in np.arange(nbparam):
            print(i)
            self.listofedit.append([QtGui.QComboBox(),QtGui.QTextEdit('20'),QtGui.QTextEdit('50'),QtGui.QTextEdit('10')])
            self.listofedit[i][0].addItems(text)
            self.listofedit[i][0].setMinimumWidth(100)
            self.listofedit[i][1].setMaximumWidth(40)
            self.listofedit[i][1].setMaximumHeight(40)
            self.listofedit[i][2].setMaximumWidth(40)
            self.listofedit[i][2].setMaximumHeight(40)
            self.listofedit[i][3].setMaximumWidth(40)
            self.listofedit[i][3].setMaximumHeight(40)
            grid2.addWidget(self.listofedit[i][0], i+1, 0)
            grid2.addWidget(self.listofedit[i][1], i+1, 1)
            grid2.addWidget(self.listofedit[i][2], i+1, 2)
            grid2.addWidget(self.listofedit[i][3], i+1, 3)

        self.updateWidget = QtGui.QWidget()
        self.updateWidget.setLayout(grid2)
        #self.updateWidget.setMaximumHeight(nbparam*60)
        self.Param2_layout.addWidget(self.updateWidget,)
        self.horizontalGroupBox_Param2.setLayout(self.Param2_layout)
        #self.mainHBOX_param_scene.addWidget(self.horizontalGroupBox_Param2)
        # NEW
        self.mainHBOX_param_scene.insertWidget(1,self.horizontalGroupBox_Param2)

        return

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.show()
    sys.exit(app.exec_())

对更新的回答

如果您愿意在不改变位置的情况下更新QGroupBox,addWidget()那么您将需要的功能不是insertWidget()功能。

我已经更新了UpdateLayout添加小部件的函数中的行。希望这适合你。

函数的引用如下:

http://doc.qt.io/qt-4.8/qboxlayout.html#insertWidget

于 2017-01-09T12:22:38.503 回答