PyQt:removeChild/addChild QGroupBox

发布于 2024-08-12 06:49:43 字数 1503 浏览 4 评论 0原文

我正在为客户开发一个系统,该系统显示在一组选项卡中,并在中央小部件中显示一个表格,其中包含从数据库中提取的数据。

根据鼠标事件,必须从centralwidget 中删除容器(groupBox),或者然后添加表的新更新数据。

下面是一段运行良好的代码,显示了 GroupBox 内包含数据的表格:

    self.tab_tableview = QtGui.QWidget()
    self.tab_tableview.setObjectName("tab_tableview")

    self.viewGroupBox = QtGui.QGroupBox(self.tab_tableview)
    self.viewGroupBox.setGeometry(QtCore.QRect(10, 0, 751, 501))
    self.viewGroupBox.setObjectName("viewGroupBox")

    self.vBox = QtGui.QVBoxLayout()
    self.vBox.addWidget(self.newGroupBox)
    self.vBox.setGeometry(QtCore.QRect(40, 170, 171, 111))
    self.vBox.addStretch(1)

    self.viewTableWidget = QtGui.QTableView(self.viewGroupBox)
    self.viewTableWidget.setGeometry(QtCore.QRect(10, 20, 731, 471))
    self.viewTableWidget.setObjectName("viewTableWidget")

    updatedTableModel=self.callShowTable() 
    self.viewTableWidget.setModel(updatedTableModel)

    self.viewTableWidget.setColumnWidth(0,30)
    self.viewTableWidget.setColumnWidth(1,550)

    self.viewTabWidget.addTab(self.tab_tableview, "")

    if removeContainer_Bottun_Pressed:
        print "remove bottun was pressed"
        self.vBox.removeWidget(self.viewGroupBox)

    if addContainer_Bottun_Pressed:
        print "add bottun was pressed"
        self.vBox.addWidget(self.viewGroupBox)

程序检测“removeContainer_Bottun_Pressed”何时为 true,并运行removeWidget(self.newGroupBox)。尽管removeWidget运行,但groupBox仍保留在同一位置,而不是根据请求消失并重新出现。

这里缺少什么?

高度赞赏所有意见和建议。

I am developing a system for a customer which is displayed in a set of tabs, and shows a table in the centralwidget with data extracted from a database.

Depending on mouse events, the container (groupBox) must be removed from the centralwidget, or then added with new updated data for the table.

Here is a piece of the code that runs nicely and shows the table with data inside the GroupBox:

    self.tab_tableview = QtGui.QWidget()
    self.tab_tableview.setObjectName("tab_tableview")

    self.viewGroupBox = QtGui.QGroupBox(self.tab_tableview)
    self.viewGroupBox.setGeometry(QtCore.QRect(10, 0, 751, 501))
    self.viewGroupBox.setObjectName("viewGroupBox")

    self.vBox = QtGui.QVBoxLayout()
    self.vBox.addWidget(self.newGroupBox)
    self.vBox.setGeometry(QtCore.QRect(40, 170, 171, 111))
    self.vBox.addStretch(1)

    self.viewTableWidget = QtGui.QTableView(self.viewGroupBox)
    self.viewTableWidget.setGeometry(QtCore.QRect(10, 20, 731, 471))
    self.viewTableWidget.setObjectName("viewTableWidget")

    updatedTableModel=self.callShowTable() 
    self.viewTableWidget.setModel(updatedTableModel)

    self.viewTableWidget.setColumnWidth(0,30)
    self.viewTableWidget.setColumnWidth(1,550)

    self.viewTabWidget.addTab(self.tab_tableview, "")

    if removeContainer_Bottun_Pressed:
        print "remove bottun was pressed"
        self.vBox.removeWidget(self.viewGroupBox)

    if addContainer_Bottun_Pressed:
        print "add bottun was pressed"
        self.vBox.addWidget(self.viewGroupBox)

The program detects when "removeContainer_Bottun_Pressed" is true, and run the removeWidget(self.newGroupBox). Although removeWidget runs, the groupBox stays in the same place, instead of disappearing and reappearing on request.

What is missing here?

All comments and suggestions are highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

