How to access properties of another file from main.qml. Qt- QML
-
Hi guys,
I'm having problems with QML and C++. If someone could take a look at this piece of code and tell me what I am doing wrong. I 'm just trying to print "msg.author" on a window (main window) but everytime I try to access it from main.qml there's an error saying "msg is not defined". Thank you for your time.
@MyItem.qml
import QtQuick 2.0
import QtQuick.Window 2.0Text {
id: text1
visible: true//property string autori: msg.author width: 100; height: 100 text: msg.author // invokes Message::author() to get this value
// anchors.centerIn: why
color: "green"
font.pixelSize: 20Component.onCompleted: { msg.author = "Jonah" // invokes Message::setAuthor() } @
main.qml
@
import QtQuick 2.2
import QtQuick.Window 2.1MyItem{
Window {
property string name: ; visible: true id: main_window width: 500; height: width color:"black"
}
} @
main.cpp
@
#include <QGuiApplication>
#include <QQmlApplicationEngine>#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
#include <QDebug>
#include "message.h"int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);QQmlApplicationEngine engine_e; engine_e.load(QUrl(QStringLiteral("qrc:/main.qml"))); QQmlEngine engine; Message msg; engine.rootContext()->setContextProperty("msg", &msg); QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml")); if(component.status()!=component.Ready){ if(component.Error){ qDebug()<<"Error: "<<component.errorString(); }
}
return app.exec();
}
@
message.h
@
#ifndef MESSAGE
#define MESSAGE#include <QObject>
#include <QString>class Message : public QObject
{
Q_OBJECT
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
public:
void setAuthor(const QString &a) {
if (a != m_author) {
m_author = a;
emit authorChanged();
}
}
QString author() const {
return m_author;
}
signals:
void authorChanged();
private:
QString m_author;
};#endif // MESSAGE
@ -
Hi,
First why are you loading the main.qml twice ?
The following way should work:
@
Message msg;
QQmlApplicationEngine engine_e;
engine_e.rootContext()->setContextProperty("msg", &msg);
engine_e.load(QUrl(QStringLiteral("qrc:/main.qml")));
@Also if you want to use QQmlComponent to load the file make sure you "create":http://doc.qt.io/qt-5/qqmlcomponent.html#create it.
-
[quote author="p3c0" date="1419141491"]Hi,
First why are you loading the main.qml twice ? [/quote]
Because I'm a noob (and a little bit dumb as it seems) plus I feel like there's no learning resources on Qt5 (especially Qt 5.4). No books, no explained examples(the ones from the doc. are vague and not explained at all. Imo of course).
I resolved this propblem with the help of some guy from another website where things get overflown (overflowed?).
Thanks for the answer anyway. I really appreciate it. -
That's fine. Everyone was newb at some point of time :)
This forum has quite good documentation for QML like:
"http://doc.qt.io/qt-5/gettingstartedqml.html":http://doc.qt.io/qt-5/gettingstartedqml.html
"http://doc.qt.io/qt-5/qmlapplications.html":http://doc.qt.io/qt-5/qmlapplications.html
"http://doc.qt.io/qt-5/qtqml-index.html":http://doc.qt.io/qt-5/qtqml-index.htmlIf you have Qt installed you can find some good examples in quick folder.
Apart from these there are also some external resources for eg.
"qmlbook.org":http://qmlbook.orgHope this helps you a bit ...
-
You have this nesting in main.qml:
@MyItem {
Window {
}
}@When loaded by QQmlApplicationEngine, only the innermost Window will become a window on your screen.
Try nesting the opposite way if you intended MyItem to be displayed inside the Window:
@Window {
MyItem {}
}@Also agree with p3c0 that you want to set the context property before loading the qml file and only load the qml file once.