Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. first qml example
Qt 6.11 is out! See what's new in the release blog

first qml example

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i'm just getting started with qml and qtquick.
    the first example i wrote is:

    // main.qml
    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
    	visible: true
    	width: 640
    	height: 480
    	title: qsTr("Hello World")
    }
    
    // qml.qrc
    <RCC>
        <qresource prefix="/">
            <file>main.qml</file>
        </qresource>
    </RCC>
    
    // main.cpp
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
    	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
    	QGuiApplication app(argc, argv);
    
    	QQmlApplicationEngine engine;
    	engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    	if (engine.rootObjects().isEmpty())
    		return -1;
    
    	return app.exec();
    }
    

    i changed main.qml to this and rebuild the project and no widget is showing up:

    import QtQuick 2.9
    
    Rectangle {
    	width: 360
    	height: 360
    	Text {
    		anchors.centerIn: parent
    		text: "Hello World"
    	}
    	MouseArea {
    		anchors.fill: parent
    		onClicked: {
    			Qt.quit();
    		}		
    	}
    }
    

    what's the problem?

    1 Reply Last reply
    0
    • oria66O Offline
      oria66O Offline
      oria66
      wrote on last edited by oria66
      #2

      Hello @user4592357.

      Use your Rectangle component code inside the Window component. Then fit the size of the window optionally. It should work.
      you must include import QtQuick.Window 2.2 or you can use the ApplicationWindow component. Refer to Qt help to get a description of these main components.

      If you are a QML beginner I recommend you Qt Cadaques book. It is the best tutorial about qml.

      Good luck

      The truth is out there

      U 1 Reply Last reply
      1
      • oria66O oria66

        Hello @user4592357.

        Use your Rectangle component code inside the Window component. Then fit the size of the window optionally. It should work.
        you must include import QtQuick.Window 2.2 or you can use the ApplicationWindow component. Refer to Qt help to get a description of these main components.

        If you are a QML beginner I recommend you Qt Cadaques book. It is the best tutorial about qml.

        Good luck

        U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        thanks. so it's like QMainWindow or QWidget.
        i'm using that book for reference, also some videos.

        and there's one thing that i find uncomfortable. it's that i'm doing clean build after every change in .qml file (because i use in inside .qrc ).
        how else can i use the .qml file so that i don't have to rebuild every time?

        EDIT: found out it's not possible :( https://forum.qt.io/topic/87085/avoid-manual-rebuild-after-modifying-qml-files-in-qt-creator

        1 Reply Last reply
        0
        • oria66O Offline
          oria66O Offline
          oria66
          wrote on last edited by
          #4

          @user4592357 Hello. I use the qml components inside .qrc too, but I had not encountered with this problem. What version of Qt, compiler and OS are you using? When I change something in my components, I only compile and the changes are included in the new compilation. Can you provide your example via Github or Gitlab?

          The truth is out there

          U 1 Reply Last reply
          0
          • jiancaiyangJ Offline
            jiancaiyangJ Offline
            jiancaiyang
            wrote on last edited by
            #5

            Do change your data in QML document only.
            QML is not used that way.
            Maybe you need a fully-functional IDE source code to know how it works.

            我们自己的论坛:http://qtdream.com
            擅长三维角色仿真动画。

            1 Reply Last reply
            0
            • oria66O oria66

              @user4592357 Hello. I use the qml components inside .qrc too, but I had not encountered with this problem. What version of Qt, compiler and OS are you using? When I change something in my components, I only compile and the changes are included in the new compilation. Can you provide your example via Github or Gitlab?

              U Offline
              U Offline
              user4592357
              wrote on last edited by
              #6

              @oria66
              here's the code: https://paste.ee/p/plf9D

              i'm on windows, qt 5.10.0, vs2017 compiler.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved