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. Error when using C++ class Linked to QML
Qt 6.11 is out! See what's new in the release blog

Error when using C++ class Linked to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 2.4k 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.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by A Former User
    #1

    iam using Qt Quick Control 2 in Qt 5.7 iam try to connect a c++ class to qml i use this code in main

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlEngine>
    #include <QQmlContext>
    #include "mainwindow.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        qputenv("QT_QUICK_CONTROLS_STYLE", "Flat");
        
        QScopedPointer<MainWindow> MyClass(new MainWindow);
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/Main.qml")));
        engine.rootContext()->setContextProperty("MyClass",MyClass.data());
    
        return app.exec();
    }
    
    

    the class i use
    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QObject>
    #include <QPoint>
    
    
    class MainWindow : public QObject
    {
        Q_OBJECT
    public:
        explicit MainWindow(QObject *parent = 0);
        Q_INVOKABLE void check();
    };
    
    #endif // MAINWINDOW_H
    
    

    and .cpp

    #include "mainwindow.h"
    #include <QDebug>
    MainWindow::MainWindow(QObject *parent) :
        QObject(parent)
    {
    
    }
    
    void MainWindow::check()
    {
        qDebug() << "hello";
    }
    
    

    i can't use the MyClass in qml when i use it i get error on the line i use in qml file

    qml

    import QtQuick 2.4
    import QtQuick 2.5
    import QtQuick.Controls 2.0
    import QtQuick.Dialogs 1.2
    import QtQuick.Layouts 1.1
    import QtQuick.Window 2.0
    
    ApplicationWindow {
        id: window
        width: 480
        height: 480
        title: "Flat Example"
        visible: true
    
        MainForm {
            id: amr
            visible: true
            text3.x: amr.width / 2 - 60
            MyClass.check() // i got error here red line under this 
        }
    }
    
    

    when i try to run i got

    qrc:/Main.qml:19 Syntax error
    
    

    so what i do wrong ?
    Thanks in Advance

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! "MyClass.check()" is a function call. But that's not allowed there.

      AmrCoderA 1 Reply Last reply
      2
      • ? A Former User

        Hi! "MyClass.check()" is a function call. But that's not allowed there.

        AmrCoderA Offline
        AmrCoderA Offline
        AmrCoder
        wrote on last edited by
        #3

        @Wieland what should i do to use this function from c++ class ?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          That depends. When do you want to call the function?

          AmrCoderA 1 Reply Last reply
          2
          • ? A Former User

            That depends. When do you want to call the function?

            AmrCoderA Offline
            AmrCoderA Offline
            AmrCoder
            wrote on last edited by
            #5

            @Wieland Thank you it solve i add it to button function

                    button1.onClicked: {
                        MyClass.check()
                    }
            
            ? 1 Reply Last reply
            1
            • AmrCoderA AmrCoder

              @Wieland Thank you it solve i add it to button function

                      button1.onClicked: {
                          MyClass.check()
                      }
              
              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @AmrCoder

              First post.

              You stole my Class. Amazingly this is word for word for what I'm doing at the moment with the exception of the qputenv(). Must check that! And I had the same problem and solved it in the same manner. I've just returned to programming after several years out of the game,

              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