【发布时间】:2016-03-31 17:40:07
【问题描述】:
我创建了一个应用程序,它有一个主窗口并且可以打开一个对话框(问题、错误等)。我没有使用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_())
注意:通常,对话框是从菜单或按钮调用的。
问题:如何在不创建新的“任务栏标签”的情况下使对话框出现在主窗口中?
【问题讨论】:
-
看到这个stackoverflow.com/a/9043996/4941927,可以帮助你
-
@Milor123 谢谢,我已经这样做了。该按钮使用
self.mybutton.clicked.connect(self.myfunc)连接。在myfunc中,使用MessageBox("Title", "My message here")调用对话框。有什么想法吗? -
除了创建一个类并在构造函数中完成所有工作没有多大意义之外(要么继承以扩展类,还是只创建一个函数?),您应该将新对话框的父级正确设置为主对话框,以使焦点和窗口分组正常工作。
-
@MatteoItalia 正如我所写,在我的真实代码中,对话框是从另一个函数调用的。我前段时间尝试设置父级,但这不起作用。你能告诉我如何设置
QDialog的父级吗?