濫情▎り 2024-08-19 06:49:43

我认为调用removeWidget是没有必要的。尝试仅对要删除的内容调用 widget.deleteLater 。然后,当您想要将其添加回来时,重新创建它并使用layout.insertWidget将其放在正确的位置。那有用吗?

它在 Windows XP 上对我有用......

import sys

from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

widget = QtGui.QWidget()
widget_layout = QtGui.QHBoxLayout()
widget.setLayout(widget_layout)

def add_group_box():
    group_box = widget.group_box = QtGui.QGroupBox()
    group_layout = QtGui.QVBoxLayout()
    group_box.setLayout(group_layout)

    for i in range(2):
        group_layout.addWidget(QtGui.QRadioButton(str(i)))

    widget_layout.insertWidget(0, group_box)
add_group_box()

show_button = QtGui.QPushButton("show")
hide_button = QtGui.QPushButton("hide")
def on_show():
    if not widget.group_box:
        add_group_box()
def on_hide():
    if widget.group_box:
        widget.group_box.deleteLater()
        widget.group_box = None
show_button.connect(show_button, QtCore.SIGNAL("clicked()"), on_show)
hide_button.connect(hide_button, QtCore.SIGNAL("clicked()"), on_hide)    
widget_layout.addWidget(show_button)
widget_layout.addWidget(hide_button)

widget.show()

app.exec_()

I don't think calling removeWidget is necessary. Try just calling widget.deleteLater on whatever you want to delete. Then when you want to add it back, recreate it and use layout.insertWidget to put it in its proper place. Does that work?

It's working for me here on Windows XP...

import sys

from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

widget = QtGui.QWidget()
widget_layout = QtGui.QHBoxLayout()
widget.setLayout(widget_layout)

def add_group_box():
    group_box = widget.group_box = QtGui.QGroupBox()
    group_layout = QtGui.QVBoxLayout()
    group_box.setLayout(group_layout)

    for i in range(2):
        group_layout.addWidget(QtGui.QRadioButton(str(i)))

    widget_layout.insertWidget(0, group_box)
add_group_box()

show_button = QtGui.QPushButton("show")
hide_button = QtGui.QPushButton("hide")
def on_show():
    if not widget.group_box:
        add_group_box()
def on_hide():
    if widget.group_box:
        widget.group_box.deleteLater()
        widget.group_box = None
show_button.connect(show_button, QtCore.SIGNAL("clicked()"), on_show)
hide_button.connect(hide_button, QtCore.SIGNAL("clicked()"), on_hide)    
widget_layout.addWidget(show_button)
widget_layout.addWidget(hide_button)

widget.show()

app.exec_()
禾厶谷欠 2024-08-19 06:49:43
  1. 它似乎包含一个拼写错误:addContainer_Bottun_Pressed

    难道不是 addContainer_Botton_Pressed 吗?

  2. 在动态更改小部件后,您可能需要调用某种“重新布局”方法。您应该尝试在删除/添加子窗口小部件后调用此方法:self.vBox.adjustSize()

  1. It seems to contain a typo: addContainer_Bottun_Pressed

    Wouldn't it be addContainer_Botton_Pressed instead?

  2. You might need to call some kind of "relayout" method after changing the widgets on the fly. You should try to call this after removing/adding child widgets: self.vBox.adjustSize()

酷到爆炸 2024-08-19 06:49:43

首先感谢我在这里收到的所有意见。序列中的源代码可以正常工作——或者至少 80% 的工作完美。

  • 80% - 它的作用:radioButton 删除 groupBox;单选按钮打招呼; radioButton 说不错;

  • 20% - 它仍然不做什么:radioButton 添加 groupBox。

正如您在序列中看到的,调用了函数 addBox,但它在第二次运行时并未添加 groupBox。

这里是导入:

#//===========================================================//#
import os
import platform
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
#//===========================================================//#

这是 Ui_Addwidget 类..

