passing data from form to Mainwindow
-
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 -
@TMJJ001 said in passing data from form to Mainwindow:
void sendtimer(QPoint&);
Hi. So you're trying to catch that signal from
delayProg
in yourMainWindow
?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. :)
-
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).
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.
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
-
@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(); ...
-
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
-
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.
-
@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&)));
-
@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. :)