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. PyQt5 - How to put two QWidgets side by side in the screen?

PyQt5 - How to put two QWidgets side by side in the screen?

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.6k 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.
  • PatitotectiveP Offline
    PatitotectiveP Offline
    Patitotective
    wrote on last edited by
    #1

    I have two windows (QWidgets) and i want them to be resized and positioned side by side.
    Like this:
    Screenshot from 2021-10-11 18-23-14.png

    eyllanescE 1 Reply Last reply
    0
    • PatitotectiveP Offline
      PatitotectiveP Offline
      Patitotective
      wrote on last edited by Patitotective
      #4

      This seems to be working:

      ...
      screen_size = QGuiApplication.primaryScreen().size()
      
      self.parent.move(0, 0)
      self.parent.resize(screen_size.width() // 2, screen_size.height())
      
      second_widget.move(screen_size.width() // 2, 0)
      second_widget.resize(screen_size.width() // 2, screen_size.height())	
      

      Where self is the main QWidget, self.parent the QMainWindow and the second_widget another QWidget.


      For anyone who is interested, here is a minimal example:

      import sys
      
      from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication, QPushButton, QVBoxLayout
      from PyQt5.QtGui import QGuiApplication
      
      class MainWindow(QMainWindow):
      	def __init__(self, *args, **kwargs):
      		super().__init__(*args, **kwargs)
      
      		self.main_widget = MainWidget(self)
      		self.setCentralWidget(self.main_widget)
      
      		self.show()
      
      class MainWidget(QWidget):
      	def __init__(self, parent=None):
      		super().__init__(parent=parent)
      
      		self.parent = parent
      		
      		self.setWindowTitle("Main widget")
      		self.setLayout(QVBoxLayout())
      		
      		side_by_side_button = QPushButton("Make windows side by side")
      		side_by_side_button.clicked.connect(self.make_window_side_by_side)
      
      		self.layout().addWidget(side_by_side_button)
      
      		self.second_window = QWidget()
      		self.second_window.setWindowTitle("Second window")
      		self.second_window.setStyleSheet("background-color: #1F75A6;") # To difference between windows
      
      		self.second_window.show()
      
      	def make_window_side_by_side(self):
      		screen_size = QGuiApplication.primaryScreen().size() # Get screen size
      
      		self.parent.showNormal() # Unmaximize and unminimize
      		self.parent.move(0, 0) # Move to the top left corner
      		self.parent.resize(screen_size.width() // 2, screen_size.height()) # Set the width to the half of the screen width and the height to the screen height
      
      		self.second_window.showNormal() # Unmaximize and unminimize
      		self.second_window.move(screen_size.width() // 2, 0) # Move X to the middle of the screen and Y to the top
      		self.second_window.resize(screen_size.width() // 2, screen_size.height()) # Set the width to the half of the screen width and the height to the screen height
      
      
      if __name__ == "__main__":
      	app = QApplication(sys.argv)
      
      	main_window = MainWindow()
      
      	sys.exit(app.exec_())
      
      eyllanescE 1 Reply Last reply
      0
      • PatitotectiveP Patitotective

        I have two windows (QWidgets) and i want them to be resized and positioned side by side.
        Like this:
        Screenshot from 2021-10-11 18-23-14.png

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #2

        @Patitotective Why don't you use a QHBoxLayout to place both QWidget inside a new window?

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        PatitotectiveP 1 Reply Last reply
        1
        • eyllanescE eyllanesc

          @Patitotective Why don't you use a QHBoxLayout to place both QWidget inside a new window?

          PatitotectiveP Offline
          PatitotectiveP Offline
          Patitotective
          wrote on last edited by
          #3

          @eyllanesc I want the user to be able to manipulate the second window as he wants. I'm asking how to position two windows side by side.

          1 Reply Last reply
          0
          • PatitotectiveP Offline
            PatitotectiveP Offline
            Patitotective
            wrote on last edited by Patitotective
            #4

            This seems to be working:

            ...
            screen_size = QGuiApplication.primaryScreen().size()
            
            self.parent.move(0, 0)
            self.parent.resize(screen_size.width() // 2, screen_size.height())
            
            second_widget.move(screen_size.width() // 2, 0)
            second_widget.resize(screen_size.width() // 2, screen_size.height())	
            

            Where self is the main QWidget, self.parent the QMainWindow and the second_widget another QWidget.


            For anyone who is interested, here is a minimal example:

            import sys
            
            from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication, QPushButton, QVBoxLayout
            from PyQt5.QtGui import QGuiApplication
            
            class MainWindow(QMainWindow):
            	def __init__(self, *args, **kwargs):
            		super().__init__(*args, **kwargs)
            
            		self.main_widget = MainWidget(self)
            		self.setCentralWidget(self.main_widget)
            
            		self.show()
            
            class MainWidget(QWidget):
            	def __init__(self, parent=None):
            		super().__init__(parent=parent)
            
            		self.parent = parent
            		
            		self.setWindowTitle("Main widget")
            		self.setLayout(QVBoxLayout())
            		
            		side_by_side_button = QPushButton("Make windows side by side")
            		side_by_side_button.clicked.connect(self.make_window_side_by_side)
            
            		self.layout().addWidget(side_by_side_button)
            
            		self.second_window = QWidget()
            		self.second_window.setWindowTitle("Second window")
            		self.second_window.setStyleSheet("background-color: #1F75A6;") # To difference between windows
            
            		self.second_window.show()
            
            	def make_window_side_by_side(self):
            		screen_size = QGuiApplication.primaryScreen().size() # Get screen size
            
            		self.parent.showNormal() # Unmaximize and unminimize
            		self.parent.move(0, 0) # Move to the top left corner
            		self.parent.resize(screen_size.width() // 2, screen_size.height()) # Set the width to the half of the screen width and the height to the screen height
            
            		self.second_window.showNormal() # Unmaximize and unminimize
            		self.second_window.move(screen_size.width() // 2, 0) # Move X to the middle of the screen and Y to the top
            		self.second_window.resize(screen_size.width() // 2, screen_size.height()) # Set the width to the half of the screen width and the height to the screen height
            
            
            if __name__ == "__main__":
            	app = QApplication(sys.argv)
            
            	main_window = MainWindow()
            
            	sys.exit(app.exec_())
            
            eyllanescE 1 Reply Last reply
            0
            • PatitotectiveP Patitotective

              This seems to be working:

              ...
              screen_size = QGuiApplication.primaryScreen().size()
              
              self.parent.move(0, 0)
              self.parent.resize(screen_size.width() // 2, screen_size.height())
              
              second_widget.move(screen_size.width() // 2, 0)
              second_widget.resize(screen_size.width() // 2, screen_size.height())	
              

              Where self is the main QWidget, self.parent the QMainWindow and the second_widget another QWidget.


              For anyone who is interested, here is a minimal example:

              import sys
              
              from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication, QPushButton, QVBoxLayout
              from PyQt5.QtGui import QGuiApplication
              
              class MainWindow(QMainWindow):
              	def __init__(self, *args, **kwargs):
              		super().__init__(*args, **kwargs)
              
              		self.main_widget = MainWidget(self)
              		self.setCentralWidget(self.main_widget)
              
              		self.show()
              
              class MainWidget(QWidget):
              	def __init__(self, parent=None):
              		super().__init__(parent=parent)
              
              		self.parent = parent
              		
              		self.setWindowTitle("Main widget")
              		self.setLayout(QVBoxLayout())
              		
              		side_by_side_button = QPushButton("Make windows side by side")
              		side_by_side_button.clicked.connect(self.make_window_side_by_side)
              
              		self.layout().addWidget(side_by_side_button)
              
              		self.second_window = QWidget()
              		self.second_window.setWindowTitle("Second window")
              		self.second_window.setStyleSheet("background-color: #1F75A6;") # To difference between windows
              
              		self.second_window.show()
              
              	def make_window_side_by_side(self):
              		screen_size = QGuiApplication.primaryScreen().size() # Get screen size
              
              		self.parent.showNormal() # Unmaximize and unminimize
              		self.parent.move(0, 0) # Move to the top left corner
              		self.parent.resize(screen_size.width() // 2, screen_size.height()) # Set the width to the half of the screen width and the height to the screen height
              
              		self.second_window.showNormal() # Unmaximize and unminimize
              		self.second_window.move(screen_size.width() // 2, 0) # Move X to the middle of the screen and Y to the top
              		self.second_window.resize(screen_size.width() // 2, screen_size.height()) # Set the width to the half of the screen width and the height to the screen height
              
              
              if __name__ == "__main__":
              	app = QApplication(sys.argv)
              
              	main_window = MainWindow()
              
              	sys.exit(app.exec_())
              
              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by
              #5

              @Patitotective I think I misunderstood what you want. On the other hand don't use QDesktopWidget which is deprecated, instead use QScreen:

              screen_size = QGuiApplication.primaryScreen().size()
              

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              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