Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. `main.qml` loaded only after the execution of a function called after
Forum Updated to NodeBB v4.3 + New Features

`main.qml` loaded only after the execution of a function called after

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 2 Posters 721 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.
  • A Offline
    A Offline
    amina
    wrote on 3 Mar 2021, 15:34 last edited by amina 3 Mar 2021, 16:05
    #1

    Hello,
    I cross compiled Qt5.14.2 for my raspberry pi 4 and I am trying to create a Qt Quick application
    so this is what my mail.cpp file looks like

    include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include<QDebug>
    #include <QQmlEngine>
    #include<QQmlContext>
    #include"contacteur.h"
    #include"capteur.h"
    #include<QApplication>
    #include <QtWidgets>
    #include <QQuickView>
    #include <QtQml>
    #include<QTimer>
    #include<QObject>
    #include<cstdlib>
    
    int main(int argc, char *argv[])
    {
    
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QScopedPointer<Contacteur>contacteur (new Contacteur);
         QApplication app(argc, argv);
        QQmlApplicationEngine engine;
    
        Capteur capteur;
        qmlRegisterType<Capteur>("capteur", 1, 0, "Capteur");
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        qDebug()<<"avant l'exution de la fct frein=" <<capteur.getFrein();
        capteur.changer_etat();
        qDebug()<<"apres l'exution de la fct frein="<< capteur.getFrein();
    
        engine.rootContext()->setContextProperty("contacteur" ,contacteur.data());   
    
        return app.exec();
    }
    

    I have created a new item with c++ class called Capteur and integrated it to QML.
    I am working on interfacing C++ class with the QML so for that I created and application called 'changer_etat()' this fuction containes a sleep fonction from QThread and changes one of the attributes off this class

    can someone please explain to me why is my main.qml loaded only after the execution of capteur.changer_etat() ends knowing that in the main.cpp the load function is on top of the changer_etat function?
    Thank you for helping

    this is what my capteur.h looks like

    #ifndef CAPTEUR_H
    #define CAPTEUR_H
    
    #include <QMainWindow>
    #include <QObject>
    #include <QWidget>
    #include<QTimer>
    
    class Capteur : public QMainWindow
    {
        Q_OBJECT
        Q_PROPERTY(int frein READ getFrein WRITE setFrein NOTIFY freinChanged)
    public:
        explicit Capteur(QWidget *parent = nullptr);
        int getFrein();
    void changer_etat();
    private:
        int m_frein;
        QTimer *timerFrein;
    private slots :
         void onTimeoutfrein ();/
    signals:
         void freinChanged(int);
    public slots:
         void setFrein(int T);
    };
    #endif // CAPTEUR_H
    

    this is what my capteur.cpp looks like

    #include "capteur.h"
    #include<QDebug>
    #include<cstdlib>
    #include<QThread>
    
    Capteur::Capteur(QWidget *parent) : QMainWindow(parent)
    {    timerFrein =new QTimer (this);
        timerFrein->setTimerType(Qt::PreciseTimer); 
        connect (timerFrein,SIGNAL(timeout()),this,SLOT(onTimeoutfrein()));
        timerFrein->start(3000);
        m_frein=1;
    }
    void Capteur::setFrein(int t)
    {    m_frein=t;
        emit freinChanged(t); }
    
    int Capteur::getFrein()
    {    return m_frein; }
    
    void Capteur::changer_etat()
    {     QThread::msleep(6000);
            if (getFrein()==0)
        {   setFrein(1);
                }
        else
               setFrein(0);
    }
    
    void Capteur::onTimeoutfrein()
    {  
       }
    
    

    and this is the part of my main.qml
    where I try to interface between both qml and C++

    Window {
        id: root
        visible: true
        width: 1024
        height: 600
        title: qsTr("Dashboard")
        color: "black"
    
        Capteur{
            id:valeur_capteurs
        }
    Rectangle{
            id:rect_frein_main
            x: 555
            y: 184
            width: 39
            height: 24
            color: "#00000000" // transparent
            opacity: 1.0 // frein à main levé
            Image {
                id: frein_main
                width: rect_frein_main.width
                height: rect_frein_main.height
                anchors.centerIn: rect_frein_main
                source: "images/b8.png"
            }
            states: [
                State {
                    name: "frein_main_levé"
                    when :(valeur_capteurs.frein == 1)
                    PropertyChanges {target: rect_frein_main; opacity:1.0}
                },
                State {
                    name: "frein_main_bas"
                    when :(valeur_capteurs.frein == 0)
                    PropertyChanges {target: rect_frein_main ; opacity:0.0 }
                }]
    }
    }
    

    It has been a while that I am trying to make it work and to see that qml and c++ are interacting, I also tryed to change the value of the attribut in the slot which is connected to the signal timeout of the timer but couldn't work .

    If you know please tell me what am I doing wrong?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 4 Mar 2021, 06:52 last edited by
      #2

      Why do you say that main.qml is loaded after changer_etat()? How have you established it? I don't see how, from this code, you could reach such conclusion.

      In general, UI is shown after you call app.exec() and the event loop gets started. That might give an impression of things being executed out of order, but I fail to see how it applies to your case here.

      BTW. You are creating Capteur object twice: one (unused) in C++ and one (used) in QML. These 2 objects are completely separate and will contain different values. You call changer_etat() on object which you do not use in QML.

      (Z(:^

      1 Reply Last reply
      4
      • A Offline
        A Offline
        amina
        wrote on 4 Mar 2021, 07:38 last edited by amina 3 Apr 2021, 07:52
        #3

        @amina said in &#x60;main.qml&#x60; loaded only after the execution of a function called after:

        QThread::msleep(6000);

        In changer_etat there is QThread::msleep(6000); in the beggining I set it 1000 so just 1 s and I said that I will see the difference when executing the code because after this 1s in qml the opacity of the rectangle rect_frein_main has to change from 1.0 to 0.0 if every thing works fine , but no I didn't see any difference so I started to increase the sleep time .. I put it 6000 so 6s and after that I noticed that my application toked more time to show up.
        I added some qdebug in changer_etat() and in main .cppbefore and after calling this function and I noticed that my application shows up only after all these qdebug..

        Thank you for clarifying that, I will try to remove one of the two object maybe this is causing this problem

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 4 Mar 2021, 07:54 last edited by
          #4

          QThread::msleep(6000); you run this in main thread, so it will block your entire application for 6 seconds.

          (Z(:^

          1 Reply Last reply
          4
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 4 Mar 2021, 07:54 last edited by
            #5

            If you want to delay some action, use QTimer::singleShot() instead.

            (Z(:^

            1 Reply Last reply
            4
            • A Offline
              A Offline
              amina
              wrote on 4 Mar 2021, 08:29 last edited by amina 3 Apr 2021, 08:34
              #6

              Thank you so much ! I am new to Qt

              I also tried this and I didn't now why my counter was incremented 2 times every time out ! now that you clarified that I had 2 objects everything make sense !Thank you

              int counter=0;
              void Capteur::onTimeoutfrein()
              {   counter++;
                  qDebug() <<"time is up"<<counter;/
                  if (getFrein()==0)
                  {
                      setFrein(1);
                      qDebug() <<"le frein à main est levé" << getFrein();
                  }
                  else 
              {setFrein(0);
              
                  qDebug() <<"le frein à main est bas" << getFrein();
              }
              }
              
              1 Reply Last reply
              1

              1/6

              3 Mar 2021, 15:34

              • Login

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