How to make a sub window stays on top of a QStackedWidget?
-
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()) -
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())@IvanIsLearning
If you are saying that even withself.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? -
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.
-
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.
@IvanIsLearning
You seem to have done what I was suggesting. I don't know anything about theQt.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
QStackedWidgetto see whether the latter is to do with the issue or it just does not work when you change windows anyway? -
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.
-