質問

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:

enter image description here

Why QLabel's default text was TextLabel?

役に立ちましたか?

解決

You should read some tutorials from Qt docs. You're mixing QtDesigner ui with manual widget creation. Your default text on label comes from your ui file. Also you don't need to create your labels/line edits when you use ui file. Just get them stright from ui class. So if you'll get your ui file back to normal, then you may do something like this:

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.

他のヒント

That's because both your

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

are different that the ones you created in your 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帰属
所属していません StackOverflow
scroll top