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



  • 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();
    }
    

  • Moderators

    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);
    


  • @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


  • Moderators

    @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);
    


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

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

    its works tnks


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.