简体   繁体   English

如何在Qt4中打开对话框并从中检索字符串?

[英]How do I open a dialog and retrieve strings from it, in Qt4?

This is the main window so far and the second window is a dialog window. 这是到目前为止的主窗口,第二个窗口是对话框窗口。 How do I get the text from a textbox on window2 when it closes? 关闭window2时如何从文本框中获取文本? Thanks. 谢谢。

#include "mainwindow.h"
#include "window2.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(closeProgram()));
    connect(ui->openWindowBtn, SIGNAL(clicked()), this, SLOT(openSecondWindow()));
}

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

void MainWindow::openSecondWindow()
{
    Window2 w2;
    w2.exec();
}

void MainWindow::closeProgram()
{
    close();
}

Found Solution 找到解决方案

All I had to do is create a getString() function in the Window2 class to retreive the text from ui->... 我要做的就是在Window2类中创建一个getString()函数,以从ui->中检索文本。

QString Window2::getString()
{
    return ui->textEdit->text();
}

Look at your .ui file in designer (or the resulting generated file from the uic), and access the QLineEdit object by name (the same way you connect that signal). 在设计器中查看您的.ui文件(或从uic生成的结果文件),然后按名称访问QLineEdit对象(连接该信号的方式相同)。 You can retrieve the text with the lineEdit::text() accessor. 您可以使用lineEdit :: text()访问器检索文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM