Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

pyqt5 - cannot be associated with QComboBox

button of class Main don't connect with class Qcombobox of Signals

from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtWidgets import *
import sys
from PyQt5 import QtGui


class Signals(QWidget):
    asignal = pyqtSignal(str)
    def __init__(self):
        super(Signals, self).__init__()
        self.setGeometry(300, 250, 400, 300)
        self.ii()
        self.show()

    def ii(self):
        vbox = QVBoxLayout()

        self.combo = QComboBox()
        self.combo.addItem("Python")
        self.combo.addItem("Java")
        self.combo.addItem("C++")
        self.combo.addItem("C#")
        self.combo.addItem("Ruby")

        self.buttom = QPushButton("Click")
        self.buttom.clicked.connect(self.windown2)
        vbox.addWidget(self.combo)
        vbox.addWidget(self.buttom)
        self.setLayout(vbox)
    def do_something(self):
        self.asignal.emit(self.combo.currentText())

    def windown2(self):
        self.ggpp = Main()  
        self.ggpp.show()


class Main(QWidget):

    def __init__(self):
        super(Main, self).__init__()
        self.setGeometry(500,150, 600, 300)
        vbox1 = QVBoxLayout()

        self.buttom1 = QPushButton("Click")
        self.buttom1.clicked.connect(self.coso1)
    
        vbox1.addWidget(self.buttom1)
        self.setLayout(vbox1)

    def coso1(self):
        s = Signals()
        s.asignal.connect(lambda sig: print("self.combo.currentText()>>>>>" + sig))
        s.do_something()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    nals = Signals()
    nals.show()
    sys.exit(app.exec())

enter image description here

enter image description here

question from:https://stackoverflow.com/questions/65886102/cannot-be-associated-with-qcombobox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you see happens because you're not using the existing instance of Signals, but you're creating a new one each time the button is clicked.

In your case, you could add a reference to the instance as an argument when you create the new window, so that you can correctly connect to its signal.

class Signals(QWidget):
    # ...
    def windown2(self):
        self.ggpp = Main(self)
        self.ggpp.show()

class Main(QWidget):
    def __init__(self, signals):
        super(Main, self).__init__()

        self.signals = signals
        self.signals.asignal.connect(self.coso1)

        self.setGeometry(500,150, 600, 300)
        vbox1 = QVBoxLayout()

        self.buttom1 = QPushButton("Click")
        self.buttom1.clicked.connect(self.signals.do_something)
    
        vbox1.addWidget(self.buttom1)
        self.setLayout(vbox1)

    def coso1(self, sig):
        print("self.combo.currentText()>>>>>" + sig)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...