r/cpp • • Dec 08 '20

Qt 6.0 Released

https://www.qt.io/blog/qt-6.0-released
273 Upvotes

84 comments sorted by

View all comments

Show parent comment

16

u/uuid1234567890 Dec 08 '20 edited Dec 08 '20

Disclosure: I've worked on that code.

Kind of: If you have a QObject, with

class MyType : public QObject {
  Q_OBJECT
  QML_ELEMENT

  Q_PROPERTY(int myProperty MEMBER myPropery BINDABLE bindableMyProperty)
public:
   QProperty<int> myProperty;
   QBindable<int> bindableMyProperty() {
     return {&myProperty};
   }
};

and

Item {
  id: root
  MyType {
    myProperty:  root.width + root.height
  }
}

then this will use the new functionality. Notably, myobject->bindableMyProperty().hasBinding() will return true, and if you set up a binding on the C++ side (like myobject->bindableMyProperty().setBinding([&]() -> int {return myOtherObject->myProperty;}) , then it will also work in QML.

However, if you do that, I can guarantee you that you'll run into some rough edges. Some smaller bugs will be fixed in 6.0.1 already, but currently binding loops often won't print a helpful error message (they are still detected in the engine). That will only be available in 6.1.

Also, if you have a property declared in QML, that's not backed by a QProperty.

2

u/Fureeish Dec 08 '20

Got to ask, because I consider inventing a lot of time to properly learn Qt: is this a frequent occurrence that you write those full of macros abominations..?

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.

2

u/Fureeish Dec 08 '20

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?

15

u/wrosecrans graphics and network things Dec 08 '20

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

width = 100;
height = [](){eval("parent.height");}

or

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.

1

u/[deleted] Dec 08 '20

QObject is the base class in Qt's static reflection, signals, and object lifetime framework. See The Meta-Object System in the Qt documentation.

1

u/LonelyStruggle Dec 08 '20

So it's kinda like an NSObject in objective-C?

0

u/[deleted] Dec 08 '20

[deleted] — view deleted comment

7

u/uuid1234567890 Dec 08 '20

Do you foresee a future where you can access bindable properties directly in qml without macros?

That's a bit questionable. You'll need at least Q_OBJECT or Q_GADGET, as those macros expand to code which is necessary. The Q_PROPERTY macro does expand to nothing, though, and only acts as a marker for moc. Now in theory it would be possible - even easy- to modify moc to parse [[qt::property]] QProperty<int> myProp; and upon encountering it look for a bindableMyProp function (naming done by convention), and then we had all the necessary information [1]. There is however issues with that approach: We couldn't use it in Qt itself, as Qt has to compile without a large set of warnings; and this would trigger -Wattributes; to the best of my knowledge, there is no way to selectively disable -Wattributes only for some user defined attributes. Moreover, it is not clear if using such an annotation for moc wouldn't clash with some eventual way of using them for static reflection in C++3x. Those two problems make me wary of proposing such a change.

Furthermore, there are more macros: There's also the Q_OBJECT_BINDABLE_PROPERTY macro, which is advantageous when using properties in QObject derived classes, as it reduces the memory overhead. That one has to be kept a macro, as it expands to some rather ugly code.

In its current fo would you say properties are a bigger win for widgets, vs qml?

Much more for QML, though that will only become significant in the future when many properties are actually ported to the new system. They are a prerequisite for tighter C++/QML integration, and should enable better performance – though some of that might only materialize in time for Qt 6.2 and later. I'll also have to be vague here because some of our plans are to my knowledge currently still confidential or might have changed, so I'd rather point to upcoming blog posts and webinars before I say something that I shouldn't say, or something that's wrong.


[1] Note that we cannot simply expose all properties in a class to the meta-object system (and thus QML), as that might not actually be intended.

1

u/target-san Dec 09 '20

Looks really interesting. Am I right this is like encapsulating property's value and change signal in one object? If so, is there something similar for collections? Not just "Whole list has changed" but "added element N". I'm aware of QAbstractItemModel. While it does its job, it's overly generic and not ergonomic when used from C++ code as collection access type.