开发者

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

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 achi开发者_StackOverflow社区eve that ?

Thanks.


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


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