Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Common buttons and positioning in Python PyQt5 QStackedLayout
QtWS25 Last Chance

Common buttons and positioning in Python PyQt5 QStackedLayout

Scheduled Pinned Locked Moved Solved General and Desktop
pyqt5python3
2 Posts 2 Posters 2.8k 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.
  • B Offline
    B Offline
    benjibenjibenji
    wrote on 5 May 2019, 17:30 last edited by
    #1

    I am creating a Python PyQt5 app for a university project, which uses QStackedLayout to keep the app single-windowed.

    First question: how can I create buttons that appear on every window and have the same function and property without having to recreate them in every window's UI setup (like in the code)?

    Second question: after switching windows, how can I open the newly opened window at the previous's position? Right now they just open at the centre of the screen. I assume pos() or QPoint should be used but I can not figure out how.

    The code:

    import sys
    
    from PyQt5.QtWidgets import (QApplication,
                                 QMainWindow, 
                                 QPushButton,
                                 QWidget,
                                 QStackedLayout)
    
    class Ui(QWidget):
    
        def setupUi(self, Main):
    
            self.application_width  = 200
            self.application_height = 200
    
            self.stack = QStackedLayout()
    
            self.window_1 = QWidget()        
            self.window_2 = QWidget()
    
            self.window_1_UI()
            self.window_2_UI()
    
            self.stack.addWidget(self.window_1)
            self.stack.addWidget(self.window_2)
    
        def window_1_UI(self):
    
            self.window_1.setFixedSize(self.application_width, self.application_height)       
            self.window_1.setWindowTitle("1")
    
            '''REPLACE THIS BUTTON'''
            self.window_1_button = QPushButton("Change window", self.window_1)
    
        def window_2_UI(self):
    
            self.window_2.setFixedSize(self.application_width, self.application_height)
            self.window_2.setWindowTitle("2")
    
            '''AND REPLACE THIS BUTTON'''
            self.window_2_button = QPushButton("Change window", self.window_2)
    
    class Main(QMainWindow, Ui):
    
        def __init__(self):
    
            super(Main, self).__init__()
    
            self.setupUi(self)
    
            self.window_1_button.clicked.connect(self.change_window)
            self.window_2_button.clicked.connect(self.change_window)
    
        def change_window(self):
    
            if self.stack.currentIndex() == 0:
    
                self.stack.setCurrentIndex(1)
    
            else:
    
                self.stack.setCurrentIndex(0)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        M = Main()
        sys.exit(app.exec())
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 May 2019, 19:58 last edited by
      #2

      Hi,

      You can put your QStackedLayout and buttons in a QVBoxLayout so you can re-use the buttons. Put that layout in a QWidget and set that widget as the central widget of your QMainWindow.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      2/2

      5 May 2019, 19:58

      • Login

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