【问题标题】:Qt - Change value of LineEdit when Dialog Box closesQt - 对话框关闭时更改 LineEdit 的值
【发布时间】:2012-09-16 05:08:29
【问题描述】:

我该怎么做:

例如,label username = "user"

我单击一个按钮,然后出现一个要求输入的对话框。我在输入框中输入“名称”,然后单击确定。现在我要如何让标签用户名在我单击对话框上的确定按钮时自动更改为“名称”?

此值转到设置值。我知道如何从设置中加载一个值,但是如果我的标签用户名和我输入“名称”的输入框位于 2 个不同的类中,我该如何进行更改?请帮忙。

【问题讨论】:

  • 当您按下“确定”按钮时,获取输入中的文本并使用 QLabel 的setText() 方法将文本设置为输入。

标签: c++ qt


【解决方案1】:

您需要使用信号和插槽。在你有这个 QLineEdit 的类中,你必须声明像

这样的信号
class SomeClass : public QDialog     //or other inheritance
{
    /* constructors, functions and other stuff */
    signals:
         void valueChanged(const QString&);        //in QString you will send new value
}

当有人点击“确定”按钮后,你必须发出这个信号:

emit valueChanged(myQLineEdit->text());

在调用 SomeClass 的类中,您必须将此信号连接到您将更改标签值的插槽,例如:

void MainWindow::someMethod()
{
    SomeClass *class = new SomeClass;
    connect(class, SIGNAL(valueChanged(QString)), this, SLOT(changeValue(QString)));
    /* set other parameters, show window*/
}

void MainWindow::changeValue(const QString &newText)
{
    myQLabel->setText(newText);
}

【讨论】:

  • 谢谢!太棒了! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2011-03-08
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
相关资源
最近更新 更多