Solved How to Promote to a Class at the Runtime
-
I have a class for double clicking signal that derived from QLabel and it is ok to promote a QLabel item that drag&dropped in designer.
But I am creating unspecific number of QLabels and QFrames etc. at the runtime. Number of items depends on a list count so it changes all the time. So the items are custom and i want to promote each item to that doubleClick class. Is it possible to do that. Promotion at runtime?
Here is how i create QLabel items:
QGridLayout *listItemsLayout = new QGridLayout; listItemsLayout ->setColumnStretch(0, 5); listItemsLayout ->setColumnStretch(1, 5); listItemsLayout ->setColumnStretch(2, 5); QList<QFrame *> frames; QList<QLabel *> labels; QList<QLabel *> infoTextLabels; for(int i = 0; i < aListCountVariable; i++) { frames << new QFrame(ui->PageWidget1); labels << new QLabel(frames.at(i)); infoTextLabels << new QLabel(frames.at(i)); // other details, specifying labels and frames sizes, qss etc /* ... */ } ui->containerWidget->setLayout(listItemsLayout );
-
What items are 'custom'? Do you have a custom class derived from QLabel/QWidget? If so why not simply instantiate them?
-
@Christian-Ehrlicher said in How to Promote to a Class at the Runtime:
instantiate
I think i used wrong word. What I tried to mean by saying custom is, the items are not created in designer. They had been created by code like i post above as code.
I have a doubleClick class. And i can promote the designer added widgets to that class and i'm connecting them like this in the MainWindow constructor:
connect(ui->testLabel1, SIGNAL(Mouse_DoubleClick()), this, SLOT(Mouse_DoubleClick())); connect(ui->testLabel2, SIGNAL(Mouse_DoubleClick()), this, SLOT(Mouse_DoubleClick()));
It's working properly. Now i want to do the same to the labels or frames created by code like above. I don't how how to do, how to promote them to doubleClick class and how to connect them. Need help on this point.
-
@R_Irudezu There is actually nothing to promote at runtime. You simply create your doubleClick instances instead of label.
doubleClick tmp = new doubleClick(parent); connect(tmp, SIGNAL(Mouse_DoubleClick()), this, SLOT(Mouse_DoubleClick()));
-
@jsulm Actually i didn't know there is no promote at runtime, but i also couldn't guess the solution is that easy. Thanks @jsulm
Thank you @Christian-Ehrlicher
Solution for my case:
labels << new doubleClick(frames.at(i)); labels.last()->connect(labels.last(), SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
It works but i don't it is the efficient way or not.
-
This post is deleted! -
@R_Irudezu said in How to Promote to a Class at the Runtime:
It works but i don't it is the efficient way or not.
Sure it is. But you can remove "labels.last()->" in front of connect() call.
-
@R_Irudezu said in How to Promote to a Class at the Runtime:
connect(labels.last(), SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
And please use the new signal/slot syntax: https://wiki.qt.io/New_Signal_Slot_Syntax - it helps you to catch errors which would only be visible on runtime otherwise.
-
@Christian-Ehrlicher Good to know that, thanks for notifying me.