Values grabbed through one class are not being passed to the inheriting class in PyQT5

Question:

I am building a GUI for a hand tracking app using PyQT5.

There are two windows: one ‘MainWindow’ which displays the camera view, and one called ‘Window 2’ which holds multiple checkboxes that correspond to actions that can be enabled/disabled while the program is running.

The code for the two classes is as follows:

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        loadUi("window1.ui", self)
        self.setWindowTitle("Tracker")
        self.display_width = 640
        self.display_height = 480
        # start the thread
        self.gesture.clicked.connect(self.gotowindow2)
        self.startbutton.clicked.connect(self.startvideo)

    def gotowindow2(self):
        widget.setCurrentIndex(widget.currentIndex() + 1)  # changes index by 1 to change page

    def startvideo(self):
        # Change label color to light blue
        self.startbutton.clicked.disconnect(self.startvideo)
        # Change button to stop
        self.startbutton.setText('Stop video')
        values = Window2()
        hello = values.returnvalues()
        print(hello)
        self.thread = VideoThread(1,1,0)
        self.thread.change_pixmap_signal.connect(self.update_image)
        # start the thread
        self.thread.start()

        self.startbutton.clicked.connect(self.thread.stop)  # Stop the video if button clicked
        self.startbutton.clicked.connect(self.stopvideo)

    def stopvideo(self):
        self.thread.change_pixmap_signal.disconnect()

        self.startbutton.setText('Start video')
        self.startbutton.clicked.disconnect(self.stopvideo)
        self.startbutton.clicked.disconnect(self.thread.stop)
        self.startbutton.clicked.connect(self.startvideo)



    def closeEvent(self, event):
        self.thread.stop()
        event.accept()

    @pyqtSlot(np.ndarray)
    def update_image(self, img):
        """Updates the image_label with a new opencv image"""
        qt_img = self.convert_cv_qt(img)
        self.image_label.setPixmap(qt_img)


    def convert_cv_qt(self, img):
        """Convert from an opencv image to QPixmap"""
        rgb_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        h, w, ch = rgb_image.shape
        bytes_per_line = ch * w
        convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
        p = convert_to_Qt_format.scaled(self.display_width, self.display_height, Qt.KeepAspectRatio)
        return QPixmap.fromImage(p)


class Window2(QWidget):
    def __init__(self):
        super(Window2, self).__init__()
        loadUi("window2.ui", self)
        self.backbutton.clicked.connect(self.returnvalues)
        self.backbutton.clicked.connect(self.gotowindow1)
        self.outputbutton.clicked.connect(self.printvalues)


    def printvalues(self):
        print(self.returnvalues())


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

    def returnvalues(self):
        left = self.leftclickbutton.isChecked()
        scrollup = self.scrollupbutton.isChecked()
        scrolldown = self.scrolldownbutton.isChecked()
        checkboxvalues = [left, scrollup, scrolldown]
        return checkboxvalues





if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = QtWidgets.QStackedWidget()
    a = MainWindow()
    b = Window2()
    widget.resize(1000, 600)
    widget.addWidget(a)
    widget.addWidget(b)
    widget.show()
    sys.exit(app.exec_())

My problem is that when I check the checkboxes and press the "outputbutton", it correctly displays the three values.

When these values are fetched by hello = values.returnvalues(), it is always [False, False, False]

How can the correct values get passed into class "MainWindow" stored under variable "hello"

Asked By: Flynn Law

||

Answers:

It seems like you are not familiar with Object Oriented Programming.

values = Window2()
hello = values.returnvalues()

This block of code actually creates a new object Window2, and its checkboxes are indeed unchecked, which is why you always get [False, False, False].

The simplest (but not the best) way to solve your problem would be to give your Window2 instance to your mainWindow.

So instead of:

a = MainWindow()
b = Window2()

you do:

b = Window2()
a = MainWindow(b)

Instead of:

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

you should do:

class MainWindow(QMainWindow):
    def __init__(self, window_2):
        super(MainWindow, self).__init__()
        self.window_2 = window_2

and instead of:

values = Window2()
hello = values.returnvalues()

you should do:

hello = self.window_2.returnvalues()
Answered By: Viper
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.