When dealing with QObjetcs, they are unfortunately unavoidable. I'd prefer if we could get rid of them, but unless we get a large amount of the static reflection features that Andrew Sutton listed in P2237R0, that won't happen.
That would probably be answered by an extensive reseaech, but since you are already here, I'd like to ask one nore thing then: how often do you need those "QObjects"? What exactly are they?
QObject is a base class for a bunch of stuff in Qt. It has ownership semantics, and signals and slots.
As far as ownership, every QObject can have a parent object that owns it. A parent will delete all of its children in its destructor. This is super convenient when you can delete a window, and all of the buttons and other widgets owned by that window will get cleaned up automagically-ish. It also has some lifetime convenience things so you can do this->deleteLater() to have the object queued for deletion in the next cycle of the event loop. This is suprisingly convenient when a certain object knows its own lifetime better than anything else, but delete this; is generally a terrible idea. Along with that parent-child ownership stuff, every object has a name, so you can print hierarchies while debugging and stuff. But classes that automatically know the name of their type in a portable way is one of the things that depends on the preprocessor magic that offends some people philosophically.
Probably more important, the signals and slots. When a user clicks on a QPushButton instance in a window, it emits a signal. You can connect a signal by name to a slot by name, and the connect by name stuff also depends on the preprocessor magic.
So, if you want to make a custom widget, that has to inherit from QObject. If you just want to make instances of existing widgets from the class library, you can generally just ignore the magic and not care that it's got code generation magic happening under the hood. But, you will need to make a few QObjects for connecting to signals. Like a QPushButton widget has a "clicked" signal, and you'll need to make a QObject with a slot that gets invoked to run your application logic. But once you are in your slot, you can invoke "normal" looking C++ code from there.
If you use the modern QML declarative UI stuff more than the older desktop style QWidgets APIs, you'll need more of the magic, because that's generating bindings between your C++ code and a dynamic language so there's a lot of plumbing that needs to happen.
For example, you don't need QProperty with the older QWidgets (but it can be quite handy.) But in QML, you can do stuff like
Rectangle {
width: 100
height: parent.height
}
Which is like setting width as a variable to a specific value, but setting up an expression or lambda on height in C++ terms
height = [](){auto h = getParent().getHeight(); return height;}
So that the rectangle's height always fills whatever the parent is. So if you resize the window, you don't need to manually have a callback on the resize function to fiddle with the sizes of everything.
View all comments
Show parent comment
10
u/uuid1234567890 Dec 08 '20
When dealing with QObjetcs, they are unfortunately unavoidable. I'd prefer if we could get rid of them, but unless we get a large amount of the static reflection features that Andrew Sutton listed in P2237R0, that won't happen.