How to pass data from one form to another in Qt?

18,390

Solution 1

Here are some options that you might want to try:

  • If one form owns the other, you can just make a method in the other and call it
  • You can use Qt's Signals and slots mechanism, make a signal in the form with the textbox, and connect it to a slot you make in the other form (you could also connect it with the textbox's textChanged or textEdited signal)

Example with Signals and Slots:

Let's assume that you have two windows: FirstForm and SecondForm. FirstForm has a QLineEdit on its UI, named myTextEdit and SecondForm has a QListWidget on its UI, named myListWidget.

I'm also assuming that you create both of the windows in the main() function of your application.

firstform.h:

class FistForm : public QMainWindow
{

...

private slots:
    void onTextBoxReturnPressed();

signals:
    void newTextEntered(const QString &text);

};

firstform.cpp

// Constructor:
FistForm::FirstForm()
{
    // Connecting the textbox's returnPressed() signal so that
    // we can react to it

    connect(ui->myTextEdit, SIGNAL(returnPressed),
            this, SIGNAL(onTextBoxReturnPressed()));
}

void FirstForm::onTextBoxReturnPressed()
{
    // Emitting a signal with the new text
    emit this->newTextEntered(ui->myTextEdit->text());
}

secondform.h

class SecondForm : public QMainWindow
{

...

public slots:
    void onNewTextEntered(const QString &text);
};

secondform.cpp

void SecondForm::onNewTextEntered(const QString &text)
{
    // Adding a new item to the list widget
    ui->myListWidget->addItem(text);
}

main.cpp

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // Instantiating the forms
    FirstForm first;
    SecondForm second;

    // Connecting the signal we created in the first form
    // with the slot created in the second form
    QObject::connect(&first, SIGNAL(newTextEntered(const QString&)),
                     &second, SLOT(onNewTextEntered(const QString&)));

    // Showing them
    first.show();
    second.show();

    return app.exec();
}

Solution 2

You could also use pointers to access the QTextEdit (assuming that's what you're using) from the other form.

Following from Venemo's example (where FirstForm has the QTextEdit and SecondForm's the one you need to access the QTextEdit from):

firstform.h:

class FistForm : public QMainWindow
{

...

public:
    QTextEdit* textEdit();
};

firstform.cpp:

QTextEdit* FirstForm::textEdit()
{
    return ui->myTextEdit;
}

You can then access the QTextEdit's text in SecondForm with something like this (assuming your instance of FirstForm is called firstForm):

void SecondForm::processText()
{
    QString text = firstForm->textEdit()->toPlainText();
    // do something with the text
}
Share:
18,390
Bokambo
Author by

Bokambo

Updated on June 14, 2022

Comments

  • Bokambo
    Bokambo almost 2 years

    How can I pass data from one form to another in Qt?
    I have created a QWidgetProgect -> QtGuiApplication, I have two forms currently. Now I want to pass data from one form to another.

    How can I achieve that ?

    Thanks.

  • Venemo
    Venemo almost 13 years
    @user662285 - Added an example
  • Bokambo
    Bokambo almost 13 years
    @Venemo : Can we pass data from QlineEdit to Qlistwidget ? How to that ?My requirement is when i enter something in my textbox on click of Ok in next form it should get add to listwidget.
  • Bokambo
    Bokambo almost 13 years
    Can we pass data from QlineEdit to Qlistwidget ? How to that ?My requirement is when i enter something in my textbox on click of Ok in next form it should get added to listwidget.
  • Venemo
    Venemo almost 13 years
    @user662285 - You can do the same thing, then use the text to create a new list widget item.
  • Bokambo
    Bokambo almost 13 years
    @Venemo : Sorry i didn't get you can you pls make changes to above example.Thanks.
  • Venemo
    Venemo almost 13 years
    @user662285 - Sure! Are the ListWidget and the QLineEdit on the same form or on two different forms?
  • Bokambo
    Bokambo almost 13 years
    @Venemo : Two Different Forms.
  • Venemo
    Venemo almost 13 years
    @user662285 - I edited the example to reflect the use case you asked for.
  • RicoRicochet
    RicoRicochet almost 9 years
    wow.. thanks a ton.. been looking something similar to this.. thank you StackOverflow..
  • H.Ghassami
    H.Ghassami almost 8 years
    Hi @Venemo : I have a problem in your code . I also want to emit signal from first.cpp that change something in second.cpp. but I did exactly like your sample ( except second.show(); because I did not want to show the second form at first). could anyone help me?
  • Venemo
    Venemo almost 8 years
    @H.Ghassami Yes. Ask your question here: stackoverflow.com/questions/ask :)