#//===========================================================//#  
class Ui_Addwidget(object):
    def runbutton3(self):
        print "hello // radioButton3.isChecked : ", self.radioButton3.isChecked()
    def runButton4(self):
        print "nice // radioButton4.isChecked : ", self.radioButton4.isChecked() 
    def addBox(self):
        self.vLayout_wdg = QtGui.QWidget(self.centralwidget)
        self.vLayout_wdg.setGeometry(QtCore.QRect(40, 160, 171, 121))
        self.vLayout_wdg.setObjectName("vLayout_wdg")

        self.vLayoutBoxObj = QtGui.QHBoxLayout()
        self.vLayout_wdg.setLayout(self.vLayoutBoxObj)

        self.newGroupBox = self.vLayout_wdg.newGroupBox = QtGui.QGroupBox(self.vLayout_wdg)
        self.newGroupBox.setObjectName("newGroupBox")
        self.newGroupBox.setTitle(QtGui.QApplication.translate("MainWindow", "newGroupBox", None, QtGui.QApplication.UnicodeUTF8))

        self.groupLayoutBox = QtGui.QVBoxLayout()
        self.groupLayoutBox.setObjectName("groupLayoutBox")
        self.newGroupBox.setLayout(self.groupLayoutBox)

        self.radioButton3 = QtGui.QRadioButton()
        self.radioButton3.setGeometry(QtCore.QRect(30, 30, 101, 21))
        self.radioButton3.setObjectName("helloRadioButton")
        self.radioButton3.setText(QtGui.QApplication.translate("MainWindow", "say: Hello", None, QtGui.QApplication.UnicodeUTF8))

        self.radioButton4 = QtGui.QRadioButton()
        self.radioButton4.setGeometry(QtCore.QRect(30, 60, 111, 18))
        self.radioButton4.setObjectName("niceRadioButton")
        self.radioButton4.setText(QtGui.QApplication.translate("MainWindow", "say: Nice", None, QtGui.QApplication.UnicodeUTF8))

        self.groupLayoutBox.addWidget(self.radioButton3)
        self.groupLayoutBox.addWidget(self.radioButton4)

        self.vLayoutBoxObj.insertWidget(0, self.newGroupBox)
    def on_show(self):
        print "addBox // radioButton1.isChecked : ", self.radioButton1.isChecked()
        if not self.vLayout_wdg.newGroupBox:
            self.addBox()
    def on_hide(self):
        print "deleteBox // radioButton2.isChecked : ", self.radioButton2.isChecked()
        if self.vLayout_wdg.newGroupBox:
            self.vLayout_wdg.newGroupBox.deleteLater()
            self.vLayout_wdg.newGroupBox = None
    def connectEvent(self):
        QtCore.QObject.connect(self.radioButton1, QtCore.SIGNAL("toggled(bool)"),self.on_show)
        QtCore.QObject.connect(self.radioButton2, QtCore.SIGNAL("toggled(bool)"),self.on_hide)
        QtCore.QObject.connect(self.radioButton3, QtCore.SIGNAL("toggled(bool)"),self.runbutton3)
        QtCore.QObject.connect(self.radioButton4, QtCore.SIGNAL("toggled(bool)"),self.runButton4)
    def selectBox(self):
        self.selectGroupBox = QtGui.QGroupBox(self.centralwidget)
        self.selectGroupBox.setGeometry(QtCore.QRect(40, 20, 171, 111))
        self.selectGroupBox.setObjectName("selectGroupBox")
        self.selectGroupBox.setTitle(QtGui.QApplication.translate("MainWindow", "select", None, QtGui.QApplication.UnicodeUTF8))

        self.radioButton1 = QtGui.QRadioButton(self.selectGroupBox)
        self.radioButton1.setGeometry(QtCore.QRect(30, 30, 111, 18))
        self.radioButton1.setObjectName("radioButton1")
        self.radioButton1.setText(QtGui.QApplication.translate("MainWindow", "add groupbox", None, QtGui.QApplication.UnicodeUTF8))

        self.radioButton2 = QtGui.QRadioButton(self.selectGroupBox)
        self.radioButton2.setGeometry(QtCore.QRect(30, 60, 111, 18))
        self.radioButton2.setObjectName("radioButton2")
        self.radioButton2.setText(QtGui.QApplication.translate("MainWindow", "delete groupbox", None, QtGui.QApplication.UnicodeUTF8))  
    def addwidget_centralwdg(self,MainWindow):
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.selectBox()
        self.addBox()
        self.connectEvent()

        MainWindow.setCentralWidget(self.centralwidget)
    def addwidget_setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(250, 300)
        self.addwidget_centralwdg(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
#//===========================================================//#  

这里有 mainDesign 类...

#//===========================================================//#
class mainDesign(QtGui.QMainWindow,Ui_Addwidget):
    def __init__(self,parent=None):
        super(mainDesign,self).__init__(parent)
        self.addwidget_setupUi(self)
#//===========================================================//#  

当然还有 def main...

#//===========================================================//#        
def main():
    app = QtGui.QApplication(sys.argv)
    main = mainDesign()
    main.show()
    sys.exit(app.exec_())
main()
#//===========================================================//#

要尝试它,只需将代码和类复制到 *.py 文件。它会运行。

任何其他解决难题的意见或建议都非常受欢迎和赞赏。

First of all thanks for all the input I received here. In the sequence is the source code working -- or at least working 80% perfectly well.

  • 80% - What it does: radioButton to delete groupBox; radioButton to say Hello; radioButton to say Nice;

  • 20% - What it still doesn't do: radioButton to add groupBox.

As you can see in the sequence, the function addBox is called, but it doesn't add the groupBox for the second time it runs.

Here are the imports:

#//===========================================================//#
import os
import platform
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
#//===========================================================//#

Here is the Ui_Addwidget class..

#//===========================================================//#  
class Ui_Addwidget(object):
    def runbutton3(self):
        print "hello // radioButton3.isChecked : ", self.radioButton3.isChecked()
    def runButton4(self):
        print "nice // radioButton4.isChecked : ", self.radioButton4.isChecked() 
    def addBox(self):
        self.vLayout_wdg = QtGui.QWidget(self.centralwidget)
        self.vLayout_wdg.setGeometry(QtCore.QRect(40, 160, 171, 121))
        self.vLayout_wdg.setObjectName("vLayout_wdg")

        self.vLayoutBoxObj = QtGui.QHBoxLayout()
        self.vLayout_wdg.setLayout(self.vLayoutBoxObj)

        self.newGroupBox = self.vLayout_wdg.newGroupBox = QtGui.QGroupBox(self.vLayout_wdg)
        self.newGroupBox.setObjectName("newGroupBox")
        self.newGroupBox.setTitle(QtGui.QApplication.translate("MainWindow", "newGroupBox", None, QtGui.QApplication.UnicodeUTF8))

        self.groupLayoutBox = QtGui.QVBoxLayout()
        self.groupLayoutBox.setObjectName("groupLayoutBox")
        self.newGroupBox.setLayout(self.groupLayoutBox)

        self.radioButton3 = QtGui.QRadioButton()
        self.radioButton3.setGeometry(QtCore.QRect(30, 30, 101, 21))
        self.radioButton3.setObjectName("helloRadioButton")
        self.radioButton3.setText(QtGui.QApplication.translate("MainWindow", "say: Hello", None, QtGui.QApplication.UnicodeUTF8))

        self.radioButton4 = QtGui.QRadioButton()
        self.radioButton4.setGeometry(QtCore.QRect(30, 60, 111, 18))
        self.radioButton4.setObjectName("niceRadioButton")
        self.radioButton4.setText(QtGui.QApplication.translate("MainWindow", "say: Nice", None, QtGui.QApplication.UnicodeUTF8))

        self.groupLayoutBox.addWidget(self.radioButton3)
        self.groupLayoutBox.addWidget(self.radioButton4)

        self.vLayoutBoxObj.insertWidget(0, self.newGroupBox)
    def on_show(self):
        print "addBox // radioButton1.isChecked : ", self.radioButton1.isChecked()
        if not self.vLayout_wdg.newGroupBox:
            self.addBox()
    def on_hide(self):
        print "deleteBox // radioButton2.isChecked : ", self.radioButton2.isChecked()
        if self.vLayout_wdg.newGroupBox:
            self.vLayout_wdg.newGroupBox.deleteLater()
            self.vLayout_wdg.newGroupBox = None
    def connectEvent(self):
        QtCore.QObject.connect(self.radioButton1, QtCore.SIGNAL("toggled(bool)"),self.on_show)
        QtCore.QObject.connect(self.radioButton2, QtCore.SIGNAL("toggled(bool)"),self.on_hide)
        QtCore.QObject.connect(self.radioButton3, QtCore.SIGNAL("toggled(bool)"),self.runbutton3)
        QtCore.QObject.connect(self.radioButton4, QtCore.SIGNAL("toggled(bool)"),self.runButton4)
    def selectBox(self):
        self.selectGroupBox = QtGui.QGroupBox(self.centralwidget)
        self.selectGroupBox.setGeometry(QtCore.QRect(40, 20, 171, 111))
        self.selectGroupBox.setObjectName("selectGroupBox")
        self.selectGroupBox.setTitle(QtGui.QApplication.translate("MainWindow", "select", None, QtGui.QApplication.UnicodeUTF8))

        self.radioButton1 = QtGui.QRadioButton(self.selectGroupBox)
        self.radioButton1.setGeometry(QtCore.QRect(30, 30, 111, 18))
        self.radioButton1.setObjectName("radioButton1")
        self.radioButton1.setText(QtGui.QApplication.translate("MainWindow", "add groupbox", None, QtGui.QApplication.UnicodeUTF8))

        self.radioButton2 = QtGui.QRadioButton(self.selectGroupBox)
        self.radioButton2.setGeometry(QtCore.QRect(30, 60, 111, 18))
        self.radioButton2.setObjectName("radioButton2")
        self.radioButton2.setText(QtGui.QApplication.translate("MainWindow", "delete groupbox", None, QtGui.QApplication.UnicodeUTF8))  
    def addwidget_centralwdg(self,MainWindow):
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.selectBox()
        self.addBox()
        self.connectEvent()

        MainWindow.setCentralWidget(self.centralwidget)
    def addwidget_setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(250, 300)
        self.addwidget_centralwdg(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
#//===========================================================//#  

Here you have the mainDesign class...

#//===========================================================//#
class mainDesign(QtGui.QMainWindow,Ui_Addwidget):
    def __init__(self,parent=None):
        super(mainDesign,self).__init__(parent)
        self.addwidget_setupUi(self)
#//===========================================================//#  

And of course, the def main...

#//===========================================================//#        
def main():
    app = QtGui.QApplication(sys.argv)
    main = mainDesign()
    main.show()
    sys.exit(app.exec_())
main()
#//===========================================================//#

To try it, just copy the code and the classes to a *.py file. And it will run.

Any other comments or suggestions to solve the missing piece of the puzzle, are highly welcome and appreciated.

无所的.畏惧 2024-08-19 06:49:43

这是最终的解决方案,100% 正常工作:

  • 更新如下:

    def on_show(self):
       print "addBox // radioButton1.isChecked : ", self.radioButton1.isChecked()
       如果不是 self.centralwidget.newGroupBox:
           #self.addBox()
           self.addwidget_centralwdg(self.globalMainWindow)
    def addwidget_centralwdg(self,MainWindow):
       self.centralwidget = QtGui.QWidget(MainWindow)
       self.globalMainWindow=主窗口
       self.centralwidget.setObjectName("centralwidget")
       self.selectBox()
       self.addBox()
       self.connectEvent()
       MainWindow.setCentralWidget(self.centralwidget)
    

希望这也能帮助其他人。

Here is the final solution, working 100%:

  • update as follows:

    def on_show(self):
       print "addBox // radioButton1.isChecked : ", self.radioButton1.isChecked()
       if not self.centralwidget.newGroupBox:
           #self.addBox()
           self.addwidget_centralwdg(self.globalMainWindow)
    def addwidget_centralwdg(self,MainWindow):
       self.centralwidget = QtGui.QWidget(MainWindow)
       self.globalMainWindow=MainWindow
       self.centralwidget.setObjectName("centralwidget")
       self.selectBox()
       self.addBox()
       self.connectEvent()
       MainWindow.setCentralWidget(self.centralwidget)
    

Hope this can help others too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文