Important: Please read the Qt Code of Conduct -

[SOLVED] Connecting two subclassed widgets using signal/slots

  • Hi! It may be a silly question but I have been struggling with this for some time...

    I have a MainWindow with 2xQPushButton: button1 and button2. I click on button1 and object from ClassA (QWidget) is created and shown. In newly opened window I have another QPushButton - buttonA. I would like to click on it and emit a signal to MainWindow's button2.

    The general idea is to use connect(buttonA, SIGNAL(emittedSignalFromClassA()), this, SLOT(doSomethingInMainWindow)));
    The question is how to get a pointer to dynamically created obcject to be "visible" in mainwindow??

    I created this example to understand how to solve my problem, my code requires similar solution. I appreciate any of Your help!

    Regards, Michael.

  • Hi and welcome to devnet,

    I suggest to change your approach to the problem; the simplest way to do this is:

    1. Create a signal in your ClassA:
        void buttonClicked();
    1. Connect the buttonA clicked() signal to that signal
    connect(buttonA, SIGNAL(clicked()), this, SIGNAL(buttonClicked()));
    1. In your MainWindow connect the buttonClicked signal coming from ClassA to your slot
    _classA = new ClassA();
    connect(_classA, SIGNAL(buttonClicked()), this, SLOT(mySlot()));
    Happy Programming

  • Thank you, connecting 2 signals did the job! I spent hours to make this work...Have a nice day! ;)

  • You're welcome

    Happy Programming

Log in to reply