[Qt5-feedback] C++ api to use for UI in addition to QML

Andre Somers andre at familiesomers.nl
Tue May 17 12:47:03 CEST 2011


Op Di, 17 mei, 2011 3:00 am, schreef Alan Alpert:
> On Tue, 17 May 2011 06:58:51 ext Alan Ezust wrote:
>> On Mon, May 16, 2011 at 5:57 AM, Andre Somers <andre at familiesomers.nl>
> wrote:
>> > Op Ma, 16 mei, 2011 2:43 pm, schreef Thiago Macieira:
>> >> On Monday, 16 de May de 2011 12:03:30 Serhiy wrote:
>> >>> So my point is that since QT itself uses C++ why not to have C++ api
>> >>> for new features as well.
>> >>
>> >> Because we lock ourselves to that API, due to C++'s binary
>> compatibility
>> >> requirements.
>> >>
>> >> We like to have a minimal API that addresses the use-cases. We'll be
>> >> doing it
>> >> once in QML. Doing it again in C++ is more work and also harder to
>> >> change later.
>> >
>> > Sure. But there needs to be some sort of sane way to interact with QML
>> > elements from C++, if QML is to be the main UI paradigm of Qt. At the
>> > very least, it would make sense to make it possible to get a pointer
>> to
>> > a QObject based on a QML id.
>>
>>     in QML 1.x, you can set
>> objectName: "someObjectName"
>>    on any QML Item and then use
>> QObject::findChild<QDeclarativeItem*>("someObjectName")
>>    to find it.
>>
>> But that assumes the QML Items derive from QObject. When we move over
>> to sceneGraph in QML 2.0, will that still be true?
>>
>> I don't know how to find something by id, but I'll be following this
>> thread in case someone else does.
>
> You can't find an item by id from C++, it's a detail internal to the QML
> engine. ObjectName will continue to work in Qt 5 though.

IMO, you should be able to do just that. I know you are not able to do
that right now, but I think that should be fixed. id's are the way you
reference objects in QML. You give objects an id, an then do stuff on
them. That is hardly an "internal detail". It simply doesn't make sense to
ask people to _also_ set the ObjectName property in order to be able to
access the same object from C++. It doesn't add to readability either. Why
isn't the id simply assigned as the object name by default? "Code less,
Create more" remember?

> In fact, QML 2.0 should have a very similar C++ API to QML1.x (because we
> expose so little of it). You will be able to instantiate QML scenes with
> components, and use the meta object system for introspection. It would
> help if
> you provide more detail than 'interact with'. What's missing from the QML
> 1.x C++ support that you want (keeping Thiago's points in mind, as he is
> entirely correct) ?
The main thing that is missing, is that it is not strait-forward to
interact with objects in the scene from C++ at all. You can not get a
type-save way to get and set properties for instance. All you can do is
use is

setProperty("width", newValue);

instead of the natural, Qt-standard way of doing:

setWidth(newValue);

>From a C++ point of view, that is *very* alien.

> Another thing to keep in mind is that you won't have to write Javascript
> to
> use QML. Qt Creator has a QML design view that you can use, and then
> everything gets hooked up like it used to be with .ui files. Except with
> greater customization and more animations. With Qt 5, you will be able to
> create UIs without 'writing' QML, just by using the tools.

I don't writing a bit of JavaScript to hook things together in my QML
parts. I *do* mind not being able to interact in a natural way with the
different elements that make up my GUI from C++. Currently, I resort to
creating a facade-like QObject an inserting that in my QML scene, and that
I hook up my values to. But I would not call that elegant, not is it the
best way to work in all cases.

André




More information about the Qt5-feedback mailing list