简体   繁体   中英

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? 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->...

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). You can retrieve the text with the lineEdit::text() accessor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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