PDA

View Full Version : setPlainText/setText simply won't run



RandomNobodyEU
9th June 2013, 16:52
I have made a simple text editor similar to notepad, with some simple functions. I have spent the entire day making a 'load file' dialog, and when everything finally worked, "ui->textEdit->setPlainText();" started failing on me. I think this might actually be a bug, as I can't for the life of me figure out what the problem is.

Anyway, here's the relevant code:

SaveLoad.cpp

QFile myFile(rPath);
#include "mainwindow.h"

void SaveLoad::load()
{
qDebug() << endl << "rPath in SaveLoad.h: " << rPath;

QFile myFile(rPath);
if (myFile.open(QIODevice::ReadOnly))
{
QTextStream textStream(&myFile);
text = textStream.readAll();

myFile.close();

MainWindow obj;
obj.setTextFile(text);
} else{
qDebug() << "The file didn't open properly";
}
}

MainWindow.cpp


void MainWindow::setTextFile(QString x)
{
qDebug() << endl << "This code runs in MainWindow.cpp" << endl << "fullText's content is:" << x << endl;
ui->textEdit->setText(x);
qDebug() << "This code(1) runs" << endl;
}


Tell me if that's not enough, then I'll post the entire code or the project file. Anyway, all the qDebugs run as they should. in the qDebug, QString x in void setTextFile(QString x) displays "Hello World!" as it should. The only problems is the line


ui->textEdit->setText(x);

I have tried replacing x with "Hello World!" but that didn't work either so the variable is not the problem. The entire program runs without errors or issues.
The only 'oddity' I noticed was that every variable I stored in SaveLoad.h or any variable in MainWindow.h that I tried to change from SaveLoad.cpp returned to null immediately after running the code.

(If I had, say, QString x in MainWindow.h, and I changed it in SaveLoad.cpp using:


MainWindow object;
object.x = "Hello World!"


then qDebug() << object.x; would return "Hello World!" but if I ran qDebug() << x; in MainWindow then it would return "".

Lesiok
9th June 2013, 17:23
How is obj (line 17 in first code) initialised ?

RandomNobodyEU
9th June 2013, 17:42
Well I include MainWindow.h in SaveLoad.cpp:
#include "mainwindow.h"

then I create an object:
MainWindow obj;

and then I run the function setTextFile with parameter (text)
obj.setTextFile(text);

I don't think I initialized it anywhere else than in SaveLoad.cpp

Also to add to the OP, nothing in this function runs except for qDebug:

void MainWindow::setTextFile(QString x)
{
qDebug() << endl << "This code runs in MainWindow.cpp" << endl << "fullText's content is:" << x << endl;
ui->textEdit->setText(x);
qDebug() << "This code(1) runs" << endl;
}

I tried setting a global variable equal to x but that didn't work either.

edit: tried

MainWindow *obj = new MainWindow;
obj->setTextFile(text);

but this had the same issue

wysota
9th June 2013, 20:04
What is the type of ui->textEdit?

Lesiok
9th June 2013, 20:55
OK, You create MainWindow object obj in line 16 (first source), in next line You put text on obj and then obj is destroyed because it is on stack.

RandomNobodyEU
9th June 2013, 22:24
Thank you very much everyone! After 24 hours I somehow fixed it!

I still don't know what was wrong with my initial code but I did a workaround.