All Qt Widgets are derived from QObject.
Yes you can use QObject, if you just want a pure C++ object
(ie, if you dont need SIGNAL, SLOTS & PROPERTIES)
Also, the Q_OBJECT macro is mandatory for any object (including QObject)
that implements signals, slots or properties.
You can set a object name to your object as follows:
setObjectName("xxxx");
{
Q_OBJECT
// follow Qt standards.
constructor
{
items<<"label1"<<"label2"<<label3";
objects<<"objl1"<<objl2"<<objl3";
QVBoxLayout* layout = new QVBoxLayout();
for (int i = 0; items.count(); i++)
{
QLabel* wid = new QLabel (items.at(i), this);
// Other way:
// QLabel* wid = new QLabel (items.at(i));
// wid.setParent (this);
setObjectName (objects.at(i)); /* Set the object name */
wid->setFixedSize(20, 20); /* Height, Width */
}
QList<QWidget *> widgets = this->findChildren<QWidget *>("objl1");
// returns you the list of widgets with the name "objl1"
if(widgets.count() > 0)
widgets[0]->setFixedSize(40,40); //changing the size
}
}
class MainWidget : public QWidget
{
Q_OBJECT
// follow Qt standards.
constructor
{
QStringList items, objects;
items<<"label1"<<"label2"<<label3";
objects<<"objl1"<<objl2"<<objl3";
QVBoxLayout* layout = new QVBoxLayout();
for (int i = 0; items.count(); i++)
{
QLabel* wid = new QLabel (items.at(i), this);
// Other way:
// QLabel* wid = new QLabel (items.at(i));
// wid.setParent (this);
setObjectName (objects.at(i)); /* Set the object name */
wid->setFixedSize(20, 20); /* Height, Width */
}
QList<QWidget *> widgets = this->findChildren<QWidget *>("objl1");
// returns you the list of widgets with the name "objl1"
if(widgets.count() > 0)
widgets[0]->setFixedSize(40,40); //changing the size
}
}
To copy to clipboard, switch view to plain text mode
Now all label boxes you have created becomes children of your main
widget. And you can use findChildren and modify their properties.
Please modify the code with fixing syntax issues and Qt standards,
it should work fine.
Thanks,
Ram
Bookmarks