Simple qt help with data between forms



  • Hello,

    I am new to QT but not C++, I am trying to learn about slots and signals but am stuck on sending data between forms. I have done a lot of looking on the Internet but can't seem to make a connection (no pun intended) in my mind. Because its easier for me to learn this way, it would be helpful for me to get my code working and study and play with that.

    So I have two forms mainwindow.cpp and secdialog.cpp. On the main window a button (pushbutton) is clicked which opens the form created by secdialog.cpp. In secdialog, I have a lineEdit box and a pushbutton. In a nut shell I want to be able to have the data thats entered in to the lineEdit box transferred over to a string value in mainwindow.cpp class.

    I have seen some example on the Internet but none dealing with just storing the data in to a data type.

    Maybe someone could please help me?

    Thanks

    main.cpp
    [code]
    #include "mainwindow.h"
    #include <QApplication>
    #include <unistd.h>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    [/code]

    mainwindow.cpp

    [code]
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "secdialog.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    SecDialog blah;
    blah.setModal(true);
    blah.exec();

    }
    [/code]

    secdialog.cpp

    [code]
    #include "secdialog.h"
    #include "ui_secdialog.h"
    #include <QString>
    using namespace std;

    SecDialog::SecDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SecDialog)
    {
    ui->setupUi(this);

    }

    SecDialog::~SecDialog()
    {
    delete ui;

    }

    void SecDialog::on_pushButton_clicked()
    {
    // data = ui->lineEdit->text(); i want to do something like this! but i know i can't

    }
    [/code]

    mainwindow.h

    [code]
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include"secdialog.h"
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:

    void on_pushButton_clicked();
    

    private:
    Ui::MainWindow *ui;
    SecDialog *blah;
    QString data;

    };

    #endif // MAINWINDOW_H

    [/code]

    secdialog.h
    [code]
    #ifndef SECDIALOG_H
    #define SECDIALOG_H

    #include <QDialog>

    namespace Ui {
    class SecDialog;
    }

    class SecDialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit SecDialog(QWidget *parent = 0);
    ~SecDialog();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::SecDialog *ui;
    QString i;
    };

    #endif // SECDIALOG_H

    [/code]



  • hi, is there a way you could be a little more specific like using my situation specific.

    Thanks!



  • secdialog.h
    @
    class SecDialog : public QDialog
    {
    public:
    ...
    QString data() const;
    ...
    }
    @
    secdialog.cpp
    @
    ...
    QString SecDialog::data() const
    {
    return ui->lineEdit->text();
    }
    ...
    @
    mainwindow.cpp
    @
    ...
    void MainWindow::on_pushButton_clicked()
    {
    SecDialog blah;
    blah.setModal(true);
    blah.exec();
    qDebug() << blah.data();
    }
    ...
    @



  • thank you,
    for some reason i was under the impression calling the new form was blocking and nothing could be ran after that until execution was completed.



  • You are right. I did not realize that you need the data while the dialog is open.
    In this case you need to use signal/slot mechanism to send data to mainwindow.
    I don't have an example right now. Will update post later.



  • That would be a great help if you could! thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.