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++;
}
}
I think, you need to replace
ui->plainTextEdit->setPlainText(InputData.readAll());
with the
plainTextEdit->setPlainText(InputData.readAll());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.