Qt:How to create a Window that does not minimize and do not block background GUI

StackOverflow https://stackoverflow.com/questions/15933980

  •  03-04-2022
  •  | 
  •  

سؤال

I have a QMainWindow that is a child to another window. When the user clicks anywhere in the parent window, I do not want the child window to be minimized. The child window should lose the focus and user should be able to continue working on the parent window.

This functionality is similar to the find/replace dialogs found in libreoffice/excel/openoffice etc as shown below. We can see that the task-bar shows only the parent application window and the child window is not visible in task-bar.

enter image description here

Are there any signals on QMainWindow that could help me achieve this? Or what would be the best way to do this?

هل كانت مفيدة؟

المحلول

If you open the new window as Dialog and give it a parent, it should stay on top of the parent. Since you're using QMainWindow, you can pass this with the constructor. If you decide to use QDialog, make sure you make it modeless with setModal(False). Otherwise it will block the parent.

A small example:

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        w = QtGui.QWidget()
        layout = QtGui.QVBoxLayout(w)
        self.button = QtGui.QPushButton('Open Dialog')
        self.text = QtGui.QTextEdit()

        layout.addWidget(self.button)
        layout.addWidget(self.text)

        self.setCentralWidget(w)

        self.button.clicked.connect(self.openDialog)

    def openDialog(self):
        self.dialog = QtGui.QMainWindow(self, QtCore.Qt.Dialog)
        self.dialog.show()

app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top