我创建了一个应用程序,它有一个主窗口并且可以打开一个对话框(问题、错误等)。我没有使用 QMessageBox.warning()
或 QMessageBox.question()
等,因为我想稍微自定义一下对话框。
但是每次我打开一个新对话框时,Windows 任务栏中(我使用的是 Windows 10)都会打开一个新的“选项卡”,这有点烦人。
我的代码(缩短):
from PySide import QtCore, QtGui
import sys
class MessageBox:
def __init__(self, title, message):
msg = QtGui.QMessageBox()
flags = QtCore.Qt.Dialog
flags |= QtCore.Qt.CustomizeWindowHint
flags |= QtCore.Qt.WindowTitleHint
msg.setWindowFlags(flags)
msg.setWindowTitle(title)
msg.setText(message)
msg.exec_()
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.show()
MessageBox("Title", "My message here")
if __name__ == "__main__":
app = QtGui.QApplication([])
window = MainWindow()
sys.exit(app.exec_())
注意:通常,该对话框是从菜单或按钮调用的。
问题:如何使对话框出现在主窗口中而不创建新的“任务栏选项卡”?
最佳答案
解决方案非常简单:将QMainWindow
的引用传递给QDialog
的构造函数即可完成工作,例如:
class MessageBox(QtGui.QDialog):
def __init__(self, parent, title, message, icon="info"):
super(MessageBox, self).__init__(parent)
...
然后从继承自QMainWindow
的类调用对话框:
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
#connect button with function, e.g.:
mybutton.clicked.connect(self.open_dialog)
def open_dialog(self):
MessageBox(self)
也许这对任何人都有帮助!
关于python - PySide 使 QDialog 出现在主窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340134/