2

I've created an app which has an main window and the possibility to open an dialog (question, error and so on). I'm not using QMessageBox.warning() or QMessageBox.question() and so on because I wanted to customize the dialogs a bit.

But every time I open a new Dialog, in the Windows task bar (I'm working on Windows 10) a new 'tab' is opened, which is a little bit annoying.

enter image description here

My code (shortened):

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_())

Note: Normally, the dialog is called from an menu or button.

Question: How can I make the dialog appear in the main window without creating a new 'task bar tab'?

linusg
  • 6,289
  • 4
  • 28
  • 78
  • see this http://stackoverflow.com/a/9043996/4941927, could help you – Milor123 Mar 31 '16 at 17:49
  • @Milor123 Thanks, I'm doing it like this already. The button is connected using `self.mybutton.clicked.connect(self.myfunc)`. In `myfunc`, the dialog is called using `MessageBox("Title", "My message here")`. Any ideas? – linusg Mar 31 '16 at 17:54
  • Besides the fact that it doesn't make much sense to create a class and just do all the job in the constructor (either you inherit to extend the class, or you just create a function?), you should set correctly the parent of the new dialog to the main dialog to have the focus and the window grouping working correctly. – Matteo Italia Mar 31 '16 at 17:58
  • @MatteoItalia As I wrote, in my real code the dialog is called from another function. I tried setting the parent some time ago, but that doesn't worked. Can you tell me how to set the parent of a `QDialog`? – linusg Mar 31 '16 at 18:02

2 Answers2

2

The solution was quite simple: Passing an reference of QMainWindow to the constructor of QDialog will do the job, e.g:

class MessageBox(QtGui.QDialog):
    def __init__(self, parent, title, message, icon="info"):
        super(MessageBox, self).__init__(parent)
        ...

and then calling the dialog from an class that inherits from 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)

Maybe this helps anyone!

linusg
  • 6,289
  • 4
  • 28
  • 78
-1

If you set the parent of the QDialog to the window, it will only show as one item on the task bar. This is generally the first argument to QMessageBox.

class MessageBox:
    def __init__(self, parent, title, message):
        msg = QtGui.QMessageBox(parent)

Also, if you really want to create a custom dialog, you might as well just subclass from QDialog.

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118