Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to make a sub window stays on top of a QStackedWidget?
Forum Updated to NodeBB v4.3 + New Features

How to make a sub window stays on top of a QStackedWidget?

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 989 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IvanIsLearning
    wrote on last edited by
    #1

    Hello,
    I have a QStackedWidget containing multiple "pages" of other widgets. I alse have another sub window on top of the QStackedWidget.
    (the sub window is with Qt.SubWindow | Qt.WindowStaysOnTopHint flags)

    The problem is:
    When I change the currentIndex of my QStackedWidget, the sub window goes behind the QStackedWidget.

    So, is there a way to make the sub window always on top?

    Below is a Demo:

    import sys
    from PySide6.QtWidgets import QApplication, QWidget, QLabel, QListWidget, QVBoxLayout, QStackedWidget, QComboBox
    from PySide6.QtCore import Qt
    
    class SubWindow(QLabel):
      def __init__(self, parent):
        super().__init__(parent)
        self.setWindowFlags(Qt.SubWindow | Qt.WindowStaysOnTopHint)
        self.setText('My subWindow')
        self.setStyleSheet(
          '''
          * {
            color: white;
            background: blue;
          }
          '''
        )
        
    class MyStackWidget(QWidget):
      def __init__(self):
        super().__init__()
        self.list1 = QListWidget()
        self.list1.addItem('List 1')
        self.list2 = QListWidget()
        self.list2.addItem('List 2')
    
        self.stack = QStackedWidget()
        self.stack.addWidget(self.list1)
        self.stack.addWidget(self.list2)
    
        self.indexBox = QComboBox()
        self.indexBox.addItems(['0', '1'])
        self.indexBox.currentIndexChanged.connect(self.stack.setCurrentIndex)
    
        self.subWindow = SubWindow(self.stack)
        self.subWindow.move(20, 20)
        self.subWindow.show()
    
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.stack)
        self.layout.addWidget(self.indexBox)
     
    if __name__ == '__main__':
      app = QApplication()
      widget = MyStackWidget()
      widget.show()
      sys.exit(app.exec())
    
    JonBJ 1 Reply Last reply
    0
    • I IvanIsLearning

      Hello,
      I have a QStackedWidget containing multiple "pages" of other widgets. I alse have another sub window on top of the QStackedWidget.
      (the sub window is with Qt.SubWindow | Qt.WindowStaysOnTopHint flags)

      The problem is:
      When I change the currentIndex of my QStackedWidget, the sub window goes behind the QStackedWidget.

      So, is there a way to make the sub window always on top?

      Below is a Demo:

      import sys
      from PySide6.QtWidgets import QApplication, QWidget, QLabel, QListWidget, QVBoxLayout, QStackedWidget, QComboBox
      from PySide6.QtCore import Qt
      
      class SubWindow(QLabel):
        def __init__(self, parent):
          super().__init__(parent)
          self.setWindowFlags(Qt.SubWindow | Qt.WindowStaysOnTopHint)
          self.setText('My subWindow')
          self.setStyleSheet(
            '''
            * {
              color: white;
              background: blue;
            }
            '''
          )
          
      class MyStackWidget(QWidget):
        def __init__(self):
          super().__init__()
          self.list1 = QListWidget()
          self.list1.addItem('List 1')
          self.list2 = QListWidget()
          self.list2.addItem('List 2')
      
          self.stack = QStackedWidget()
          self.stack.addWidget(self.list1)
          self.stack.addWidget(self.list2)
      
          self.indexBox = QComboBox()
          self.indexBox.addItems(['0', '1'])
          self.indexBox.currentIndexChanged.connect(self.stack.setCurrentIndex)
      
          self.subWindow = SubWindow(self.stack)
          self.subWindow.move(20, 20)
          self.subWindow.show()
      
          self.layout = QVBoxLayout(self)
          self.layout.addWidget(self.stack)
          self.layout.addWidget(self.indexBox)
       
      if __name__ == '__main__':
        app = QApplication()
        widget = MyStackWidget()
        widget.show()
        sys.exit(app.exec())
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @IvanIsLearning
      If you are saying that even with self.subWindow = SubWindow(self.stack) the subwindow does not stay on top of whichever widget the stack is showing (when it changes) --- I would not know if that is the case --- then I suspect you would need to connect a signal to the stack page changing and re-parent the subwindow to the actual page shown in the stack?

      1 Reply Last reply
      1
      • I Offline
        I Offline
        IvanIsLearning
        wrote on last edited by
        #3

        Thanks for the reply.
        Just tried adding :

        self.stackedWidgetList = [self.list1, self.list2]
        self.stack.currentChanged.connect(lambda index: self.subWindow.setParent(self.stackedWidgetList[index]))
        

        But still, the sub window goes behind.

        It is a very strange behavior from my perspective. I think I must be missing something very basic.

        JonBJ 1 Reply Last reply
        1
        • I IvanIsLearning

          Thanks for the reply.
          Just tried adding :

          self.stackedWidgetList = [self.list1, self.list2]
          self.stack.currentChanged.connect(lambda index: self.subWindow.setParent(self.stackedWidgetList[index]))
          

          But still, the sub window goes behind.

          It is a very strange behavior from my perspective. I think I must be missing something very basic.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @IvanIsLearning
          You seem to have done what I was suggesting. I don't know anything about the Qt.WindowStaysOnTopHint. Maybe that only works against the parent at the time the window was opened, I don't know.

          Are you able to start from test code which has a couple of windows to swap between but not a QStackedWidget to see whether the latter is to do with the issue or it just does not work when you change windows anyway?

          1 Reply Last reply
          1
          • I Offline
            I Offline
            IvanIsLearning
            wrote on last edited by
            #5

            Just tried doing this with two windows + the subWindow:

            • Both Window emit an "activated" signal when QEvent.WindowActivate event happen

            • The "activated" signal is connected to a function that sets the subWindow's parent to the signal emitter

            • subWindow flag is: Qt.SubWindow | Qt.WindowStaysOnTopHint

            Here is what I observe:

            • If the subWindow is without a parent when initialized, then the subWindow will not show. The signals from the two windows do set subWindow's parent, but the subWindow is not shown(is no where to be found)

            • If the subWindow has a parent when initialized, it will show on the parent. But when the signals reset its parent, it just disappear. Its parent is being reset by the signals, but it is no where to be found.

            So the problem is probably not about QStackedWidget but a general window and parent thing related to Qt.SubWindow flag.

            I think I will try the Qt.Tool flag instead of Qt.SubWindow for my program.

            Thank you for the reply, I will leave this unsolved in case there is a corrent way to use Qt.SubWindow flag in my situation.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved