PySide 使 QDialog 出现在 main window
PySide make QDialog appear in main window
我创建了一个应用程序,它有一个主要的 window 并且可以打开一个对话框(问题、错误等等)。我没有使用 QMessageBox.warning()
或 QMessageBox.question()
等,因为我想稍微自定义一下对话框。
但是每次我打开一个新的Dialog,在Windows任务栏(我在Windows10)打开一个新的'tab',有点小有点烦人。
我的代码(缩写):
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_())
注意:通常,对话框是从菜单或按钮调用的。
问题:如何在不创建新的'task bar tab'的情况下使对话框出现在主window中?
如果您将 QDialog
的父级设置为 window,它只会在任务栏上显示为一个项目。这通常是 QMessageBox
.
的第一个参数
class MessageBox:
def __init__(self, parent, title, message):
msg = QtGui.QMessageBox(parent)
另外,如果你真的想创建一个自定义对话框,你也可以从 QDialog
.
继承
解决方案非常简单:将 QMainWindow
的引用传递给 QDialog
的构造函数即可,例如:
class MessageBox(QtGui.QDialog):
def __init__(self, parent, title, message, icon="info"):
super(MessageBox, self).__init__(parent)
...
然后从继承自 QMainWindow
的 class 调用对话框:
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)
也许这对任何人都有帮助!
我创建了一个应用程序,它有一个主要的 window 并且可以打开一个对话框(问题、错误等等)。我没有使用 QMessageBox.warning()
或 QMessageBox.question()
等,因为我想稍微自定义一下对话框。
但是每次我打开一个新的Dialog,在Windows任务栏(我在Windows10)打开一个新的'tab',有点小有点烦人。
我的代码(缩写):
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_())
注意:通常,对话框是从菜单或按钮调用的。
问题:如何在不创建新的'task bar tab'的情况下使对话框出现在主window中?
如果您将 QDialog
的父级设置为 window,它只会在任务栏上显示为一个项目。这通常是 QMessageBox
.
class MessageBox:
def __init__(self, parent, title, message):
msg = QtGui.QMessageBox(parent)
另外,如果你真的想创建一个自定义对话框,你也可以从 QDialog
.
解决方案非常简单:将 QMainWindow
的引用传递给 QDialog
的构造函数即可,例如:
class MessageBox(QtGui.QDialog):
def __init__(self, parent, title, message, icon="info"):
super(MessageBox, self).__init__(parent)
...
然后从继承自 QMainWindow
的 class 调用对话框:
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)
也许这对任何人都有帮助!