Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. how can I use c ++ not qt QUICK / QML?
Forum Updated to NodeBB v4.3 + New Features

how can I use c ++ not qt QUICK / QML?

Scheduled Pinned Locked Moved Solved Language Bindings
5 Posts 2 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.
  • N Offline
    N Offline
    Nathan Miguel
    wrote on last edited by
    #1

    I'm doing a little test, and was wondering how can I pass this string to the "text" in qml

    QML

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import com.company.cpuname 1.0
    
    Text {
                id: element
                x: 41
                y: 71
                width: 175
                height: 39
                text: cpuname.getcpu()
                style: Text.Sunken
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                fontSizeMode: Text.FixedSize
                font.pixelSize: 14
            }
    

    Main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "cpuname.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        qmlRegisterType<cpuname>("com.company.cpuname",1,0,"CpuName");
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    cpuname.h/cpuname.cpp

    #ifndef CPUNAME_H
    #define CPUNAME_H
    
    #include <QObject>
    
    Q_PROPERTY ( QString theChange READ getTheChange NOTIFY changeOfStatus )
    
    class cpuname : public QObject
    {
        Q_OBJECT
    public:
        explicit cpuname(QObject *parent = nullptr);
        Q_INVOKABLE void getcpu(QString namecpu);
    
    signals:
    
    
    public slots:
    
    private:
        QString namecpu;
    };
    
    #endif // CPUNAME_H
    

    -------------------------------------------

    #include "cpuname.h"
    #include <QProcess>
    #include <QSysInfo>
    #include <QDebug>
    
    cpuname::cpuname(QObject *parent) : QObject(parent)
    {
    
    }
    
    void cpuname::getcpu(QString namecpu)
    {
        namecpu = QSysInfo::kernelType();
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Is there any question here that you wanted to ask?

      In the code you pasted, you have registered cpuname as a QML type but you never create the object anywhere. So to make that work you need to:

      Text {
        CpuName { id: cpuname }
        text: cpuname.getcpu()
      }
      

      Or, a simpler solution is to add cpuname object as context property to your engine:

      auto myCpu = cpuname();
      engine.rootContext()->setContextProperty("cpuname", &cpuname);
      

      (Z(:^

      N sierdzioS 2 Replies Last reply
      2
      • sierdzioS sierdzio

        Is there any question here that you wanted to ask?

        In the code you pasted, you have registered cpuname as a QML type but you never create the object anywhere. So to make that work you need to:

        Text {
          CpuName { id: cpuname }
          text: cpuname.getcpu()
        }
        

        Or, a simpler solution is to add cpuname object as context property to your engine:

        auto myCpu = cpuname();
        engine.rootContext()->setContextProperty("cpuname", &cpuname);
        
        N Offline
        N Offline
        Nathan Miguel
        wrote on last edited by Nathan Miguel
        #3

        @sierdzio said in how can I use c ++ not qt QUICK / QML?:

        Is there any question here that you wanted to ask?

        Reformulating my question, I want to know how I can use c ++ in qt qml, because I saw several documents and videos and none worked at random, I'm trying to pass the kernel(Windows OR Linux) from the pc to the qml as a test, but it is not working .

        I added as you said in "main.cpp" but I get this result on the output

        Main.cpp

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        #include "cpuname.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            qmlRegisterType<cpuname>("com.company.cpuname",1,0,"CpuName");
            cpuname CpuName;
            const QUrl url(QStringLiteral("qrc:/main.qml"));
            QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                             &app, [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
            }, Qt::QueuedConnection);
            engine.load(url);
            engine.rootContext()->setContextProperty("cpuname", &CpuName);
        
            return app.exec();
        }
        

        OUTPUT

        15:26:35: Starting E:\Git\Denoiser-Script\QML_C++\build-Denoiser-Desktop_Qt_5_12_3_MinGW_64_bit2-Debug\debug\Denoiser.exe ...
        QML debugging is enabled. Only use this in a safe environment.
        qrc:/main.qml:33: Error: Insufficient arguments
        

        0_1558376886967_94abc971-f8d6-4cb7-9628-e833adb2eb34-image.png

        1 Reply Last reply
        0
        • sierdzioS sierdzio

          Is there any question here that you wanted to ask?

          In the code you pasted, you have registered cpuname as a QML type but you never create the object anywhere. So to make that work you need to:

          Text {
            CpuName { id: cpuname }
            text: cpuname.getcpu()
          }
          

          Or, a simpler solution is to add cpuname object as context property to your engine:

          auto myCpu = cpuname();
          engine.rootContext()->setContextProperty("cpuname", &cpuname);
          
          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @sierdzio said in how can I use c ++ not qt QUICK / QML?:

          Or,

          I've said OR. Do not implement both these solutions at once, because cpuname identifiers will clash...

          Anyway, getting to your error. Your c++ code expects a string argument cpuname::getcpu(QString namecpu) but you are not providing any in QML text: cpuname.getcpu().

          Which brings me to another bug:

          Your getcpu method does not return anything! So QML will never get any data from it!

          Your method should look more like:

          QString cpuname::getcpu() const
          {
              return QSysInfo::kernelType();
          }
          

          Lastly, the root context should be set before the QML code is loaded.

          engine.rootContext()->setContextProperty("cpuname", &CpuName);
          engine.load(url);
          

          (Z(:^

          N 1 Reply Last reply
          2
          • sierdzioS sierdzio

            @sierdzio said in how can I use c ++ not qt QUICK / QML?:

            Or,

            I've said OR. Do not implement both these solutions at once, because cpuname identifiers will clash...

            Anyway, getting to your error. Your c++ code expects a string argument cpuname::getcpu(QString namecpu) but you are not providing any in QML text: cpuname.getcpu().

            Which brings me to another bug:

            Your getcpu method does not return anything! So QML will never get any data from it!

            Your method should look more like:

            QString cpuname::getcpu() const
            {
                return QSysInfo::kernelType();
            }
            

            Lastly, the root context should be set before the QML code is loaded.

            engine.rootContext()->setContextProperty("cpuname", &CpuName);
            engine.load(url);
            
            N Offline
            N Offline
            Nathan Miguel
            wrote on last edited by
            #5

            @sierdzio said in how can I use c ++ not qt QUICK / QML?:

            engine.rootContext()->setContextProperty("cpuname", &CpuName);

            its works tnks

            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