change the image the index of a stakedWidget using a QcomboBox

3

Good evening I have the following code:

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic
from PyQt5.QtCore import Qt
from tabla import tabla_style

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("new_exe.ui",self)

        self.setWindowFlags(Qt.FramelessWindowHint)
        self.b_close.clicked.connect(lambda:self.close())
        self.b_min.clicked.connect(lambda:self.showMinimized())
        if self.combo.currentIndex() == 0:
            self.stackedWidget.setCurrentIndex(0)
        elif self.combo.currentIndex()== 1:
            self.stackedWidget.setCurrentIndex(1)




app = QApplication([])
p = Principal()
p.show()
app.exec_()

and what I'm trying to do is that when I change the value of qcombobox I change the index of the stakedwidget however it does not work Any suggestions?

    
asked by Mystic_Force 02.09.2018 в 04:30
source

1 answer

1

You must use a signal to make the change since your verification is only done in the constructor for once.

Assuming that the QComboBox has the same number of items as the QStackedWidget pages, it is only necessary to connect the signal currentIndexChanged :

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic
from PyQt5.QtCore import Qt


class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self, flags=Qt.FramelessWindowHint)
        uic.loadUi("new_exe.ui",self)

        self.b_close.clicked.connect(self.close)
        self.b_min.clicked.connect(self.showMinimized)
        self.combo.currentIndexChanged.connect(self.stackedWidget.setCurrentIndex)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    p = Principal()
    p.show()
    sys.exit(app.exec_())

Note: Do not abuse lambda functions, in the case of PyQt it is better to make a direct connection since it saves resources and increases the speed.

    
answered by 02.09.2018 / 05:19
source