我正在为客户开发一个系统,该系统显示在一组选项卡中,并在centralwidget中显示一个表,其中包含从数据库中提取的数据。
根据鼠标事件,必须从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保持在同一个位置,而不是消失并在请求时重新出现。
这里缺少什么?
我们非常感谢所有意见和建议。
答案 0 :(得分:2)
我不认为调用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_()
答案 1 :(得分:1)
似乎包含拼写错误:addContainer_Bottun_Pressed
不是addContainer_Bott o n_Pressed而已?
在动态更改窗口小部件后,您可能需要调用某种“重新布局”方法。您应该在删除/添加子窗口小部件后尝试调用此窗口:self.vBox.adjustSize()
答案 2 :(得分:1)
首先感谢我在这里收到的所有意见。顺序是源代码工作 - 或至少完美地工作80%。
80% - 它的作用:radioButton删除groupBox; radioButton说你好; 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主...
#//===========================================================//#
def main():
app = QtGui.QApplication(sys.argv)
main = mainDesign()
main.show()
sys.exit(app.exec_())
main()
#//===========================================================//#
要尝试它,只需将代码和类复制到* .py文件即可。它会运行。
任何其他意见或建议,以解决这个难题的缺失部分,非常欢迎和赞赏。
答案 3 :(得分:0)
这是最终解决方案,100%工作:
更新如下:
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)
希望这也可以帮助其他人。