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. Simple qt help with data between forms

Simple qt help with data between forms

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.8k Views 1 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.
  • B Offline
    B Offline
    bwcal1999
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bwcal1999
      wrote on last edited by
      #2

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

      Thanks!

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #3

        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();
        }
        ...
        @

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bwcal1999
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bwcal1999
              wrote on last edited by
              #6

              That would be a great help if you could! thanks

              1 Reply Last reply
              0

              • Login

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