PyQt:移除/添加 QGroupBox
我正在为一个客户开发一个系统,这个系统会以标签的形式展示内容,并在中央区域显示一个从数据库提取的数据表格。
根据鼠标的操作,容器(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”这个状态为真时,就会执行removeWidget(self.newGroupBox)这个操作。虽然removeWidget这个操作确实执行了,但groupBox却没有消失,而是停留在原地,没有按照要求消失和重新出现。
这里缺少了什么呢?
欢迎大家提出意见和建议。
4 个回答
看起来这里有个拼写错误:addContainer_Bottun_Pressed
应该是 addContainer_Botton_Pressed 吧?
在你动态更改控件后,可能需要调用某种“重新布局”的方法。你可以在添加或移除子控件后试着调用这个方法:
self.vBox.adjustSize()
首先,感谢大家在这里提供的意见。接下来是一个源代码,它的运行效果还不错——或者说大约有80%的效果。
80% - 它能做的事情:有一个单选按钮可以删除一个组框;一个单选按钮可以说“你好”;还有一个单选按钮可以说“很好”。
20% - 还不能做的事情:没有单选按钮可以添加组框。
从顺序上看,addBox这个功能被调用了,但它在第二次运行时并没有添加组框。
以下是需要导入的内容:
#//===========================================================//#
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文件中,就可以运行了。
如果有其他的评论或建议,帮助解决这个缺失的部分,非常欢迎和感谢。
我觉得调用 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_()