Locate QML Properties
-
I am struggling trying to use the QML interface. I have run into countless properties that exist in the C++ API that do not seem to exist in QML. I would like to know if there is a way to figure out this information when it is never seems to be addressed in the docs. Does this mean the properties or methods are simply not exposed to the XML interface, or is there something I am missing. A specific example is QTextEdit::setTabStopWidth(). Is there a way to set this value in QML?
-
@whitecastleroad If I understand correctly, same-named QtQuick Components and Qt Widgets do not have the same API. Often, the QtQuick version has fewer features/is simpler, to make programming simpler.
My approach is the following: The context-sensitive help in QtCreator is fairly accurate. You can hit F1 on the name of any QtQuick Component in your QML code, and as long as there is no syntax error in your code, you'll get to see all the truly available properties.
If it turns out that the QtQuick Component is not sufficient for your needs, you can always create a new C++ class that inherits from a Qt Widget in which you are interested (in this case, QTextEdit), give it your own name, and expose it to be used in QML using the typical process of using the Q_OBJECT macro in your class header file and calling qmlRegisterType() in main.cpp to make your new class available to QML.
- VStevenP
-
@VStevenP
Thank you, that is a good idea - I never thought about extending an existing object and exposing it to QtQuick. Otherwise, in the situation I mentioned above, is there any way to get a C++ QTextEdit object pointer for the TextEdit component in the QML file?Something along these lines:
QObject *o = engine.rootObjects().first()->findChild<QObject*>("rich"); if (o) { QQuickItem *item = qobject_cast<QQuickItem*>(o); if (item) { qDebug() << "got item"; QTextEdit *rich = qobject_cast<QTextEdit*>(o); //this does not work if (rich) { qDebug() << "got rich"; } }
-
@whitecastleroad Yes. Since TextEdit inherits Item you will have to cast it to QQuickItem instead of QTextEdit.
P.S: The new forum's editor follows MarkDown markup language. so you can follow its rules. For eg. to post code put it inside ```(3 backticks).
-
@p3c0 I have a feeling this may not help him, though, because if he has to cast it to a QQuickItem, he will be unable to call setTabStopWidth on that pointer.
@whitecastleroad Basically, I think p3co's response explains it all. He explains that QML TextEdit inherits from QQuickItem, not QTextEdit. This is the evidence they are not objects of the same class, despite some similarity in properties available.
-
@jalomic Thanks for the clarification. That's good to know in case I ever want to look at the code.
@whitecastleroad Earlier in the thread I had mentioned about using the QML_DECLARE_TYPE macro. I did some more reading, and I learned that macro is vestigial and no longer needed/used. If you use Q_OBJECT macro in your class and simply use qmlRegisterType() in your main.cpp as documented in the Qt online doc, that is enough to make your class available to QML.