简体   繁体   English

Qt QLabel默认文本

[英]Qt QLabel default text

My code is very simple: 我的代码很简单:

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    lineEdit = new QLineEdit();
    label = new QLabel("");
    connect(lineEdit, SIGNAL(textChanged(QString)), label, SLOT(setText(QString)));
    ui->setupUi(this);
}

I compiled it, and no error or warning. 我编译了它,没有错误或警告。

But When I run it, The UI like this: 但是当我运行它时,UI如下所示:

在此处输入图片说明

Why QLabel 's default text was TextLabel ? 为什么QLabel的默认文本是TextLabel

You should read some tutorials from Qt docs. 您应该阅读Qt文档中的一些教程。 You're mixing QtDesigner ui with manual widget creation. 您正在将QtDesigner ui与手动小部件创建混合在一起。 Your default text on label comes from your ui file. 标签上的默认文本来自ui文件。 Also you don't need to create your labels/line edits when you use ui file. 同样,当您使用ui文件时,您无需创建标签/行编辑。 Just get them stright from ui class. 只要让它们从ui类中变直。 So if you'll get your ui file back to normal, then you may do something like this: 因此,如果您将ui文件恢复为正常,则可以执行以下操作:

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->lineEdit, SIGNAL(textChanged(QString)), ui->label, SLOT(setText(QString)));
}

Also change text in your label with Qt Designer by doubleclick on it. 也可以通过双击Qt Designer更改标签中的文本。

That's because both your 那是因为你们两个

 lineEdit = new QLineEdit();
 label = new QLabel("");

are different that the ones you created in your ui. 与您在ui中创建的代码不同。 You are defining two new widgets, while you should probably reference the previous ones: 您正在定义两个新的小部件,而您可能应该参考前面的小部件:

 ui->lineEdit->clear();
 ui->label->clear();
 connect(ui->line....
 //etc...

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

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