Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connection after creating new object each time



  • I have a button which creates a new widget every time its pressed:

    void protocolForm::on_button_graph_clicked()
    {
    
        qDebug() << VectorMap[ ui->tableWidget->item(rowFocused,0)->text()];
        graphFormInstance = new graphForm(this);
        graphFormInstance->show();
    }
    

    And I want connect a signal to all of these created objects such as:
    connect(this, &SendVectorMaptoGraph, graphFormInstace, &graphForm::receiveVectorMap);

    So it will look like :

    void protocolForm::on_button_graph_clicked()
    {
    
        qDebug() << VectorMap[ ui->tableWidget->item(rowFocused,0)->text()];
        graphFormInstance = new graphForm(this);
        graphFormInstance->show();
        connect(this, &SendVectorMaptoGraph, graphFormInstance, &graphForm::receiveVectorMap);
       
    }
    

    After this connection when I emit the signal SendVectorMaptoGraph. Will it emit the slots in the all created graphFormInstance's?



  • @GunkutA said in Connection after creating new object each time:

    creates a new widget every time its pressed:

    @GunkutA said in Connection after creating new object each time:

    to all of these created objects

    Yes, your are right, literally.. It creates a new widget every time by overriding the old instance. So you have only one at a time...

    @GunkutA said in Connection after creating new object each time:

    Will it emit the slots in the all created graphFormInstance's?

    The connection itself emits nothing, but it ensures that this knows about the slot in graphForm.

    @GunkutA said in Connection after creating new object each time:

    connect(this, &SendVectorMaptoGraph, graphFormInstance, &graphForm::receiveVectorMap);

    There's also a signal missing.

    connect(this, &THIS_CLASS::SIGNAL, graphFormInstance, &graphForm::receiveVectorMap);
    

    (THIS_CLASS = ProtocolForm in your case)



  • @Pl45m4 But it does not close the old one. Instead creates a new object. So first one is still opened when I press the button twice



  • @GunkutA said in Connection after creating new object each time:

    So first one is still opened when I press the button twice

    Do you want old one to remain open? If not it's up to you to close it before creating a new object... and thus having to worry about making the signal/slot connection just once



  • @Pablo-J-Rogina I want to keep them. But what I am not sure about those signal slot connections. If I do not close the previous objects and lets say I opened 3 objects. All of them open. And I put that connect line after all of them. Will all these 3 objects be connected to that signal slot mechanism?



  • @GunkutA

    But you still override graphFormInstance with the new instance

    @GunkutA said in Connection after creating new object each time:

    I want to keep them. But what I am not sure about those signal slot connections. If I do not close the previous objects and lets say I opened 3 objects. All of them open

    You lose access to the previous instances, if you do not store them somewhere (by adding them to e.g. QGraphicsScene or whatever you want to do with these graphs). Only because they are shown, doesn't mean, that you have access to all of them, but it still will call their slots.


  • Moderators

    @GunkutA said:

    I want to keep them. But what I am not sure about those signal slot connections. If I do not close the previous objects and lets say I opened 3 objects. All of them open. And I put that connect line after all of them. Will all these 3 objects be connected to that signal slot mechanism?

    Yes.

    Each new graphForm creates a separate object and each connection between this and those objects is a separate connection so when you emit a signal a slot in all connected objects will be called.



  • @GunkutA said in Connection after creating new object each time:

    This is a basic programming concept. If you have only a variable to hold a pointer, every time a new object is created (you've got a address) that only variable will point to the new recently created object.

    I want to keep them.

    So you need a data structure capable of holding several pointers at the same time. Again, this is a basic programming concept.


Log in to reply