[英]QTextEdit not Setting Text
I'm sure there most be a simple explanation for my problem but I just don't see it. 我敢肯定,对于我的问题,最简单的解释是,但我只是看不到它。 I'm trying to read a text file into a QTextEdit but apparently i'm unable to change the QTextEdit text in this method and I can't see why.
我正在尝试将文本文件读取到QTextEdit中,但是显然我无法以这种方法更改QTextEdit文本,而且我不知道为什么。
Document::Document(QWidget *parent) : QWidget(parent)
{
this->layout = new QGridLayout(this);
this->layout->setSpacing(2);
this->layout->setMargin(0);
this->setLayout(layout);
this->textArea = new QTextEdit(this);
this->textArea->setLineWrapMode(QTextEdit::NoWrap);
this->textArea->setAcceptDrops(true);
this->textArea->setAcceptRichText(true);
this->textArea->setUndoRedoEnabled(true);
this->textArea->setFont(QFont("Mono" , 11));
this->layout->addWidget(textArea);
this->textArea->show();
this->textArea->setFocus();
this->textArea->setText("Prueba de texto1");
}
void Document::open(QString archivo)
{
std::cout << "Opening..................." << std::endl;
this->textArea->setPlainText("Prueba de texto2");
QFile file(archivo);
QTextStream stream(&file);
//this->textArea->append(stream.readAll());
this->textArea->setText(stream.readAll());
std::cout << "Opened" << std::endl;
}
The first time I use SetText during the constructor it works fine but when I call it from open it doesn't work. 第一次在构造函数中使用SetText时,它工作正常,但是当我从open调用它时,它不起作用。 Help please
请帮助
You forgot to call open()
on the QFile
object 您忘记了在
QFile
对象上调用open()
QFile file(archivo);
if (file.open(QFile::ReadOnly){
QTextStream stream(&file);
...
} else {
/// Oops, no pude abrir el archivo para leer :(
}
Try 尝试
this->textArea->setPlainText(QString(stream.readAll());
or better yet replace the whole stream thing with 或者更好的是用
QFile *file = new QFile(archivo);
this->textArea->setPlainText(QString(file->readAll()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.