View Full Version : CPP QML communication
nestuser
21st August 2012, 08:00
Now I'm working with QT Quick Project -> QT Quick application. I loaded a QML file using QDeclaratieveView and did the CPP to QML and QML to CPP communication. It is worked... :)
Can i do the same in any other QT applications like QT Widget-> QT GUI or Other Project??
spirit
21st August 2012, 08:05
For other GUI - yes. But, anyway, you need QDeclaratieveView instance.
nestuser
21st August 2012, 09:00
thanks for your replay.
But I opened a "QT Widget->QT GUI App" and try to include QDeclarativeView.
then i got the error QDeclarativeView: No such file or directory.. :(
spirit
21st August 2012, 09:02
That's because you didn't add QT += declarative in your pro-file.
nestuser
21st August 2012, 09:37
yes, it is worked... :)
thank you very much.....
I used
qmlRegisterType<Widget>("File", 1,1, "TextUpdate"); to register the class in main and in the QML file I imported the file and called the function
TextUpdate{
id: sample1
}. then i got the visual c++ runtime library error.. :(
spirit
21st August 2012, 09:38
What's an error message?
nestuser
21st August 2012, 09:43
That error message says that "The application has requested the runtime to terminate it in an unusual way"
spirit
21st August 2012, 09:47
We can only guess what's wrong without seeing your code.
nestuser
21st August 2012, 10:04
ok. I will show you my code.
This is my Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void ShowQML();
void setText(QString);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
This is my Widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
#include <QDeclarativeComponent>
#include <QDeclarativeEngine>
#include <QDeclarativeItem>
#include <QDeclarativeView>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
qDebug()<<"Inside cnstrctr";
}
void Widget::ShowQML()
{
QDeclarativeView *testView = new QDeclarativeView();
testView->setSource(QUrl::fromLocalFile("test2.qml"));
testView->show();
}
Widget::~Widget()
{
delete ui;
}
void Widget::setText(QString msg)
{
qDebug()<<"Inside slot in CPP";
//SendSignal();
}
this is the main file
#include <QtGui/QApplication>
#include "widget.h"
#include <qdeclarative.h>
#include <QDeclarativeView>
#include <QApplication>
#include <QBoxLayout>
int main(int argc, char *argv[])
{
qmlRegisterType<Widget>("File", 1,1, "TextUpdate");
QApplication a(argc, argv);
Widget w;
//w.show();
w.ShowQML();
return a.exec();
}
and the QML file is
// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import File 1.1
Rectangle {
width: 100
height: 62
TextUpdate{
id: sample1
}
}
spirit
21st August 2012, 10:11
First of all you have memory leak in Widget::ShowQML.
Second, are you trying to register Widget (which inherits QWidget) here qmlRegisterType<Widget>("File", 1,1, "TextUpdate");?
nestuser
21st August 2012, 10:18
Yes, Widget is my class name. I'm trying to register with that class name.
spirit
21st August 2012, 10:25
That's not possible. You can only register classes inherited from QObject. I know that QWidget inherits QObject, but you can't register widgets for using them in QML.
Added after 4 minutes:
QML relies heavily on Qt's meta object system and can only instantiate classes that derive from QObject. For visual element types, this will usually mean a subclass of QDeclarativeItem; for models used with the view elements, a subclass of QAbstractItemModel; and for arbitrary objects with properties, a direct subclass of QObject.
See Extending QML Functionalities using C++ (http://qt-project.org/doc/qt-4.8/qml-extending.html).
nestuser
21st August 2012, 10:40
yes, now it is worked... :)
one more doubt
In my project i have lots of CPP to QML communication. Then which UI is best for me, QT Quick or QT Widget?
spirit
21st August 2012, 10:51
It depends on what kind of UI do you what: modern fancy live ui (with animations and other stuff) or old school ui. :)
nestuser
21st August 2012, 11:30
:) I'm not developing QML file. I need only communication part.
spirit
21st August 2012, 11:34
So, you are working on the back-end. Then you should concentrate on architecting of the back-end structure. If you make the back-end independent from ui then you (or your colleagues) will be able to choose the best ui approach: QML or Qt widget-based.
nestuser
21st August 2012, 11:42
thank you very much.. :)
I'm working on back-end. I need to emit only some signals to QML file.
I think QT widget is apt for it, right??
spirit
21st August 2012, 11:45
Yes, it is.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.