Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Updating c++ data in Qml
Forum Updated to NodeBB v4.3 + New Features

Updating c++ data in Qml

Scheduled Pinned Locked Moved Solved General and Desktop
46 Posts 6 Posters 6.7k Views 2 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.
  • J J.Hilk
    27 Jul 2021, 16:05

    alright @jsulm
    alt text

    @dziko147 this does actually help

    My project compouned of 3 classes :
    1/Mainwindow
    2/backend
    /UploadCSV
    In Mainwindow :
    1/ I have a quickwidget which integrate a qml file .
    2/ I create a backend instance to get data from it and fill the QMl File

    In Backend
    1/I have the defenition of my data (getter, setter, change slots).

    In UploadCSV
    1/I have a button when it's clicked a slot in Backend will change some data .
    2/After change data I would like to display it on QML .

    I hope it's clear

    some more required information:

    • how do you expose backend to the qml context, and where (in your code)
    • name of your backend and uploadcsv instances, and where they are defined
    • does UploadCSV have its own UI components? do you mean that with "button"

    I have the high suspicion that everything is centered in and around mainwindow already and its only a matter of connecting it. But truthfully code will help, it doesn't look like you have much yet, so post as much as you can

    D Offline
    D Offline
    dziko147
    wrote on 27 Jul 2021, 16:33 last edited by
    #41

    @J-Hilk
    1/ I expose backend to qml like this :

    back= new Backend();
        m_ui->quickWidget->setVisible(true);
        m_ui->quickWidget->rootContext()->setContextProperty("cppObject",back);
        m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
        m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));
    

    2/ And UploadCSV in MainWindow just for show . I dont use UploadCSV in MainWindow

    //BTW CAN i define the connect signal of UploadCSV button here ?
    void MainWindow::on_actionUpload_CSV_File_triggered()
    {
        uploadcsv = new UploadCSV();
        uploadcsv->show();
    }
    

    3/ Yes Sure UploadCSV has a UI form .

    J 1 Reply Last reply 27 Jul 2021, 16:51
    0
    • D dziko147
      27 Jul 2021, 16:33

      @J-Hilk
      1/ I expose backend to qml like this :

      back= new Backend();
          m_ui->quickWidget->setVisible(true);
          m_ui->quickWidget->rootContext()->setContextProperty("cppObject",back);
          m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
          m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));
      

      2/ And UploadCSV in MainWindow just for show . I dont use UploadCSV in MainWindow

      //BTW CAN i define the connect signal of UploadCSV button here ?
      void MainWindow::on_actionUpload_CSV_File_triggered()
      {
          uploadcsv = new UploadCSV();
          uploadcsv->show();
      }
      

      3/ Yes Sure UploadCSV has a UI form .

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 27 Jul 2021, 16:51 last edited by
      #42

      @dziko147 alright,

      we are getting closer,

      I assume this:

      back= new Backend();
          m_ui->quickWidget->setVisible(true);
          m_ui->quickWidget->rootContext()->setContextProperty("cppObject",back);
          m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
          m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));
      

      is inside the MainWindow constructor ?

      //BTW CAN i define the connect signal of UploadCSV button here ?
      void MainWindow::on_actionUpload_CSV_File_triggered()
      {
          uploadcsv = new UploadCSV();
          uploadcsv->show();
      }
      

      The way you have it currently set up, you have to make the connect call here.

      but that won't work quite yet.

      • I assume the button you want to press, to update your backend is part of the UploadCSV ui ? Therefore ui->parambtn_2 is the button you want to press and is part of the UploadCSV ui?
      • have you yet defined a (custom)signal inside the UploadCSV class ?

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 27 Jul 2021, 17:00 last edited by J.Hilk
        #43

        If my assumptions are correct, I'm writing this before a response,

        you can/should make the following changes:

        inside UploadCSV .h add a custom signal, for example purpose we'll name it myCustomButtonClicked(),

        public:
            explicit UploadCSV(QObject *parent = nullptr);
        .....
        
        signals:
              void myCustomButtonClicked();
        
        

        inside UploadCSV.cpp constructor definition, after ui->setupUi(this), connect the button to the recently created signal:

        {
            ui->setupUi(this);
            connect(ui->parambtn_2, &QAbstractButton::clicked, this, &UploadCSV::myCustomButtonClicked); // this connects/forwards the button clicked signal to the  class signal 'myCustomButtonClicked'
        }
        

        in mainwindow constructor, instantiate the UploadCSV in the constructor and connect it to the backend:

        {
            ui->setupUi(this);
        back= new Backend();
            m_ui->quickWidget->setVisible(true);
            m_ui->quickWidget->rootContext()->setContextProperty("cppObject",back);
            m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
            m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));
        
        uploadcsv = new UploadCSV();
        connect(uploadcsv, & UploadCSV:: myCustomButtonClicked, backend, &Backend::status); //status has to be public!
        
        }
        

        your on_actionUpload_CSV_File_triggered now only contains this:

        void MainWindow::on_actionUpload_CSV_File_triggered()
        {
            uploadcsv->show();
        }
        

        let's see if that worked.

        code my contain typos, so be aware :D


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2
        • D Offline
          D Offline
          dziko147
          wrote on 27 Jul 2021, 17:16 last edited by
          #44

          @J-Hilk said in Updating c++ data in Qml:

          If my assumptions are correct, I'm writing this before a response,

          Finally it works :D :D :D
          téléchargement (1).jpg

          J 1 Reply Last reply 27 Jul 2021, 17:30
          1
          • D Offline
            D Offline
            dziko147
            wrote on 27 Jul 2021, 17:18 last edited by
            #45

            And thanks @KroMignon @jsulm thank you for the effort you made for me

            1 Reply Last reply
            2
            • D dziko147
              27 Jul 2021, 17:16

              @J-Hilk said in Updating c++ data in Qml:

              If my assumptions are correct, I'm writing this before a response,

              Finally it works :D :D :D
              téléchargement (1).jpg

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 27 Jul 2021, 17:30 last edited by
              #46

              @dziko147 WUHUH congrats!

              If you would be so kind and use the topic tool button and set the topic to solved :D


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0

              41/46

              27 Jul 2021, 16:33

              • Login

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