0

I am working on a muti-tab text editor project. I have a problem when I try to display text file in new tab. When openning a text file, I want the program display the content in a new tab with a new QPlainTextEdit widget and don't toutch old tabs and their contents.

My Problem: When the program opens a new text file, it will create a new tab and change its text to file name but it displays file content in first tab and its plainTextEdit widget. How to fix it?

My Code:

void MainWindow::on_btn_Open_triggered()
{
  FilePath = QFileDialog::getOpenFileName(this, "Open File", "./", "All Files(*.*)");
  QFile GetFile(FilePath);
  QFileInfo FileMetaData(FilePath);

  if (!GetFile.open(QIODevice::ReadOnly|QIODevice::Text))
  {
     QMessageBox::information(0, "ERROR", "Cannot open this file.");
  }
  else
  {
     int clickTimes = 1;
     QTextStream InputData(&GetFile);
     QPlainTextEdit *plainTextEdit = new QPlainTextEdit;
     ui->tabWidget->insertTab(clickTimes, plainTextEdit, FileMetaData.fileName());
     ui->tabWidget->setCurrentIndex(clickTimes);
     ui->plainTextEdit->setPlainText(InputData.readAll());
     clickTimes++;
  }
}
user3654736
  • 55
  • 1
  • 5

1 Answers1

0

I think, you need to replace

ui->plainTextEdit->setPlainText(InputData.readAll());

with the

plainTextEdit->setPlainText(InputData.readAll());
vahancho
  • 20,808
  • 3
  • 47
  • 55