2

I'm using PyQt4 with python 2.7 on Windows7

I have a QWidget that I want to stay above the QMainWindow while clicking on the main. The idea is that the main will contain a series of 'edit' buttons that will open the edit widget, while the edit widget refreshes with info contained on the main. I don't particularly care if either is "always on top" as long as the widget stays in front of the main window.

There are a couple of questions that address this topic, but I'm not seeing an answer that works for my specific use case. One deals in widgets but provides an answer only for the app main window (widget stays above other environmental windows but falls behind the application main window when clicking on the main), and the other addresses Qt generally but not a pythonic example:

PyQt: Always on top

How keep a QWidget always on top?

Here is the code I have so far:

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        self.setWindowTitle("MainWindow")
        self.resize(400, 300)

class Ui_Widget(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        self.setWindowTitle("Widget")
        self.resize(400, 300)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = Ui_MainWindow()
    MainWindow.show()
    Widget = Ui_Widget()
    Widget.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tiercen
  • 45
  • 1
  • 7
  • From what I understand you You want Ui_Widget to always be on top of Ui_MainWindow, and for example another window like Google Chrome can be on top of both. – eyllanesc Jun 16 '18 at 22:54
  • @eyllanesc Correct, I don't specifically need Chrome to be in front or behind the main window as long as the widget is in front of the main window. Your answer below meets that perfectly. Thanks again! – tiercen Jun 17 '18 at 01:14

1 Answers1

3

If you want Ui_Widget to always be on top of Ui_MainWindow, Ui_Widget must be a child of Ui_MainWindow and the flag Qt::Dialog must be activated as shown below:

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle("MainWindow")
        self.resize(400, 300)

class Ui_Widget(QtGui.QWidget):
    def __init__(self,  parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.Dialog)
        self.setWindowTitle("Widget")
        self.resize(400, 300)
        self.move(200, 150)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = Ui_MainWindow()
    MainWindow.show()
    Widget = Ui_Widget(MainWindow)
    Widget.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Awesome, thanks! I tried setting the child-parent relationship earlier and didn't the result I was looking for, so this explains why. Can you provide some background explanation for why the Qt::Dialog flag has to be set, or point me to where I should look? – tiercen Jun 17 '18 at 01:11
  • Great, much appreciated! – tiercen Jun 17 '18 at 06:48