Results 1 to 18 of 18

Thread: Qml and Visual studio 2012

  1. #1
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Talking Qml and Visual studio 2012

    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
    Qt Code:
    1. TestListView::TestListView(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui= new QDeclarativeView;
    5. QString filePath = QDir::currentPath() + "/main_view.qml";
    6.  
    7. ui->setSource(QUrl::fromLocalFile(filePath));
    8. setCentralWidget(ui);
    9.  
    10. ui->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    11. root = ui->rootObject();
    12. ui->rootContext()->setContextProperty("Window", this);
    13.  
    14. std::vector<std::string> dataTable;
    15. dataTable.push_back("C++");
    16. dataTable.push_back("Java");
    17. dataTable.push_back("Python");
    18.  
    19. QList<QObject*> dataList;
    20. QString color = "gray";
    21.  
    22. for(int i=0; i<dataTable.size(); i++)
    23. {
    24. color = (color == "gray") ? "silver" : "gray" ;
    25. dataList.append(new DataObject(QString(dataTable.at(i).c_str()), color));
    26.  
    27. }
    28.  
    29. ui->rootContext()->setContextProperty("dataModel", QVariant::fromValue(dataList));
    30. }
    To copy to clipboard, switch view to plain text mode 

    But QtDeclarative works only with QtQuick 1.0
    I'm using QtQuick 2.0 so I tried to use QQuickView :
    Qt Code:
    1. ui= new QQuickView;
    2. QString filePath = QDir::currentPath() + "/main_view.qml";
    3.  
    4. ui->setSource(QUrl::fromLocalFile(filePath));
    5. setCentralWidget(ui); // QQuickView is incompatible with QWidget
    6.  
    7. ui->setResizeMode(QQuickView::SizeRootObjectToView);
    8. root = ui->rootObject();
    9. ui->rootContext()->setContextProperty("Window", this);
    To copy to clipboard, switch view to plain text mode 

    Someone can resolve this error

    best regards

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    QQuickView is its own Window.

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

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    RegMe (22nd December 2015)

  4. #3
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Thanks,

    If I want to do it with QQmlApplicationEngine, how can I embed it into a QMainWindow
    Qt Code:
    1. ui = new QQmlApplicationEngine;
    2.  
    3. QString filePath = QDir::currentPath() + "/main.qml";
    4. ui->load(QUrl::fromLocalFile(filePath));
    5.  
    6. setCentralWidget(ui); // QQmlApplicationEngine is incompatible with QWidget
    7.  
    8. root = ui->rootObjects().first();
    9. ui->rootContext()->setContextProperty("Window", this);
    To copy to clipboard, switch view to plain text mode 


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

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

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Quote Originally Posted by RegMe View Post
    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.

    Quote Originally Posted by RegMe View Post
    ApplicationWindow don't work with QQmlApplicationEngine, Am t right ???
    No, it does.

    Quote Originally Posted by RegMe View Post
    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,
    _

  6. #5
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    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

    Qt Code:
    1. #include "testlistview.h"
    2. #include <QtWidgets/QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. /*QApplication a(argc, argv);
    7. TestListView w;
    8. w.show();
    9. return a.exec();*/
    10.  
    11. QApplication a(argc, argv);
    12. QQmlApplicationEngine engine;
    13. QString filePath = QDir::currentPath() + "/main1.qml";
    14. engine.load(QUrl::fromLocalFile(filePath));
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    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,

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Quote Originally Posted by RegMe View Post
    I would like to say : ApplicationWindow don't work with QQuickView, Am I right ???
    Right, ApplicationWindow is not a QQuick Item.

    Quote Originally Posted by RegMe View Post
    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,
    _

  8. #7
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Talking Re: Qml and Visual studio 2012

    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

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Quote Originally Posted by RegMe View Post
    - Graphical interface with QML (then embed the QML in Visual Studio 2012)
    What does "embed in Visual Studio" mean?

    Quote Originally Posted by RegMe View Post
    - Treatment with C++ (Visual Studio 2012)
    I also don't understand what you mean with that.

    Quote Originally Posted by RegMe View Post
    using QQuickView or QQmlApplicationEngine
    See previous comment

    Quote Originally Posted by RegMe View Post
    and I've two versions of Qt : Qt 5.1.1 and a newest version of Qt 5.5
    Definitely the newer one.

    Cheers,
    _

  10. #9
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Quote Originally Posted by anda_skoa View Post
    What does "embed in Visual Studio" mean?
    _
    Just put the QML file created (by QtCreator) in visual studio 2012, 1.png

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


    Quote Originally Posted by anda_skoa View Post
    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
    Last edited by RegMe; 23rd December 2015 at 17:39.

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Quote Originally Posted by RegMe View Post
    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.


    Quote Originally Posted by RegMe View Post
    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,
    _

  12. #11
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Quote Originally Posted by anda_skoa View Post
    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 :
    Qt Code:
    1. #include "testlistview.h"
    2. #include <QtWidgets/QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. TestListView w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    And embed the QML file in QMainWindow : In TestListView class, we use QQuickView ....

    2- With complete change of the main() in Visual Studio 2012 :
    Qt Code:
    1. #include "testlistview.h"
    2. #include <QtWidgets/QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. QQmlApplicationEngine engine;
    8. QString filePath = QDir::currentPath() + "/main1.qml";
    9. engine.load(QUrl::fromLocalFile(filePath));
    10. return a.exec();
    11. };
    To copy to clipboard, switch view to plain text mode 

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

    Cheers,
    Last edited by RegMe; 26th December 2015 at 12:14.

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Quote Originally Posted by RegMe View Post
    Using Qt's API
    Ok, then there shouldn't be any Visual Studio specific to be concerned about.


    Quote Originally Posted by RegMe View Post
    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,
    _

  14. #13
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    Thank you so much

    Quote Originally Posted by anda_skoa View Post
    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

    Qt Code:
    1. #include "dataObject.h"
    2. TestListView::TestListView(QWidget *parent)
    3. : QMainWindow(parent)
    4. {
    5. ui= new QQuickView;
    6. QString filePath = QDir::currentPath() + "/main_view.qml";
    7.  
    8. ui->setSource(QUrl::fromLocalFile(filePath));
    9.  
    10. QWidget*container = QWidget::createWindowContainer();
    11. setCentralWidget(container);
    12.  
    13. ui->setResizeMode(QQuickView::SizeRootObjectToView);
    14. root = ui->rootObject();
    15. ui->rootContext()->setContextProperty("Window", this);
    16.  
    17.  
    18. // Treatment with C++/Qt :
    19. // add a name (exp C++) and color (exp gray)
    20. // in [B]ListView[/B] created (QML file)
    21.  
    22. std::vector<std::string> dataTable;
    23. dataTable.push_back("C++");
    24. dataTable.push_back("Java");
    25. dataTable.push_back("Python");
    26.  
    27. QList<QObject*> dataList;
    28. QString color = "gray";
    29.  
    30. for(int i=0; i<dataTable.size(); i++)
    31. {
    32. color = (color == "gray") ? "silver" : "gray" ;
    33. dataList.append(new DataObject(QString(dataTable.at(i).c_str()), color));
    34.  
    35. }
    36.  
    37. ui->rootContext()->setContextProperty("dataModel", QVariant::fromValue(dataList));
    38. }
    To copy to clipboard, switch view to plain text mode 

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

    What is the best ???

    Cheers,

  15. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    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,
    _

  16. #15
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    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: 2.jpg
    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,
    Last edited by RegMe; 26th December 2015 at 19:02.

  17. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    For that kind of interface I would be using QtWidgets.

    Cheers,
    _

  18. #17
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    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

    Cheers,

  19. #18
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml and Visual studio 2012

    As I already said: it depends on the individual use case.

    Cheers,
    _

Similar Threads

  1. Qt Creator QT add-in for Visual Studio 2012
    By tesmai4 in forum Qt Tools
    Replies: 0
    Last Post: 29th January 2014, 15:41
  2. Qt 5.1.1 not recognized by Visual Studio Add-in 2012
    By cdarwin in forum Qt Programming
    Replies: 0
    Last Post: 5th June 2013, 13:05
  3. Help regarding QT plugins for Visual studio 2012
    By shah_27 in forum Qt Programming
    Replies: 0
    Last Post: 9th January 2013, 10:42
  4. Build qt 5 with Visual Studio 2012
    By Edder in forum Installation and Deployment
    Replies: 4
    Last Post: 28th December 2012, 11:16
  5. Qt 4.8.3 with Visual Studio 2012 does not compile the webkit
    By dennis81 in forum Installation and Deployment
    Replies: 1
    Last Post: 4th December 2012, 03:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.