Search code examples
pythonpython-3.xpyqtpyqt4

Create Qt windows based on number of list items


first of all, this is my code:

class Fillscreen(QtGui.QWidget, Ui_View):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent,)
        self.setupUi(self)

    def full(self):
        self.showMaximized()

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)

    monitors = selector.getMonitors()
    resolutions = selector.getResolution(monitors)
    for monitor in monitors:
        window = Fillscreen()
        window.move(monitor[0],monitor[1])
        window.full()
    app.exec_()

monitors looks like this: [(-1280, 0, 0, 1024), (0, 0, 1920, 1080), (1920, 0, 3840, 1080)]

As is it only creates one window, which absolutely makes sense since im overwriting window on each loop. To solve this i could manually create a window1, window2 and window3

But how would i do this when i can't be sure of the Number of Monitors?


Solution

  • Use QApplication::desktop():

    ...
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
        ws = []
    
        for i in range(QtGui.QApplication.desktop().screenCount()):
            topLeft = QtGui.QApplication.desktop().screenGeometry(i).topLeft()
            window = Fillscreen()
            window.move(topLeft)
            window.full()
            ws.append(window)
        sys.exit(app.exec_())