0

So, I am pretty new on PyQt5 and I am facing the following problem:

  • I have a window showing some information and buttons:

class HomeWindow(QDialog):

def __init__(self):
    super(HomeWindow, self).__init__()
    loadUi('homeScreen.ui', self)

    self.newItemButton.clicked.connect(self.newItem)
    self.flightLogButton.clicked.connect(self.flightLog)
    self.maintenanceLogButton.clicked.connect(self.maintenanceLog)

def newItem(self):
    newItem = NewItemWindow()
    widget.addWidget(newItem)
    widget.setCurrentIndex(widget.currentIndex()+1)

def flightLog(self):
    flightLog = FlightLogWindow()
    widget.addWidget(flightLog)
    widget.setCurrentIndex(widget.currentIndex()+1)

def maintenanceLog(self):
    maintenanceLog = MaintenanceLogWindow()
    widget.addWidget(maintenanceLog)
    widget.setCurrentIndex(widget.currentIndex()+1)

When I click on the button it will take me to the window I want, for example here newItem:

class NewItemWindow(QDialog):

def __init__(self):
    super(NewItemWindow, self).__init__()
    loadUi('addItem.ui', self)

    self.backButton.clicked.connect(self.back)

def back(self):
    widget.setCurrentIndex(widget.currentIndex()-1)

It works fine and it takes me to the window I want, when I click backButton, it takes me to the initial window. Now the problem is that if I click again in a different button, for example flighLogButton, it will take me to the window linked to the button that I first clicked, in this case newItem.

So my question is: Am I missing something here or is it not possible to work this way with PyQt5?

  • You should use [`setCurrentWidget()`](https://doc.qt.io/qt-5/qstackedwidget.html#setCurrentWidget) with the widget as argument instead of index. Be aware that you shouldn't constantly add new widgets, but instead reuse them. There are also other issues, and I can recognize that they are *all* caused by some "Hala" tutorial you're following from YouTube, which is known to provide ***a lot*** of **terrible** suggestions and bad practices. You should really ignore it. – musicamante Apr 22 '22 at 06:35
  • Thank you for your reply. Yes you are right I have followed a tutorial from YouTube as I had trouble using the documentation. Actually not only one but several "Halas" say use the same method. Could you be so kind and point the other issues on the code? Thanks – Thomas Cordeiro Apr 22 '22 at 12:56
  • The problem is not in using `setCurrentIndex()`, but the reason for and the way of using it. As said, QStackedWidget is intended to *reuse* widgets. Simply put: the first widget has index 0, then you add a widget, which will have index 1, and doing `setCurrentIndex(currentIndex() + 1)` will work because 0 + 1 is 1; then you go *back* with `currentIndex()-1` and it will be again index 0, but when you add a *new* widget, which will have index 2, and go again to `currentIndex() + 1`, since `currentIndex()` is again 0, going to 1 will switch to the *previously* added widget, not the new one. – musicamante Apr 24 '22 at 22:47
  • There are many other issues in your code (but those are not your fault, again it's due to that terrible tutorial), most of them are already listed in the duplicate answer, but there are also others I didn't list there. I also tried to contact the channel owner but they ignored me, and they obviously deleted my comments where I pointed out most of the (many, many) issues. I know other people are following similar practices (and usually it's because they followed that tutorial on their own), but that doesn't change the point: just ignore it, it's just a **bad** tutorial. – musicamante Apr 24 '22 at 22:55

0 Answers0