【发布时间】:2016-08-27 23:31:01
【问题描述】:
这个程序的目的是将tradeWindow显示为一个QWidget,然后在每次调用doStuff(通过按钮)时显示一个QDialog(如果有结果)。该代码第一次工作,但第二次我收到错误消息:
Traceback (most recent call last):
File "GUI.py", line 68, in doStuff
popup = Dialog((Qt.WindowSystemMenuHint | Qt.WindowTitleHint), popLayout)
File "GUI.py", line 47, in __init__
self.setLayout(popLayout)
RuntimeError: Internal C++ object (PySide.QtGui.QHBoxLayout) already deleted.
当我第一次关闭 QDialog 时,我的布局似乎被删除了。
将 popLayout = QHBoxLayout() 移动到 doStuff 的开头,我认为这会解决这个问题,而是给我这个错误:
Traceback (most recent call last):
File "GUI.py", line 69, in doStuff
popup = Dialog((Qt.WindowSystemMenuHint | Qt.WindowTitleHint), popLayout)
File "GUI.py", line 47, in __init__
self.setLayout(popLayout)
NameError: name 'popLayout' is not defined
这对我来说根本没有多大意义,因为它应该总是在被引用之前被定义?反正我找不到问题。我确信我的很多代码都可以改进,而且我对类等很陌生。
如果您对如何每次打开 QDialog 有任何比我目前正在尝试的更好的提示或其他有用的提示,请不要犹豫提及。 (尽量忽略蹩脚的命名约定,我会在未来解决这个问题。)
感谢您的帮助!
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PySide.QtCore import *
from PySide.QtGui import *
import webbrowser
class Window(QWidget):
def __init__(self, windowTitle, layout):
super().__init__()
self.resize(800,500)
self.setWindowTitle(windowTitle)
self.setLayout(layout)
class TextField(QTextEdit):
def __init__(self, tooltip, layout):
super().__init__()
self.setToolTip(tooltip)
layout.addWidget(self)
class Button(QPushButton):
def __init__(self, text, layout):
super().__init__()
self.setText(text)
layout.addWidget(self)
class Label(QLabel):
def __init__(self, text, layout):
super().__init__()
self.setText(text)
layout.addWidget(self)
class Table(QTableWidget):
def __init__(self, layout):
super().__init__()
self.cellDoubleClicked.connect(self.slotItemDoubleClicked)
layout.addWidget(self)
def slotItemDoubleClicked(self,row,col):
if col == 0 or col == 1:
webbrowser.open(self.item(row, 1).text())
class Dialog(QDialog):
def __init__(self, flags, layout):
super().__init__()
self.setWindowFlags(flags)
self.resize(800,500)
self.setLayout(popLayout)
#Layouts
mainLayout = QVBoxLayout()
subLayout = QHBoxLayout()
subLayout2 = QHBoxLayout()
mainLayout.addLayout(subLayout)
mainLayout.addLayout(subLayout2)
popLayout = QHBoxLayout()
#Main
tradeApp = QApplication(sys.argv)
textedit = TextField('bla',subLayout)
textedit2 = TextField('bla2',subLayout)
label = Label('Hover over input fields for instructions.', subLayout2)
button = Button('click me', subLayout2)
label2 = Label('Hover over input fields for instructions.', subLayout2)
def doStuff():
gameResults = {'doom' : '111232', 'quake' : '355324'}
if len(gameResults) > 0:
popup = Dialog((Qt.WindowSystemMenuHint | Qt.WindowTitleHint), popLayout)
table = Table(popLayout)
table.setRowCount(len(gameResults))
table.setColumnCount(2);
table.setHorizontalHeaderItem(0, QTableWidgetItem("Game"))
table.setHorizontalHeaderItem(1, QTableWidgetItem("URL"))
for index, game in enumerate(sorted(gameResults)):
table.setItem(index,0,QTableWidgetItem(game))
table.item(index,0).setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled )
table.setItem(index,1,QTableWidgetItem('http://store.steampowered.com/app/'+gameResults[game]+'/'))
table.item(index,1).setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled )
table.resizeColumnsToContents()
popup.exec_()
else:
msgBox = QMessageBox()
msgBox.setText("No results.")
msgBox.exec_()
button.clicked.connect(doStuff)
tradeWindow = Window('Tradefinder', mainLayout)
tradeWindow.show()
tradeApp.exec_()
【问题讨论】: