Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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



  • 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_())
    


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



  • @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.



  • 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_())
    


  • @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()
    

Log in to reply