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. Trouble with calling a cpp method from qml
Forum Updated to NodeBB v4.3 + New Features

Trouble with calling a cpp method from qml

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 1.3k Views 1 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.
  • Y Offline
    Y Offline
    ysing
    wrote on last edited by
    #1

    Hi every body,

    Actually I'm having trouble with merging both qml and c++ .My little application contains two pages at the moment and my problem is:

    In the first page (main.qml) whan I created an Item class that contains all the widgets of my window nothing is displayed when I run the program.

    I will be grateful if someone helps me to solve this problem.

    main.cpp
    @#include "enter.h"
    #include <QUrl>
    #include <QQuickView>
    #include <QApplication>
    #include <QQmlComponent>
    #include <QQmlEngine>
    #include <QObject>
    #include <QQuickWindow>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    qmlRegisterType<enter>("appEntrance", 1, 0, "appEntrance");
    qmlRegisterType<HomePage>("home",1,0,"home");
    
    QQuickView view;
    view.setSource(QUrl::fromLocalFile&#40;"qrc:///main.qml"&#41;);
    view.show();
    return app.exec();
    

    }@

    enter.h

    @#ifndef ENTER_H
    #define ENTER_H

    #include <QWidget>
    #include "home.h"
    class enter :public QWidget
    {
    Q_OBJECT

    public:
    Q_INVOKABLE void openHomePage();

    };

    #endif // ENTER_H
    @

    enter.cpp

    @#include "home.h"
    #include "enter.h"

    void enter::openHomePage()
    {
    HomePage w1;

     w1.show();  
    

    }
    @

    home.h

    @#ifndef HOME_H
    #define HOME_H

    #include <QWidget>
    class HomePage : public QWidget
    {
    //I havent defined the any method yet
    };

    #endif // HOME_H
    @

    main.qml

    @
    import QtQuick 2.2
    import QtQuick.Controls 1.1
    import QtQuick.Controls.Styles 1.1
    import QtQuick.Layouts 1.1

    Item {
    id:fenetrePrincipale
    enter{
    id:appEntrance
    Rectangle {
    color: "#fbf79a"
    ColumnLayout{
    id:conteneur
    anchors.fill:parent
    ColumnLayout {
    Layout.alignment:Qt.AlignCenter
    id: columnLayout1
    spacing: 60
    Image {
    id: image1
    width: 6
    height: 0
    Layout.alignment: Qt.AlignCenter
    source: "windows8-1-food.jpg"
    }

                Button {
                    Layout.alignment: Qt.AlignCenter
                    width: 200
                    height: 100
                    text: "Entrer"
                    style: ButtonStyle {
                        background: Rectangle {
                            implicitWidth: 100
                            implicitHeight: 25
                            border.width: control.activeFocus ? 2 : 1
                            border.color: "#888"
                            radius: 14
                            gradient: Gradient {
                                GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#f4d76f" }
                                GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ddb936" }
                            }
                        }
                    }
                    clicked: appEntrance.openHomePage()
                }
    
    
    
    
            }
            }
    

    }
    }
    }

    @

    home.qml

    @import QtQuick 2.2
    import QtQuick.Controls 1.1
    import QtQuick.Controls.Styles 1.1
    import QtQuick.Layouts 1.1

    Item
    {
    HomePage{
    id:home
    ColumnLayout {
    spacing:60
    anchors.fill:parent
    Button {
    id: acceuil
    Layout.alignment: Qt.AlignVCenter
    width: 167
    height: 28
    text: qsTr("button1")
    activeFocusOnPress: false
    }

            Button {
                id: candidats
                Layout.alignment: Qt.AlignVCenter
                width: 167
                height: 28
                text: qsTr("button2")
            }
    
            Button {
                id: interimaires
                Layout.alignment: Qt.AlignVCenter
                width: 167
                height: 28
                text: qsTr("button3")
            }
    
            Button {
                id: entreprises
                Layout.alignment: Qt.AlignVCenter
                width: 167
                height: 28
                text: qsTr("button4")
            }
    
            Button {
                id: aPropos
                Layout.alignment: Qt.AlignVCenter
                width: 167
                height: 28
                text: qsTr("button 5")
            }
    
        }
    
    }
    

    }

    @

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Multiple fundamental issue here

      1. Enter type is not defined.
      2. Why do you want to inherit from QWidget ? You can'd to this to QML 2.0 implementation.
      3. Your registration is ok. But the way you are using type in QML file is wrong. You are registering as appEntrance. You need to use this in QML.

      Since there multiple fundamental issue here, I suggest you look at Integrating C++ and QML. Also check how to extend the Qml with C++. Try with simple example.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        ysing
        wrote on last edited by
        #3

        Thanks Dreehendra ,
        but can you explain to me what are my mistakes exactly because I read integrating c++ with qml and how to extend the qml with c++ .

        The code that I've written is what I've understood from the tutorial (there are some points which are still ambiguous .

        1. I defined the enter type in "enter.h" is the way in which I defined it wrong?
        2. I didn't know that i can't inherit my class from QWidget. Should I use QObject instead?
        3. about the registration actually even in the tutorial I didn't understand how it is done exactly .
          What I've understood that appEntrance is the instantiation of the class enter that I've created(you can refer to "main.qml" line number 10)

        Please let me know if I was mistaken and it will be great if you explain it better to me .

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tomme
          wrote on last edited by
          #4

          Hi ysing,

          1. Your class must have a default constructeur.
          2. Qwidget, is not a QML type. Use QObject or QquickItem.
          3. At the beginning of your qml file, you must add : import appEntrance 1.0 and import home 1.0

          I hope it helps.

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            ysing
            wrote on last edited by
            #5

            Tomme Thank you I appreciate your help . Actually since I couldn't handle mixing qml with c++ I decided to use Qt designer instead for my project , but I will try this later .

            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