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
#include "mainwindow.h"
void SaveLoad::load()
{
qDebug() << endl << "rPath in SaveLoad.h: " << rPath;
{
text = textStream.readAll();
myFile.close();
MainWindow obj;
obj.setTextFile(text);
} else{
qDebug() << "The file didn't open properly";
}
}
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";
}
}
To copy to clipboard, switch view to plain text mode
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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);
ui->textEdit->setText(x);
To copy to clipboard, switch view to plain text mode
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!"
MainWindow object;
object.x = "Hello World!"
To copy to clipboard, switch view to plain text mode
then qDebug() << object.x; would return "Hello World!" but if I ran qDebug() << x; in MainWindow then it would return "".
Bookmarks