PDA

View Full Version : Qml and Visual studio 2012



RegMe
22nd December 2015, 12:13
Hello everybody,
I created a graphical interface with QML, I would like to integrate it in Visual studio 2012 to do some treatements and operations with C++

I did some research in NET, I found an exemple with QDeclarativeView

TestListView::TestListView(QWidget *parent)
: QMainWindow(parent)
{
ui= new QDeclarativeView;
QString filePath = QDir::currentPath() + "/main_view.qml";

ui->setSource(QUrl::fromLocalFile(filePath));
setCentralWidget(ui);

ui->setResizeMode(QDeclarativeView::SizeRootObjectToVi ew);
root = ui->rootObject();
ui->rootContext()->setContextProperty("Window", this);

std::vector<std::string> dataTable;
dataTable.push_back("C++");
dataTable.push_back("Java");
dataTable.push_back("Python");

QList<QObject*> dataList;
QString color = "gray";

for(int i=0; i<dataTable.size(); i++)
{
color = (color == "gray") ? "silver" : "gray" ;
dataList.append(new DataObject(QString(dataTable.at(i).c_str()), color));

}

ui->rootContext()->setContextProperty("dataModel", QVariant::fromValue(dataList));
}

But QtDeclarative works only with QtQuick 1.0
I'm using QtQuick 2.0 so I tried to use QQuickView :
ui= new QQuickView;
QString filePath = QDir::currentPath() + "/main_view.qml";

ui->setSource(QUrl::fromLocalFile(filePath));
setCentralWidget(ui); // QQuickView is incompatible with QWidget

ui->setResizeMode(QQuickView::SizeRootObjectToView);
root = ui->rootObject();
ui->rootContext()->setContextProperty("Window", this);

Someone can resolve this error

best regards

anda_skoa
22nd December 2015, 13:19
QQuickView is its own Window.

If you need to embed it into a QMainWindow, you have two options:
- use QWidget::createWindowContainer()

- use QQuickWidget


Cheers,
_

RegMe
22nd December 2015, 17:13
Thanks,

If I want to do it with QQmlApplicationEngine, how can I embed it into a QMainWindow

ui = new QQmlApplicationEngine;

QString filePath = QDir::currentPath() + "/main.qml";
ui->load(QUrl::fromLocalFile(filePath));

setCentralWidget(ui); // QQmlApplicationEngine is incompatible with QWidget

root = ui->rootObjects().first();
ui->rootContext()->setContextProperty("Window", this);


ApplicationWindow don't work with QQmlApplicationEngine, Am t right ???

What do you think is better, working with QQuickView or QQmlApplicationEngine

anda_skoa
22nd December 2015, 19:46
If I want to do it with QQmlApplicationEngine, how can I embed it into a QMainWindow

QQmlApplicationEngine is no UI component, it can't be anymore "embedded" in a QMainWindow than e.g. a QTimer.



ApplicationWindow don't work with QQmlApplicationEngine, Am t right ???

No, it does.



What do you think is better, working with QQuickView or QQmlApplicationEngine

This is not an either/or kind of decision.
Both QQuickView and QQuickWidget can work on an externally created QQmlEngine.

Cheers,
_

RegMe
23rd December 2015, 10:05
Thanks

I would like to say : ApplicationWindow don't work with QQuickView, Am I right ???

In your opinion, What do you prefer :
1- * Embed QQuickView in QMainWindow
or 2- * Put directly QQmlApplicationEngine in the main, Code belowCode below


#include "testlistview.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
/*QApplication a(argc, argv);
TestListView w;
w.show();
return a.exec();*/

QApplication a(argc, argv);
QQmlApplicationEngine engine;
QString filePath = QDir::currentPath() + "/main1.qml";
engine.load(QUrl::fromLocalFile(filePath));
return a.exec();
}


Other question : What do you prefer :
work with : Qt 5.1.1 (with QQuickView)
or with a newest version of Qt 5.5 (QQuickView or QQmlApplicationEngine)

cheers,

anda_skoa
23rd December 2015, 11:43
I would like to say : ApplicationWindow don't work with QQuickView, Am I right ???

Right, ApplicationWindow is not a QQuick Item.



In your opinion, What do you prefer :
1- * Embed QQuickView in QMainWindow
or 2- * Put directly QQmlApplicationEngine in the main, Code belowCode below

Well, first, as I've pointed out already, QQmlApplicationEngine can be used with QQuickView so that is not an either/or situation.

Secondly, it depends entirely on your requirements. If you need a QMainWindow then you need to embed the QtQuick scene in some form.
If all you need is a menu bar, then ApplicationWindow might be better since you then don't mix QtWidgets and QtQuick.

Cheers,
_

RegMe
23rd December 2015, 12:08
Thanks a lot,

I have a project part (Data management) to achieve:
Creating a fat client:
- Graphical interface with QML (then embed the QML in Visual Studio 2012)
- Treatment with C++ (Visual Studio 2012)

I would like to know your opinion:
using QQuickView or QQmlApplicationEngine
and I've two versions of Qt : Qt 5.1.1 and a newest version of Qt 5.5

I need your suggestions before introducing in project

Cheers

anda_skoa
23rd December 2015, 12:31
- Graphical interface with QML (then embed the QML in Visual Studio 2012)

What does "embed in Visual Studio" mean?



- Treatment with C++ (Visual Studio 2012)

I also don't understand what you mean with that.



using QQuickView or QQmlApplicationEngine

See previous comment



