I’m attempting to write a text editor. I’m unable to open a file. Why? Here’s my code:
void MainWindow::readfromfile(){
QFile inputFile(":/text.txt");
inputFile.open(QIODevice::ReadOnly);
QTextStream in(&inputFile);
QString line = in.readAll();
inputFile.close();
ui->textEdit->setPlainText(line);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
and
void MainWindow::on_actionOpen_triggered()
{
connect(ui->actionOpen,SIGNAL(triggered()),ui->textEdit,SLOT(readfromfile()));
}
First, it seems that you are using a text file, so it might be worth specifying that when you open the file:
Then also check the output of the
open()call:If the file could not be opened, check that the file path is correct.