A
A
Alexander2020-01-15 15:58:59
Python
Alexander, 2020-01-15 15:58:59

How to pass variable from Widget to MainWindow via pyqtSignal and why doesn't it work?

There is MainWindow. It has a Btn button and a label_CC. By clicking on the button, a separate widget opens, in which there is a tableWidget and label_2. Through the cellClicked method, the value in the table cell is safely passed inside the widget to label_2 (as a variable inside the function).
But I can’t figure out how to transfer this value to label_СС as well by clicking on a cell through a signal. I tried various combinations (both the variable in the signal and the one below) - nothing works. The question is why? Or is the signal between classes somehow not working that way?

Here is the code (removed all unnecessary)

import sys 

from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import pyqtSignal

class Main(QMainWindow):
    def __init__(self):        
        super().__init__()
        uic.loadUi("CancerRiskAssesment_main.ui", self)
    
        self.B_CC.clicked.connect(self.show_cancer_ch)
        self.dialog22 = Cancerogen_choicewindow()
        self.dialog22.mySignal.connect(self.changelabel)

    def show_cancer_ch(self):
        self.dialog = Cancerogen_choicewindow()
        self.dialog.show()

    def changelabel(self):
        self.label_CC.setText(self.dialog22.SF_transmit)


class Cancerogen_choicewindow(QWidget):
    SF_transmit = None

    mySignal = pyqtSignal()

    def __init__(self, parent=None):      
        super().__init__(parent)
        uic.loadUi("Cancerogen_choice.ui", self)
        
        self.tableWidget.cellClicked.connect(self.cellclick)
        self.pushButton11.clicked.connect(self.send_data)
    
    def cellclick(self, row, column):
        item = self.tableWidget.item(row, column)
        self.SF = item.text()
        self.label_2.setText(self.SF)
    def send_data(self):
        self.SF_transmit = self.SF
        self.mySignal.emit()
        self.close()

def main():
    app = QApplication(sys.argv)  
    window = Main()  
    window.show()  
    app.exec_()  

if __name__ == '__main__':  
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2020-01-15
@Cuaquero

Forget about global
Subscribe the main widget to the cellClicked signal when creating another widget, then unsubscribe. (or create a custom signal and subscribe to it)
Call the main window method from the widget directly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question