and I've two versions of Qt : Qt 5.1.1 and a newest version of Qt 5.5

Definitely the newer one.

Cheers,
_

RegMe
23rd December 2015, 16:30
What does "embed in Visual Studio" mean?
_

Just put the QML file created (by QtCreator) in visual studio 2012, 11587

then try to view QML file using Visual Studio 2012 by QQuickView or QQmlApplicationEngine



I also don't understand what you mean with that.
- Treatment with C++ (Visual Studio 2012) :
_

access to this file (main.qml), and do some operations such as put DATA in DATABASE, etc .... using (C++ VS 2012)

I wish I could hand on the message

Best regards

anda_skoa
24th December 2015, 12:33
Just put the QML file created (by QtCreator) in visual studio 2012

Ah, I see, making the file visible in the project tree.
I am sure Visual Studio has functions to add arbitrary files to a project.
In a Qt .pro file you would add them into the OTHER_FILES variable.




access to this file (main.qml), and do some operations such as put DATA in DATABASE, etc .... using (C++ VS 2012)

Do you mean using special Microsoft libraries or do you mean using Qt's database API?

Cheers,
_

RegMe
26th December 2015, 10:42
Do you mean using special Microsoft libraries or do you mean using Qt's database API?
_

Using Qt's API

As you can see, there are some ways to add or rather view QML file in Visual Studio 2012 :

1- Without changing the structure of main() in Visual Studio 2012 :

#include "testlistview.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestListView w;
w.show();
return a.exec();
}
And embed the QML file in QMainWindow : In TestListView class, we use QQuickView ....

2- With complete change of the main() in Visual Studio 2012 :
#include "testlistview.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQmlApplicationEngine engine;
QString filePath = QDir::currentPath() + "/main1.qml";
engine.load(QUrl::fromLocalFile(filePath));
return a.exec();
};

So, as I am a beginner in QML, I don't know the best way, wht's your advice for me ;) ?

Cheers,

anda_skoa
26th December 2015, 13:15
Using Qt's API

Ok, then there shouldn't be any Visual Studio specific to be concerned about.




So, as I am a beginner in QML, I don't know the best way, wht's your advice for me ;) ?

As I wrote before, that depends on your requirements.

If you need anything from QMainWindow then that is what you need to use. E.g. multiple toolbars or dock widgets.
If all you need is a menu bar and at most a single toolbar, then the ApplicationWindow approach should be fine.

In either case not in any way dependent on the usage of Visual Studio, that's all application internal.

Cheers,
_

RegMe
26th December 2015, 14:49
Thank you so much


Ok, then there shouldn't be any Visual Studio specific to be concerned about.
_
Yeah sure, We decided to use Visual Studio (MVSC compiler) because it's better than QtCreator.

What's the difference between this 2 ways:

1- Adding a new class "dataObject.h" in Visual Studio, and doing the treatment below


#include "dataObject.h"
TestListView::TestListView(QWidget *parent)
: QMainWindow(parent)
{
ui= new QQuickView;
QString filePath = QDir::currentPath() + "/main_view.qml";

ui->setSource(QUrl::fromLocalFile(filePath));

QWidget*container = QWidget::createWindowContainer();
setCentralWidget(container);

ui->setResizeMode(QQuickView::SizeRootObjectToView);
root = ui->rootObject();
ui->rootContext()->setContextProperty("Window", this);


// Treatment with C++/Qt :
// add a name (exp C++) and color (exp gray)
// in ListView created (QML file)

std::vector<std::string> dataTable;
dataTable.push_back("C++");
dataTable.push_back("Java");
dataTable.push_back("Python");

QList<QObject*> dataList;
QString color = "gray";

for(int i=0; i<dataTable.size(); i++)
{
color = (color == "gray") ? "silver" : "gray" ;
dataList.append(new DataObject(QString(dataTable.at(i).c_str()), color));

}

ui->rootContext()->setContextProperty("dataModel", QVariant::fromValue(dataList));
}

2- using qmlRegisterType : qmlRegisterType<TestClass>("test",1,0,"TestClass") ( I have not tested it yet)

What is the best ???

Cheers,

anda_skoa
26th December 2015, 16:28
Well, you probably want to use a QAbstractListModel derived class instead of a list of objectsat some point, but both approaches of making the data accessible are valid.

The first approach (setting a context object) is nice when you need access to the object from other C++ bits, the second approach (registering a custom type) allows easy creation of multple instance and object creation on demand.

So there is no iuniversally better way, it always depends.

Cheers,
_

RegMe
26th December 2015, 17:44
Well, I have not started the project yet, I wanted to know in the previous example the avantages of each method

Currently I am working on GUI with QML. e.g similar example: 11591
After typing the infos(name, age, ...), you should send the data to the web server via webservices
So you should make some class and fonctions (Treatment Part with C++/Qt : adding some class in Visual Studio 2012)

In my case, I assume that the second approach is better : registering a custom type, What's yr opinion ?


Cheers,

anda_skoa
26th December 2015, 18:07
For that kind of interface I would be using QtWidgets.

Cheers,
_

RegMe
26th December 2015, 21:22
Yeah, but there will be many interfaces, the previous interface was just an example to show you how the data should be sent via webservices,
and therefore choose the best approach : setting a context object (rootObject()) or registering a custom type (QmlRegisterType())
Still waiting yr advice :D

Cheers,

anda_skoa
27th December 2015, 12:03
As I already said: it depends on the individual use case.

Cheers,
_