Connecting inherited and new Signals & Slots



  • I'm in the process of writing my first Qt application and attempting a signal and slot connection. I want the user to click on a menu item, so I need to use a triggered() signal from the QAction object. I want to connect that signal to a slot that is in another class. That slot will end up creating a QDockWidget and several other objects whose parent will be the MainWindow. So I'll need to send an instance of the mainwindow to that slot. How is this done? I've read the documentation on "Signals & Slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html#signals but the examples there show a signal and slot that is created within that class whose function code can be accessed. A few questions:

    1. How does triggered() know how to call the slot that I've designated in the other class? I thought emit <slot name> needs to be specifically called.
    2. If I'm using the inherited triggered(), how can I pass the instance of the MainWindow for the slot's parameter?

    EDIT: Hm, on a second read through, I'm understanding signals & slots incorrectly. I create my own signals and slots, and in my slot I check to see if isChecked() returns true, if it is, I emit the signal that I want. Is that more correct?

    EDIT TWO: Hm, I tried connecting a slot in one class and emitting a signal from another class. I get an error about "cannot access protected member declared in class '<class>'". Are signals and slots only connectable from within the same class? You can connect different objects of the same class but not two objects from different classes?



  • [quote author="Plissken" date="1342340756"]1. How does triggered() know how to call the slot that I've designated in the other class? I thought emit <slot name> needs to be specifically called. [/quote]
    The signal itself doesn't know anything about the slot(s) connected to it. The QObject-Instance in which you called connect on the signal stores and invokes these connections.
    emit does not have to be specifically called when you're using the built in signals from QT. Of course, your own signals must be emitted manually.

    [quote author="Plissken" date="1342340756"]. If I'm using the inherited triggered(), how can I pass the instance of the MainWindow for the slot's parameter?[/quote]
    You can't. The Slot can only recieve parameters provided by the signal. But you could give the instance of your other class a pointer to the MainWindow, that is stored in a member...

    [quote author="Plissken" date="1342340756"]EDIT: Hm, on a second read through, I'm understanding signals & slots incorrectly. I create my own signals and slots, and in my slot I check to see if isChecked() returns true, if it is, I emit the signal that I want. Is that more correct?[/quote]
    You will still need to connect to the triggered()-signal because you won't notice the click if you don't. You won't need own signals for what you're trying to do.

    [quote author="Plissken" date="1342340756"]EDIT TWO: Hm, I tried connecting a slot in one class and emitting a signal from another class. I get an error about "cannot access protected member declared in class '<class>'". Are signals and slots only connectable from within the same class? You can connect different objects of the same class but not two objects from different classes?[/quote]
    All signals are protected. So if you want to emit a signalfrom another class, inherit it.

    Try this:
    In MainWindow (Probably constructor):
    [code]
    c_pDockWidgetCreator = new CDockWidgetCreator();
    c_pDockWidgetCreator->setMainWindowRef(this);
    connect(c_pMenuInstance, SIGNAL(triggered()), c_pDockWidgetCreator(SLOT(yourSlotName()));
    [/code]
    And then in your slot use the MainWindow that has been passed to setMainWindowRef()



  • Can you send the code ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.