I have made an application in PyQt5. I want my application to resize each of the elements of QStackedWidget
the moment application is launched.
Currently, it resizes those elements(Pages herein) only when I visit those Pages one by one.
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Main(QtWidgets.QWidget):
def __init__(self, stacked_widget):
super(Main, self).__init__()
self.stacked_widget = stacked_widget
btn = QtWidgets.QPushButton('Click here!')
btn.clicked.connect(self.goto)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(btn)
self.setLayout(layout)
def goto(self):
self.stacked_widget.setCurrentIndex(1)
class Page(QtWidgets.QWidget):
def __init__(self, stacked_widget, idx):
super(Page, self).__init__()
self.stacked_widget = stacked_widget
self.idx = idx
menu = QtWidgets.QToolButton()
menu.setText('Select Page')
menu.setStyleSheet("background-color: rgb(191, 191, 191); padding: 1px;")
viewType = QtWidgets.QMenu()
group = QtWidgets.QActionGroup(viewType)
texts = ["Page 01", "Page 02", "Page 03"]
for text in texts:
action = QtWidgets.QAction(text, viewType)
viewType.addAction(action)
group.addAction(action)
group.setExclusive(True)
group.triggered.connect(self.onTriggered)
menu.setMenu(viewType)
menu.setPopupMode(QtWidgets.QToolButton.InstantPopup)
viewType.setStyle(QtWidgets.QStyleFactory.create("Cleanlooks"))
menu.setFixedWidth(90)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(menu)
self.setLayout(layout)
def event(self, e):
if e.type() in (QtCore.QEvent.Show, QtCore.QEvent.Resize):
print("Page: {}, size".format(self.idx), self.width(), self.height())
return QtWidgets.QWidget.event(self, e)
def onTriggered(self, action):
if action.text() == 'Page 01':
self.stacked_widget.setCurrentIndex(1)
elif action.text() == 'Page 02':
self.stacked_widget.setCurrentIndex(2)
elif action.text() == 'Page 03':
self.stacked_widget.setCurrentIndex(3)
class Window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.stacked_widget = QtWidgets.QStackedWidget()
layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.addWidget(self.stacked_widget)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
widget_1 = Main(self.stacked_widget)
widget_2 = Page(self.stacked_widget, 1)
widget_3 = Page(self.stacked_widget, 2)
widget_4 = Page(self.stacked_widget, 3)
wid_list = [widget_1, widget_2, widget_3, widget_4]
for wid in wid_list:
self.stacked_widget.addWidget(wid)
self.showMaximized()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
w = Window()
sys.exit(app.exec_())
The output should look like below the moment application is launched:
Page: 1, size 1920 986
Page: 1, size 1920 986
Page: 2, size 1920 986
Page: 2, size 1920 986
Page: 3, size 1920 986
Page: 3, size 1920 986
Edit 1:
I need this information at launch time because my actual project consists of video frames on these Pages
and I have drawn some points/zones on these frames using OpenCV
. In case I'm unable to resize them at launch time, they show a lot of offset at my Dashboard which is defined in Main
class (I have just placed a QPushBtton in this example).
Edit 2:
I tried a work-around to this by modifying my Main
class as below:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from threading import Timer
class Main(QtWidgets.QWidget):
def __init__(self, stacked_widget):
.........
.........
t = Timer(60.0, self.hello)
t.start()
def hello(self):
for i in range(1,7):
self.stacked_widget.setCurrentIndex(i)
self.stacked_widget.setCurrentIndex(0)
But then I get QObject::setParent: Cannot set parent, new parent is in a different thread
. Though, the application works in this case but I know it's not the correct approach and I'm afraid it may cause problem in future.