【问题标题】:How to pass data from one form to another in Qt?如何在 Qt 中将数据从一种形式传递到另一种形式?
【发布时间】:2011-06-01 10:50:06
【问题描述】:

如何在 Qt 中将数据从一种形式传递到另一种形式?
我创建了一个 QWidgetProgect -> QtGuiApplication,我目前有两个表单。现在我想将数据从一种形式传递到另一种形式。

我怎样才能做到这一点?

谢谢。

【问题讨论】:

  • 我可以帮你,但你需要告诉我你想在他们之间传递什么样的数据。
  • @Venemo : 我在表格 1 的文本框中输入的数据,我应该能够在表格 2 中获得

标签: qt qt4 qwidget


【解决方案1】:

以下是您可能想尝试的一些选项:

  • 如果一个表单拥有另一个表单,您可以在另一个表单中创建一个方法并调用它
  • 你可以使用Qt的Signals and slots机制,用文本框在表单中生成一个信号,并将它连接到你在其他表单中创建的插槽(你也可以将它与文本框的textChangedtextEdited连接起来信号)

信号和槽示例:

假设您有两个窗口:FirstFormSecondFormFirstForm 在其 UI 上有一个 QLineEdit,名为 myTextEditSecondForm 在其 UI 上有一个 QListWidget,名为 myListWidget

我还假设您在应用程序的 main() 函数中创建了两个窗口。

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

ma​​in.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();
}

【讨论】:

  • @user662285 - 添加了一个示例
  • @Venemo:我们可以将数据从 QlineEdit 传递到 Qlistwidget 吗?怎么做?我的要求是,当我在下一个表单中单击“确定”在我的文本框中输入内容时,它应该被添加到列表小部件中。
  • @user662285 - 你可以做同样的事情,然后使用文本创建一个新的列表小部件项。
  • @Venemo :对不起,我没听懂你能不能对上面的例子做些修改。谢谢。
  • @user662285 - 当然! ListWidget 和 QLineEdit 是在同一个表单上还是在两个不同的表单上?
【解决方案2】:

您还可以使用指针从其他表单访问 QTextEdit(假设这是您正在使用的)。

按照 Venemo 的示例(FirstForm 具有 QTextEdit,而 SecondForm 是您需要从中访问 QTextEdit 的示例):

firstform.h:

class FistForm : public QMainWindow
{

...

public:
    QTextEdit* textEdit();
};

firstform.cpp:

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

然后,您可以使用以下方式访问 SecondForm 中 QTextEdit 的文本(假设您的 FirstForm 实例称为 firstForm):

void SecondForm::processText()
{
    QString text = firstForm->textEdit()->toPlainText();
    // do something with the text
}

【讨论】:

  • 我们可以将数据从 QlineEdit 传递到 Qlistwidget 吗?怎么做?我的要求是当我在下一个表单中单击“确定”在我的文本框中输入内容时,它应该被添加到列表小部件中。
猜你喜欢
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
相关资源
最近更新 更多