我正在为一个客户开发一个系统,该系统显示在一组选项卡中,并在中央小部件中显示从数据库中提取数据的表。
根据鼠标事件的不同,容器(groupBox)必须从centralwidget中删除,或者添加新的表更新数据。
下面是一段运行良好的代码,它显示了在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会留在相同的位置,而不是在请求时消失和重新出现。
这里缺少什么?
所有的意见和建议都是非常感谢的。
发布于 2009-11-24 06:35:53
我认为没有必要调用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_()发布于 2009-11-23 13:03:26
难道不是addContainer_Botton_Pressed吗?
在动态更改小部件之后,您可能需要调用某种“
self.vBox.adjustSize()发布于 2009-11-24 11:43:31
首先,感谢我在这里得到的所有意见。序列中的源代码正在工作--或者说至少有80%工作得很好。
正如您在序列中看到的,函数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文件中。然后它就会运行。
任何其他的意见或建议,以解决缺失的一块拼图,是高度欢迎和感谢。
https://stackoverflow.com/questions/1781173
复制相似问题