0

I'm building a plugin for QGIS using PyQGIS and PyQt5. I want to work with multiple windows. After clicking a button, a new window should open. Ideally, it should overlap window 1. After editing in window 2, it should go back to window 1.

My previous code is like this:

class MainWindow(QDialog):
    def __init__(self):
        super(MainWindow, self).__init__()
        loadUi(r"test1.ui", self)
        self.button.clicked.connect(self.gotoScreen2)

    def gotoScreen2(self):
        screen2=Screen2()
        widget.addWidget(screen2)
        widget.setCurrentIndex(widget.currentIndex()+1)
        
class Screen2(QDialog):
    def __init__(self):
        super(Screen2,self).__init__()
        loadUi(r"test2.ui", self)
        self.pushButton.clicked.connect(self.gotoScreen1)
        
    def gotoScreen1(self):
        mainwindow = MainWindow()
        widget.addWidget(mainwindow)
        widget.setCurrentIndex(widget.currentIndex()+1)
        
#main
app = QApplication(sys.argv)
widget = QtWidgets.QStackedWidget() 
mainwindow = MainWindow()
widget.addWidget(mainwindow)
widget.show()

Building on this, I would like the plugin window to dock on the right in the program. I know that it is possible with the QDockWidget class and addDockWidget (QtCore.Qt.RightDockWidgetArea,...). But how do I get these class built into my script?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
cc_schr_gis
  • 123
  • 3
  • Your question is a bit confusing. First of all, why are you adding a QMainWindow to a QStackedWidget? Then, such widget is intended to allow *reusing* of "pages" already added previously (similarly to what QTabWidget does, but without the tab bar). So, you should not add *another* window, you should display the previous one, either by using `setCurrentIndex()` or `setCurrentWidget()`. – musicamante Jun 23 '21 at 19:44

1 Answers1

0

This is my solution (there are now three UIs (windows) that communicate directly with each other.) It works wonderfully in QGIS.

class MainWindow(QDialog):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.gui = loadUi(r"test1.ui", self)
        self.button.clicked.connect(self.gotoScreen2)
        self.btn_adding.clicked.connect(self.adding)

    def gotoScreen2(self):
        screen2=Screen2()
        widget.setWidget(screen2)
        
class Screen2(QDialog):
    def __init__(self):
        super(Screen2,self).__init__()
        loadUi(r"test2.ui", self)
        self.pushButton.clicked.connect(self.gotoScreen1)
        self.pushButton_2.clicked.connect(self.gotoScreen3)
        
    def gotoScreen1(self):
        mainwindow = MainWindow()
        widget.setWidget(mainwindow)
        
    def gotoScreen3(self):
        screen3=Screen3()
        widget.setWidget(screen3)
        
class Screen3(QDialog):
    def __init__(self):
        super(Screen3,self).__init__()
        loadUi(r"test3.ui", self)
        self.pushButton.clicked.connect(self.gotoScreen2)
        
    def gotoScreen2(self):
        screen2=Screen2()
        widget.setWidget(screen2)


widget = QtWidgets.QDockWidget("test")
mainwindow = MainWindow()
widget.setWidget(mainwindow)
#dock on the right side of the screen
iface.addDockWidget(QtCore.Qt.RightDockWidgetArea, widget)
widget.show()
cc_schr_gis
  • 123
  • 3