Reputation: 25
I made a push button that will browse and get a textfile. But I need to open it in a new window to check if the content of the textfile is correct. How do I do this?
Also, I would like to have a line edit right next to the button that shows which file I'm looking at. In other words, the directory of the file that is opened through the button.
Currently, this is what I have:
void MainWindow::on_fileButton_clicked()
{
QString fileName1 = QFileDialog::getOpenFileName(this,tr("Open Text File"), "", tr("Text Files (*.txt)"));
QFile file1(fileName1);
if(!file1.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file1);
while(!in.atEnd()){
QString line = in.readLine();
}
}
Upvotes: 1
Views: 4510
Reputation: 60014
I suggest using one of powerful text interfaces available:
void MainWindow::openfile() {
QString fileName1 = QFileDialog::getOpenFileName(this,tr("Open Text File"), "", tr("Text Files (*.txt)"));
QFile file1(fileName1);
if(!file1.open(QIODevice::ReadOnly | QIODevice::Text))
return;
// show the directory path of opened file
dir->setText(QFileInfo(file1).dir().path());
QTextBrowser *b = new QTextBrowser;
b->setText(file1.readAll());
b->show();
}
dir is a member variable, initialized in constructor with
dir = new QLineEdit(this);
Upvotes: 1
Reputation: 662
you should make a new window by add a Dialog or Mainwindow. after that add widgets like textEdit and other things to your new Dialog.
you need to learn some basics of Qt framework: there is very well documentation of Qt, you can use it. and also there is about 100 short videos of Qt learning.
Upvotes: 0