문제

I've created a qt widgets application. Using the design mode I've created a QTextEdit and indicated that in the header file:

...

QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QTextEdit;
QT_END_NAMESPACE

...

private:
    Ui::MainWindow *ui;
    QTextEdit *textEdit_2;
};

There is also a slot which is triggered by pushing a button. What it has to do is to insert some text into textEdit_2 after the button is pushed, still the program crashes. In mainwindow.cpp:

void MainWindow::on_action_4_triggered()
{
    textEdit_2->setText("text");
}

I've also tried

textEdit_2->setText(QString("text"));

which anyway doesn't work. What's the problem?

도움이 되었습니까?

해결책

textEdit_2->setText("text");

The problem is that you are trying to ignore the actual text widget created in QtDesigner and invent another as a class member. This is not going to fly as you seem to want it.

In order to reuse the text widget from the UI that you created with the graphical tool, you would need to reuse the ui object as follows:

ui->textEdit_2->setText("text");

Please also note that you do not need to construct QString explicitly like this:

textEdit_2->setText(QString("text"));

This will be all automatic for you.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top