是否在不同的窗口和/或对话框中显示QLineEDIT的输入? [英] Display the input of QLineEdit in a different window and or dialog?

查看:61
本文介绍了是否在不同的窗口和/或对话框中显示QLineEDIT的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型Qt Gui应用程序,其中我的mainwindow.ui中有一个QLineEdit,并且我希望在按下按钮时在单独的对话框和/或窗口中显示输入的文本。

现在,我已经将输入存储在一个变量中,并且我还能够在同一个主窗口中的标签上显示该字符串

void MainWindow::on_GoButton_clicked()
{
    QString mytext = ui->lineEdit_1->text();
    ui->label_1->setText(mytext);
}

现在,我想打开一个弹出对话框(也可以是窗口),例如SecDialog;

SecDialog secdialog;
secdialog.setModal(true);
secdialog.exec();

并在SecDialog的标签中显示main Window->myText字符串变量的文本。我怎么才能做到这一点?我知道这是一个基本级别的问题,但我认为它将有助于消除我对表单和类之间变量的移动值的许多疑虑。

推荐答案

情况

这就是您的情况:

从您的代码中,该对话框是modal dialog

SecDialog secdialog;
//secdialog.setModal(true); // It's not needed since you already called exec(), and the 
                            // dialog will be automatically set to be modal just like what
                            // document says in Chernobyl's answer 

secdialog.exec();          

解决方案

若要使对话框显示窗口中的文本,

其概念是传递窗口中的信息(文本 并使用对话框中的setter函数来显示它。

Floris Velleman的回答一样,他将mytext字符串(通过引用)传递给自定义的对话框构造函数,并立即调用settertheStringInThisClass(myString)

该函数的实现细节由切尔诺贝利的回答补充(改用名称setLabelText):

void SecDialog::setLabelText(QString str)
{
    ui->label->setText(str); // this "ui" is the UI namespace of the dialog itself.
                             // If you create the dialog by designer, it's from dialog.ui
                             // Do not confuse with the ui from mainwindow.ui
}

切尔诺贝利建议了另一种方法,即在Slot函数中调用setter,它省去了定义另一个构造函数的需要,但概念基本上是相同的:

void MainWindow::on_GoButton_clicked()
{
    QString mytext = ui->lineEdit_1->text();
    ui->label_1->setText(mytext);
    SecDialog secdialog;

    secdialog.setLabelText(myText); // display the text in dialog

    secdialog.exec();
}

评论

我尽可能清楚地说明这个概念,因为根据我之前对您问题的经验,您只是从答案中复制和粘贴代码并将其作为最终解决方案,这是不正确的。因此,我希望这篇摘要可以帮助您理解概念,然后您可以编写自己的代码

这篇关于是否在不同的窗口和/或对话框中显示QLineEDIT的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