Solved Performance of a one time signal/slot connection?
-
I have a vector that is going to store a bunch of the same widget with a signal, and I want to dynamically connect them to the same slot at runtime.
At runtime, I will have an action that will trigger a function that's going to dynamically create signal/slot connections from the selected widget.
void MainWindow::transformNumber(int num) // this will triggered by arrow key { int currentIndex = m_ui->SomeWidget->currentIndex(); MyWidget * core= m_myVector[currentIndex].core; connect(core, &MyWidget ::signalNumberChanged, this, &MainWindow::slotUpdateNumber); core->changeNumber(num); // this will fire signalNumberChanged }
As you can see the connection was created inside the triggered function at runtime, and with this code, the slot is going to loop more and more each time with more key press, which is not good.
I did found a solution on StackOverflow to create a custom connection that will delete itself after first call. But my question is will there be a performance issue if I'm to trigger this function 60 times a second for like 15 minutes?
-
Then do you connection only once when you fill m_myVector. If you are not able to do so (don't see a reason why it should not be possible) then take a look at Qt::UniqueConnection.
When you don't trigger a signal the slot will not be executed so there is no performance impact. -
How do I add the connection to my vector?
-
@lansing said in Performance of a one time signal/slot connection?:
How do I add the connection to my vector?
Loop through the container and do a connect maybe?
-
This is what I got. Is this correct?
struct MyPair{ MyWidget * core; QMetaObject::Connection connection; } // mainwindow.cpp MyPair mp; mp.core= new MyWidget (); mp.connection = connect(mp.core, &MyWidget::signalNumberChanged, this, &MainWindow::slotUpdateNumber); m_myVector.append(mp);
-
@lansing said in Performance of a one time signal/slot connection?:
Is this correct?
More or less, why do you need to store the connection?
-
Didn't you say to add it to the vector?
-
He was talking about something like this:
QVector<MyWidget> vec; MyWidget *w = new MyWidget(); // Make connection before MyWidget is added to vector connect(w, &MyWidget ::signalNumberChanged, this, &MainWindow::slotUpdateNumber); vec.push_back(w);
-
Oh, thank you, this looks better