I'm converting a project made with Borland C++Builder. There I have a class that manages messages for and from an electronic device. This class (call it MsgManager) encapsulate both a serial port and a tcpsocket, provides unified method and events no matter what kind of connection is used as long as some properties, and handle the whole protocol checks.
I want this class to be "global": so each of all my other classes have a pointer to the MsgManager, but also QML pages needs to use his methods and properties. The only thing that QML pages will not use are the various "events" (like a datareceived events or some error like timouts etc)
1) Is this a bad design for a QT applications? Seems that my old habits are sometime wrong in this new enviroment...
2)if no (or at least not THAT bad) how can i obtain this?
I tried using
qmlRegisterType<cMessageManager>("Phase.MessageManager", 1, 0, "MessageManager");
in main.cpp and then
MessageManager {
id: msgman;
}
in the mainForm.qml but i'm not able to "point" to the class from C++... and i'm not able to view the class from QML when i create it in c++
Thanks.
-EDIT- Adding code and asking for clarification:
@leemes: Thanks for the detailed (but noob-friendly :) ) answer.
I still have troubles.... this was my previous working code (even before trying to adding my new class):
#include <QtGui/QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QtQuick/QQuickView> // Necessario per QQuickWindow
#include "ui_updater.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<UI_Updater>("Phase.UI_Updater", 1, 0, "UI_Updater");
QQmlApplicationEngine engine(QUrl("qrc:/qml/MainForm.qml"));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
if ( !window ) {
qWarning("Error: Your root item has to be a Window.");
return -1;
}
window->show();
return app.exec();
}
I tried this changes:
#include <QtGui/QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QtQuick/QQuickView> // Necessario per QQuickWindow
#include "ui_updater.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// --- CHANGES TO TRY setContextProperty SOLUTION -------------------------
//qmlRegisterType<UI_Updater>("Phase.UI_Updater", 1, 0, "UI_Updater");
//QQmlApplicationEngine engine(QUrl("qrc:/qml/MainForm.qml"));
UI_Updater* ui_up;
ui_up = new UI_Updater();
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("UI_Updater",ui_up);
engine.setBaseUrl(QUrl("qrc:/qml/MainForm.qml"));
//-------------------------------------------------------------------------
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
if ( !window ) {
qWarning("Error: Your root item has to be a Window.");
return -1;
}
window->show();
return app.exec();
}
But the debugger breaks somewhere in the disassembler window with a "segmentation fault" error. I see that i don't have the QQuickView (or QtQuickApplicationViewer) object you mentioned, and i can't recall why i ended up with the code i have now (but it was the only way i found to make it work :( )
So, could you help me a tiny bit again?
QQmlContext::setContextProperty.