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

My main application object is a QObject, that juggles a QSystemTrayIcon, a QDialog, a QWindow and a few other components. The app mostly sits in the tray, with some options dialogs etc etc.

Now, I'd like to use QMetaObject::connectSlotsByName() to connect signals from these objects to slots in the main object. It's 10-15 of them, so writing them by hand doesn't seem efficient, right, professional, modern, etc etc.

However, I can't use my QObject as parent to the QWidget based objects, nor can I change the object to inherit QWidget, as they will not show up, since the main object isn't visible.



Connecting signals to slots manually is perfectly fine. Qt itself is doing that, most Qt applications are doing that.

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


You could promote the QObject to 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(); }


  • Create a QPaintDevice from HDC handle
  • How to have a QTextBrowser to display contents of a QTextEdit?
  • Can NetBeans debug PHP and JavaScript at same time?
  • Python kernel dies for second run of PyQt5 GUI
  • Read and Write DOCX file
  • Dropdown menu with the dropdown-menu-right class does not align to the right
  • Looking for datastructure that maintains a size & purges older elements in the process
  • How to send None with Signals across threads?
  • WPF ClickOnce missed references
  • C++ development on linux Code::Blocks, EMACS or GVIM [closed]
  • missing pie chart and other elements when display serenity report from jenkins
  • Ant: fileset “dir” attribute with a runtime expanded full path
  • Fixed Background Works in Chrome but Not Firefox?
  • MySQL: Difference between `… ADD INDEX(a); … ADD INDEX(b);` and `… ADD INDEX(a,b);`?
  • Accessing another variable in the same class with a click event
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Why can't UI components be accessed from a backgroundworker?
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • How to getText() from the input field of an angularjs Application
  • Hide HTML elements without javascript, only CSS
  • Filter strings with regex before casting to numeric
  • Object and struct member access and address offset calculation
  • Force show.bind execution
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Use of this Javascript
  • Disable Enter in editText android
  • C++ Partial template specialization - design simplification
  • Sending data from AppleScript to FileMaker records
  • Javascript convert timezone issue
  • How to get next/previous record number?
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • Python: how to group similar lists together in a list of lists?
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Linking SubReports Without LinkChild/LinkMaster
  • Easiest way to encapsulate a HTML5 webpage into an android app?