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.0k 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.
  • D Offline
    D Offline
    dziko147
    wrote on last edited by
    #24

    @KroMignon if I understand you . I should create an MainWindow Instance in UploadCSV.cpp then I call the back defined in MainWindow ?
    Is that ?

    KroMignonK jsulmJ 2 Replies Last reply
    0
    • D dziko147

      @KroMignon if I understand you . I should create an MainWindow Instance in UploadCSV.cpp then I call the back defined in MainWindow ?
      Is that ?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #25

      @dziko147 said in Updating c++ data in Qml:

      if I understand you . I should create an MainWindow Instance in UploadCSV.cpp then I call the back defined in MainWindow ?
      Is that ?

      No, you don't understand anything, and that is very bad :(
      You have to connect the click signal from the UploadCSV instance to the back variable (which should be a class member ==> https://www.learncpp.com/cpp-tutorial/classes-and-class-members/) of the MainWindow instance.

      And NOT create a new instance.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      1
      • D dziko147

        @KroMignon if I understand you . I should create an MainWindow Instance in UploadCSV.cpp then I call the back defined in MainWindow ?
        Is that ?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #26

        @dziko147 said in Updating c++ data in Qml:

        Is that ?

        No.
        Why should UploadCSV create main window?!
        Please read more carefully.

        You should really learn what instances are! Two different instance are exactly that: two different instances/objects, they are NOT same.
        If you create two instances of a class (lets name them A and B), then connect a slot from A to a signal, then do you expect that the slot in B will also be called if the signal is emitted? Yes or No?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        0
        • jsulmJ jsulm

          @dziko147 said in Updating c++ data in Qml:

          Is that ?

          No.
          Why should UploadCSV create main window?!
          Please read more carefully.

          You should really learn what instances are! Two different instance are exactly that: two different instances/objects, they are NOT same.
          If you create two instances of a class (lets name them A and B), then connect a slot from A to a signal, then do you expect that the slot in B will also be called if the signal is emitted? Yes or No?

          D Offline
          D Offline
          dziko147
          wrote on last edited by dziko147
          #27

          @jsulm Sure No .

          @KroMignon If I don't create MainWindow instance, how can i access to mainwindow variable from UploadCSV.

          MainWindow *mymain=new MainWindow();
              connect(ui->parambtn_2, SIGNAL(clicked()),mymain->back, SLOT(status()),Qt::QueuedConnection);
          
          

          this is what I think .
          Correct me please :)

          jsulmJ 1 Reply Last reply
          0
          • D dziko147

            @jsulm Sure No .

            @KroMignon If I don't create MainWindow instance, how can i access to mainwindow variable from UploadCSV.

            MainWindow *mymain=new MainWindow();
                connect(ui->parambtn_2, SIGNAL(clicked()),mymain->back, SLOT(status()),Qt::QueuedConnection);
            
            

            this is what I think .
            Correct me please :)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #28

            @dziko147 said in Updating c++ data in Qml:

            If I don't create MainWindow instance, how can i access to mainwindow variable from UploadCSV.

            Don't create a SECOND MainWindow instance! Use the one you already have (you probably create it in main.cpp, right?)...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            0
            • jsulmJ jsulm

              @dziko147 said in Updating c++ data in Qml:

              If I don't create MainWindow instance, how can i access to mainwindow variable from UploadCSV.

              Don't create a SECOND MainWindow instance! Use the one you already have (you probably create it in main.cpp, right?)...

              D Offline
              D Offline
              dziko147
              wrote on last edited by
              #29

              @jsulm Yes sure . so I use it ?

              jsulmJ 1 Reply Last reply
              0
              • D dziko147

                @jsulm Yes sure . so I use it ?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #30

                @dziko147 said in Updating c++ data in Qml:

                so I use it ?

                Yes, the one you already have

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dziko147
                  wrote on last edited by
                  #31

                  @KroMignon Is this code correct ??

                  I create a class member :

                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  public:
                      Backend *back;
                  

                  then in uploadCSV.cpp

                    connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);
                  
                  

                  But still doesn't work !

                  jsulmJ 1 Reply Last reply
                  0
                  • D dziko147

                    @KroMignon Is this code correct ??

                    I create a class member :

                    class MainWindow : public QMainWindow
                    {
                        Q_OBJECT
                    public:
                        Backend *back;
                    

                    then in uploadCSV.cpp

                      connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);
                    
                    

                    But still doesn't work !

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #32

                    @dziko147 said in Updating c++ data in Qml:

                    connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);

                    You're creating a new MainWindow instance in the connect.
                    I give up...

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    D 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @dziko147 said in Updating c++ data in Qml:

                      connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);

                      You're creating a new MainWindow instance in the connect.
                      I give up...

                      D Offline
                      D Offline
                      dziko147
                      wrote on last edited by
                      #33

                      @jsulm Sorry :( :( :(
                      why you don't/want give an example ?
                      I can understand you with example :)

                      jsulmJ 1 Reply Last reply
                      0
                      • D dziko147

                        @jsulm Sorry :( :( :(
                        why you don't/want give an example ?
                        I can understand you with example :)

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #34

                        @dziko147 You should really learn C++ basics first before you continue with Qt.
                        This is now second thread from you where people are trying to explain to you what you are doing wrong. Your problem is not Qt but missing C++ knowledge.

                        For example: why did you write "MainWindow()" bellow? Do you know what MainWindow() does? And how does this relate to what I wrote (use existing MainWindow instance)? You again create a new MainWindow instance, even if you was told many times to not to do so!

                        connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        D 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @dziko147 You should really learn C++ basics first before you continue with Qt.
                          This is now second thread from you where people are trying to explain to you what you are doing wrong. Your problem is not Qt but missing C++ knowledge.

                          For example: why did you write "MainWindow()" bellow? Do you know what MainWindow() does? And how does this relate to what I wrote (use existing MainWindow instance)? You again create a new MainWindow instance, even if you was told many times to not to do so!

                          connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);
                          
                          D Offline
                          D Offline
                          dziko147
                          wrote on last edited by
                          #35

                          @jsulm ok told me how to do this . I am a student in first year . So i am already learning :/ .
                          Yes I understand that i create a second instance but i haven't another choice . the only solution for me is to ask you . :(

                          jsulmJ 1 Reply Last reply
                          0
                          • D dziko147

                            @jsulm ok told me how to do this . I am a student in first year . So i am already learning :/ .
                            Yes I understand that i create a second instance but i haven't another choice . the only solution for me is to ask you . :(

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #36

                            @dziko147 said in Updating c++ data in Qml:

                            Yes I understand that i create a second instance

                            You need to understand that it is quite disappointing for others to repeat the same thing again and again and again and then see that you are still doing EXACTLY the same mistake even if you know that it is wrong! So, why do you again post code which is wrong if you know that it is wrong?

                            I can't tell you how exactly you need to do it because I don't have your code and so don't know how it is structured...
                            From architectural point of view it is strange why uploadCSV should connect a signal to a slot in a member variable in another class? uploadCSV should not even know that MainWindow has a member called "back". This was already pointed out by @SGaist

                            Where is the uploadCSV instance created? Does MainWindow create it?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • D Offline
                              D Offline
                              dziko147
                              wrote on last edited by
                              #37

                              @jsulm this is my situation.

                              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

                              jsulmJ 1 Reply Last reply
                              0
                              • D dziko147

                                @jsulm this is my situation.

                                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

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #38

                                @dziko147 said in Updating c++ data in Qml:

                                I hope it's clear

                                No, you did not answer my question, which is actually quite clear...

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  dziko147
                                  wrote on last edited by
                                  #39

                                  I give uuuuuuuuuuuuuup

                                  1 Reply Last reply
                                  0
                                  • J.HilkJ Offline
                                    J.HilkJ Offline
                                    J.Hilk
                                    Moderators
                                    wrote on last edited by
                                    #40

                                    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


                                    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.

                                    D 1 Reply Last reply
                                    2
                                    • J.HilkJ J.Hilk

                                      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 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.HilkJ 1 Reply Last reply
                                      0
                                      • D dziko147

                                        @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.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on 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.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on 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

                                          • Login

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