Is it possible to have a QWidget as a child for a QObject?

My main application object is QObject , which juggles QSystemTrayIcon , a QDialog , a QWindow and several other components. The application is mainly located in the tray, with some parameter dialogs, etc. Etc.

Now I would like to use QMetaObject::connectSlotsByName() to connect the signals from these objects to the slots in the main object. These are 10-15 of them, so writing them manually does not seem effective, correct, professional, modern, etc.

However, I cannot use my QObject as the parent object for QWidget objects, nor can I change the object to inherit from QWidget , since they will not be displayed because the main object is not visible.

Ideas?

+1
source share
2 answers

Manually connecting signals to the connectors is great. Qt itself does this; most Qt applications do this.

I'm afraid that you cannot use connectSlotsByName for parent-child problems with QWidget , but if you really want to, you have all the metadata available in QMetaObject , so you can write a function that works like connectSlotsByName on any pair / set of QObject s .

+1
source

You can promote a QObject in a hidden QWidget , see this answer . In a nutshell:

 #include <QtWidgets> int main(int argc, char ** argv) { QApplication app{argc, argv}; QWidget parent; QLabel l1{"Close me to quit!"}, l2{"Hello!"}; for (auto label : {&l1, &l2}) { label->setMinimumSize(200, 100); label->setParent(&parent); label->setWindowFlags(Qt::Window); label->setText(QString("%1 Parent: %2."). arg(label->text()).arg((quintptr)label->parent(), 0, 16)); label->show(); } l2.setAttribute(Qt::WA_QuitOnClose, false); return app.exec(); } 
+2
source

Source: https://habr.com/ru/post/1215162/


All Articles