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. passing data from form to Mainwindow

passing data from form to Mainwindow

Scheduled Pinned Locked Moved Solved General and Desktop
form
8 Posts 4 Posters 4.9k Views
  • 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.
  • T Offline
    T Offline
    TMJJ001
    wrote on 19 Mar 2018, 22:48 last edited by
    #1

    Hi all,

    I'm getting frustated with the following problem:
    I don't succeed in passing data (QLCDnumber) from a form back to my MainWindow. I tried a lot, I don't completely understand what and how I should do it.

    So I have a form delayProg.

    In the form I have an QLCDnumber.

    Delayprog.h

    #ifndef DELAYPROG_H
    #define DELAYPROG_H
    
    #include <QDialog>
    
    
    namespace Ui {
    class delayProg;
    }
    
    class delayProg : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit delayProg(QWidget *parent = 0);
        ~delayProg();
        void on_saveButton_clicked();
    
    
    signals:
         void sendtimer(QPoint&);
    
    private slots:
        void on_dial_sliderMoved(int position);
        void on_upButton_clicked();
    
        void on_downButton_clicked();
        void on_cancelButton_clicked();
    
    private:
        Ui::delayProg *ui;
    };
    
    #endif // DELAYPROG_H
    

    delayprog.cpp

    #include "delayprog.h"
    #include "ui_delayprog.h"
    
    delayProg::delayProg(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::delayProg)
    {
        ui->setupUi(this);
        QWidget::setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    }
    
    
    
    delayProg::~delayProg()
    
    {
        delete ui;
    }
    
    
    
    void delayProg::on_dial_sliderMoved(int position)
    {
        delay = position;
        ui->delayLCD->display(delay);
    }
    
    void delayProg::on_upButton_clicked()
    {
        delay = delay++;
        ui->delayLCD->display(delay);
        ui->dial->setValue(delay);
    }
    
    void delayProg::on_downButton_clicked()
    {
        delay = delay--;
        ui->delayLCD->display(delay);
        ui->dial->setValue(delay);
    }
    
    void delayProg::on_cancelButton_clicked()
    {
        QWidget::close();
    }
    
    
    void delayProg::on_saveButton_clicked()
    {
        int time=ui->delayLCD->value();
        emit sendtimer(time);
    }
    
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    
    
    
    #include <QtGui>
    #define QD qDebug() << __FILE__ << __LINE__
    #include <QProcess>
    #include <QPixmap>
    #include <delayprog.h>
    #include <outputprog.h>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    public slots:
        void receiveTimer(QPoint& time);
    
    
    private:
        Ui::MainWindow *ui;
    
    };
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    
    {
        ui->setupUi(this);
        QWidget::showFullScreen();
        QWidget::setWindowTitle("FCS");
    
        connect( /*? what to do here, I have tried a lot but Ido something very stupid*/)
    
    
    void MainWindow::receiveTimer(QPoint &time)
    {
        ui->label->setText(time);
    }
    
    

    I hope somebody can explain me what I'm doing wrong?
    Kind regards

    A 1 Reply Last reply 19 Mar 2018, 23:23
    0
    • T TMJJ001
      19 Mar 2018, 22:48

      Hi all,

      I'm getting frustated with the following problem:
      I don't succeed in passing data (QLCDnumber) from a form back to my MainWindow. I tried a lot, I don't completely understand what and how I should do it.

      So I have a form delayProg.

      In the form I have an QLCDnumber.

      Delayprog.h

      #ifndef DELAYPROG_H
      #define DELAYPROG_H
      
      #include <QDialog>
      
      
      namespace Ui {
      class delayProg;
      }
      
      class delayProg : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit delayProg(QWidget *parent = 0);
          ~delayProg();
          void on_saveButton_clicked();
      
      
      signals:
           void sendtimer(QPoint&);
      
      private slots:
          void on_dial_sliderMoved(int position);
          void on_upButton_clicked();
      
          void on_downButton_clicked();
          void on_cancelButton_clicked();
      
      private:
          Ui::delayProg *ui;
      };
      
      #endif // DELAYPROG_H
      

      delayprog.cpp

      #include "delayprog.h"
      #include "ui_delayprog.h"
      
      delayProg::delayProg(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::delayProg)
      {
          ui->setupUi(this);
          QWidget::setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
      }
      
      
      
      delayProg::~delayProg()
      
      {
          delete ui;
      }
      
      
      
      void delayProg::on_dial_sliderMoved(int position)
      {
          delay = position;
          ui->delayLCD->display(delay);
      }
      
      void delayProg::on_upButton_clicked()
      {
          delay = delay++;
          ui->delayLCD->display(delay);
          ui->dial->setValue(delay);
      }
      
      void delayProg::on_downButton_clicked()
      {
          delay = delay--;
          ui->delayLCD->display(delay);
          ui->dial->setValue(delay);
      }
      
      void delayProg::on_cancelButton_clicked()
      {
          QWidget::close();
      }
      
      
      void delayProg::on_saveButton_clicked()
      {
          int time=ui->delayLCD->value();
          emit sendtimer(time);
      }
      
      

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      
      
      
      #include <QtGui>
      #define QD qDebug() << __FILE__ << __LINE__
      #include <QProcess>
      #include <QPixmap>
      #include <delayprog.h>
      #include <outputprog.h>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      public slots:
          void receiveTimer(QPoint& time);
      
      
      private:
          Ui::MainWindow *ui;
      
      };
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      
      {
          ui->setupUi(this);
          QWidget::showFullScreen();
          QWidget::setWindowTitle("FCS");
      
          connect( /*? what to do here, I have tried a lot but Ido something very stupid*/)
      
      
      void MainWindow::receiveTimer(QPoint &time)
      {
          ui->label->setText(time);
      }
      
      

      I hope somebody can explain me what I'm doing wrong?
      Kind regards

      A Offline
      A Offline
      ambershark
      wrote on 19 Mar 2018, 23:23 last edited by ambershark
      #2

      @TMJJ001 said in passing data from form to Mainwindow:

      void sendtimer(QPoint&);

      Hi. So you're trying to catch that signal from delayProg in your MainWindow?

      I'll just give you the code assuming that and if I'm wrong you can better explain what you want and I can give you what you need.

      Your MainWindow needs to construct your dialog and (assuming here) show it. During construction it will connect to that signal from the dialog.

      Changes in mainwindow.h:

      class delayProg;
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      public slots:
          void receiveTimer(QPoint& time);
      
      
      private:
          Ui::MainWindow *ui;
          delayProg *dpDlg;
      
      };
      

      Changes in mainwindow.cpp:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      #include "delayProg.h" // we implicitly declared the class in the header so include it here
      
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      
      {
          ui->setupUi(this);
          QWidget::showFullScreen();
          QWidget::setWindowTitle("FCS");
      
          // create your dialog
          dpDlg = new delayProg(this);
      
          // connect your signal to the mainwindow slot
          connect(dpDlg, SIGNAL(sendTimer(QPoint&)), this, SLOT(receiveTimer(QPoint&)));
      
          // show the dialog
          dpDlg->show();
      }
      

      That should do it.

      Oh one more thing if you don't want the delayProg dialog created in mainwindow then you will need to provide a pointer to it to mainwindow in order to connect the signal from it. You could do the connect in the delayProg creation and just connect to MainWindow which is easy to get from other places without passing pointers around.

      Also just a minor note on coding style... Most class names should be CamelCase... so DelayProg vs delayProg. Function names are camelCase so to me delayProg would be a function not a class. Variables are camelCase as well. Coding style is ultimately up to the coder if not working in a team but in Qt programs that is the typical style. Take that how you will. :)

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      5
      • T Offline
        T Offline
        TMJJ001
        wrote on 20 Mar 2018, 15:39 last edited by
        #3

        Hi,

        First of all, thanks for the constructive reply.

        It is not exactly what I meant. I will try to explain it with pictures:
        First of all my mainwindow opens. From the mainwindow I open the second form by pushing onto the clock symbol (mouse pointer).
        0_1521559668381_b717a862-af67-4625-8dc8-9c9583ed7e20-image.png

        Now from when the second form (delayProg) opens, I set a value in the QLCDnumber with the dial and arrows. After this is done I push the save button.

        Now comes my problem: When I push the save button I want to copy the value which is set in the QLCDnumber to the mainwindow. This value I want to store in a variable in the mainwindow to do some other things with it in the future.

        0_1521559930155_16f93c4b-b0ac-412d-a099-a3eb37d0ec33-image.png

        I hope I was able to clarify my problem.

        The second comment, I will keep this in mind for the following project. But as you can see I'm not a professional programmer. I'm making an application for an embedded system (TI AM335x with a carrierboard developed by myself) which is able to read data from the canbus of my tractor. So I'm doing this to learn about electronics and have fun ;).
        But Thanks for the advice and help.

        Kind regards

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pablo J. Rogina
          wrote on 20 Mar 2018, 16:46 last edited by
          #4

          @TMJJ001 I'd add a method to your delayProg (BTW, it's a convention not a rule, to start name of classes with uppercase and name of objects with lowercase) to retrieve the value of the LCD object, some pseudo-code:

          double DelayProg::getDelay()
          {
              return ui->qLCDNumber()->value();
          }
          

          and then in MainWindow after the dialog is close, and before the dialog object is not available anymore, grab the LCD value (you may want to have a new member in MainWindow for that).

          ...
              // create your dialog
              dpDlg = new delayProg(this);
          
              // no need to connect any signal...
          
              // show the dialog
              dpDlg->show();
          
              // dialog closed, get the value of LCD
              double delay = dpDlg->getDelay();
          
              ...
          

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          T A 2 Replies Last reply 20 Mar 2018, 21:06
          3
          • P Pablo J. Rogina
            20 Mar 2018, 16:46

            @TMJJ001 I'd add a method to your delayProg (BTW, it's a convention not a rule, to start name of classes with uppercase and name of objects with lowercase) to retrieve the value of the LCD object, some pseudo-code:

            double DelayProg::getDelay()
            {
                return ui->qLCDNumber()->value();
            }
            

            and then in MainWindow after the dialog is close, and before the dialog object is not available anymore, grab the LCD value (you may want to have a new member in MainWindow for that).

            ...
                // create your dialog
                dpDlg = new delayProg(this);
            
                // no need to connect any signal...
            
                // show the dialog
                dpDlg->show();
            
                // dialog closed, get the value of LCD
                double delay = dpDlg->getDelay();
            
                ...
            
            T Offline
            T Offline
            TMJJ001
            wrote on 20 Mar 2018, 21:06 last edited by
            #5

            @Pablo-J.-Rogina

            Hi thanks for the reply. But I wish to have it a bit different.

            I want it to go autmotically. When I push the save button in dpDlg form, I want it to put the value from the LCDnumber automatically into the variable.

            IS there a way on how to see if the dpDlg form is closed or should I do this in another way?

            Kind regards

            A 1 Reply Last reply 21 Mar 2018, 00:46
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Mar 2018, 21:12 last edited by
              #6

              Hi,

              Then emit the a signal in the slot connected to the save button so that it sends that value to whatever wants to know it.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • T TMJJ001
                20 Mar 2018, 21:06

                @Pablo-J.-Rogina

                Hi thanks for the reply. But I wish to have it a bit different.

                I want it to go autmotically. When I push the save button in dpDlg form, I want it to put the value from the LCDnumber automatically into the variable.

                IS there a way on how to see if the dpDlg form is closed or should I do this in another way?

                Kind regards

                A Offline
                A Offline
                ambershark
                wrote on 21 Mar 2018, 00:46 last edited by
                #7

                @TMJJ001 I'm confused here... you already do exactly what @SGaist suggested and it should work just fine...

                void delayProg::on_saveButton_clicked()
                {
                int time=ui->delayLCD->value();
                emit sendtimer(time);
                }

                You are emitting the signal when save is clicked, and then in your main window if you use my example to connect to the signal it would call receiveTimer with the value and set your variable.

                If the only issue is where you create and show the dialog then I covered that too... Just get a pointer to your mainwindow (multiple ways to do this) and then do the connect from your dialog, like so:

                connect(this, SIGNAL(sendTimer(QPoint&)), myMainWindowPtr, SLOT(receiveTimer(QPoint&)));

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply
                2
                • P Pablo J. Rogina
                  20 Mar 2018, 16:46

                  @TMJJ001 I'd add a method to your delayProg (BTW, it's a convention not a rule, to start name of classes with uppercase and name of objects with lowercase) to retrieve the value of the LCD object, some pseudo-code:

                  double DelayProg::getDelay()
                  {
                      return ui->qLCDNumber()->value();
                  }
                  

                  and then in MainWindow after the dialog is close, and before the dialog object is not available anymore, grab the LCD value (you may want to have a new member in MainWindow for that).

                  ...
                      // create your dialog
                      dpDlg = new delayProg(this);
                  
                      // no need to connect any signal...
                  
                      // show the dialog
                      dpDlg->show();
                  
                      // dialog closed, get the value of LCD
                      double delay = dpDlg->getDelay();
                  
                      ...
                  
                  A Offline
                  A Offline
                  ambershark
                  wrote on 21 Mar 2018, 00:48 last edited by
                  #8

                  @Pablo-J.-Rogina said in passing data from form to Mainwindow:

                  ...
                  // create your dialog
                  dpDlg = new delayProg(this);

                  // no need to connect any signal...
                  
                  // show the dialog
                  dpDlg->show();
                  
                  // dialog closed, get the value of LCD
                  double delay = dpDlg->getDelay();
                  

                  Just a side note here, you'd have to change dpDlg->show() to:

                  if (dpDlg->exec() == QDialog::Accepted)
                  {
                     delay = dpDlg->getDelay();
                  }
                  

                  I know he's not using that method but didn't want someone else to come along after and wonder why that wasn't working since show() returns immediately. :)

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  1

                  5/8

                  20 Mar 2018, 21:06

                  • Login

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