当前位置:网站首页>QT opens a file and uses QFileDialog to obtain the file name, content, etc

QT opens a file and uses QFileDialog to obtain the file name, content, etc

2022-07-07 03:41:00 Program yuan ZCC

Main code :

//.h

#include <QFile>
#include <QFileDialog>

QFile *localFile;
QString filename;
qint64 totalBytes;      // Total bytes of file 
QByteArray authorizeFile;


//.cpp

// First bind the button to open the file 
connect(this->ui->pushButton_openFile, SIGNAL(clicked()), this, SLOT(selectFile()));

    

void MyDialog::selectFile()
{
    this->filename = QFileDialog::getOpenFileName(this, "Open a file", "/", "files (*)");
//    ui->textEdit->setText(filename);
    this->localFile = new QFile(filename);
    if (!localFile->open(QFile::ReadOnly | QIODevice::Text))
    {
        ui->textEdit->setText(tr("FileSetDialog:open file error!"));
        return;
    }
    //  Get file size 
    this->totalBytes = localFile->size();

    qDebug() << "this->totalBytes====" << this->totalBytes;

// Get everything in the file 
    authorizeFile = localFile->readAll();
    fileTxt = QString(authorizeFile);
    ui->textEdit->setText(fileTxt);

}

 1、 Click the open file button   Pop up the select File pop-up box

2、 Pop up and press cancel   Tips  

3、 Select file to confirm Show the contents of the document  

原网站

版权声明
本文为[Program yuan ZCC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062028310889.html