PyQt:removeChild/addChild QGroupBox
我正在为客户开发一个系统,该系统显示在一组选项卡中,并在中央小部件中显示一个表格,其中包含从数据库中提取的数据。
根据鼠标事件,必须从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为调用removeWidget是没有必要的。尝试仅对要删除的内容调用 widget.deleteLater 。然后,当您想要将其添加回来时,重新创建它并使用layout.insertWidget将其放在正确的位置。那有用吗?
它在 Windows XP 上对我有用......
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...
它似乎包含一个拼写错误:addContainer_Bottun_Pressed
难道不是 addContainer_Botton_Pressed 吗?
在动态更改小部件后,您可能需要调用某种“重新布局”方法。您应该尝试在删除/添加子窗口小部件后调用此方法:
self.vBox.adjustSize()
It seems to contain a typo: addContainer_Bottun_Pressed
Wouldn't it be addContainer_Botton_Pressed instead?
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()
首先感谢我在这里收到的所有意见。序列中的源代码可以正常工作——或者至少 80% 的工作完美。
80% - 它的作用:radioButton 删除 groupBox;单选按钮打招呼; radioButton 说不错;
20% - 它仍然不做什么:radioButton 添加 groupBox。
正如您在序列中看到的,调用了函数 addBox,但它在第二次运行时并未添加 groupBox。
这里是导入:
这是 Ui_Addwidget 类..
这里有 mainDesign 类...
当然还有 def 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:
Here is the Ui_Addwidget class..
Here you have the mainDesign class...
And of course, the def 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.
这是最终的解决方案,100% 正常工作:
更新如下:
希望这也能帮助其他人。
Here is the final solution, working 100%:
update as follows:
Hope this can help others too.