简体   繁体   English

使用insertText时Qt程序崩溃

[英]Qt program crashes when using insertText

I'm trying to display some information in my Qt program with the QPlainTextEdit , which I included with the Qt Designer. 我正在尝试使用Qt Designer附带的QPlainTextEdit在Qt程序中显示一些信息。 But when i try to display something with 但是当我尝试用

pTextEdit->insertPlainText("text");

my program crashes on execution. 我的程序在执行时崩溃。 When I try to declare the object myself 当我尝试自己声明对象时

QTextEdit *txt = new QTextEdit();

it doesn't work either. 它也不起作用。

The only time it works is when I create the object in the main.cpp. 它唯一有效的时间是在main.cpp中创建对象时。 But I need to display the information in my widget not in another window. 但是我需要在我的窗口小部件中而不是在另一个窗口中显示信息。

Any help is appreciated. 任何帮助表示赞赏。

EDIT: 编辑:

working: 工作:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    QTextEdit *txt = new QTextEdit();
    txt->setText("Hello, world!");
    txt->append("Appending some text…");

    txt->show();

    return a.exec();
}

not working (object created by Qt Designer): 不起作用(由Qt Designer创建的对象):

MainWindow::MainWindow( QWidget *parent ) :

... initialization list ...

{
    ui->setupUi( this );

    console->setPlainText("text");

    .. other stuff ...
}

Take into consideration that insertPlainText inserts text at current cursor position, so I think you may have problems if cursor is not set (no focus, for example). 考虑到insertPlainText在当前光标位置插入文本,所以我认为如果未设置光标(例如,没有焦点),您可能会遇到问题。 You may try: 您可以尝试:

txt->setPlainText("your text");

Or, if want to append: 或者,如果要附加:

txt->setPlainText(txt->toPlainText() + "appended text");

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

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