开发者

PyQt: removeChild/addChild QGroupBox

开发者 https://www.devze.com 2022-12-12 00:26 出处:网络
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.

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.setGeom开发者_StackOverflow社区etry(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.


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_()


  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()


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消