Using the standard file dialog box QFileDialog Open an existing file . use QtextEdit Display file contents .

An example is given below


#ifndef FILEWIDGET_H #define FILEWIDGET_H #include <QFileDialog> #include
<QWidget> #include <QPushButton> #include <QLabel> #include <QHBoxLayout>
#include <QFile> #include <QIODevice> #include <QTextEdit> #include
<QVBoxLayout> #include <QTextStream> #include <QMessageBox> class FileWidget :
public QWidget { Q_OBJECT public: explicit FileWidget(QWidget *parent = 0);
public slots: void slotOpenFileDialog(); private: QPushButton *openBtn; QLabel
*tipsLabel; QTextEdit *showText; }; #endif //FILEWIDGET_Hfile_widget.cpp

#include "file_widget.h" FileWidget::FileWidget(QWidget *parent /*= 0*/) :
QWidget(parent) { //--- Buttons and label openBtn = new QPushButton("OPEN", this);
connect(openBtn, SIGNAL(clicked()), this, SLOT(slotOpenFileDialog()));
tipsLabel = new QLabel("open file...", this); //----showText showText = new
QTextEdit(); //--- Buttons and label layout QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->addStretch(); vLayout->addWidget(tipsLabel);
vLayout->addWidget(openBtn); vLayout->addStretch(); vLayout->setSpacing(10); //
hLayout->setContentsMargins(10, 2, 10, 2); //---3 Overall layout of controls QHBoxLayout *hLayout =
new QHBoxLayout(this); hLayout->addLayout(vLayout);
hLayout->addWidget(showText); hLayout->setSpacing(10);
hLayout->setContentsMargins(10, 2, 10, 2); //-- Layout display setLayout(hLayout);
//--- Window properties resize(400, 300); setWindowTitle("open file"); } //------ Open file dialog void
FileWidget::slotOpenFileDialog() { /* getOpenFileName Function description Function prototype : QStringList
QFileDialog::getOpenFileNames( QWidget * parent = 0, const QString & caption =
QString(), // Open the title of the file dialog box const QString & dir = QString(), // find directory const QString
& filter = QString(), // Set the file format to be filtered QString * selectedFilter = 0, Options
options = 0) [static] */ //--- Get file name QString fileName = QFileDialog ::
getOpenFileName(this, NULL, NULL, "*.h *.cpp *.txt"); //--- Open the file and read the contents of the file QFile
file(fileName); //-- File opened successfully if ( ::ReadOnly | QIODevice
::Text)) { QTextStream textStream(&file); while (!textStream.atEnd()) {
//---QtextEdit Display file contents by line showText->append(textStream.readLine()); } } else
//--- fail to open file { /* information Function parameter description : Function prototype QMessageBox::information( QWidget *
parent, const QString & title, //--- title const QString & text, //--- show contents
StandardButtons buttons = Ok, //---OK Button StandardButton defaultButton =
NoButton) [static] */ QMessageBox ::information(NULL, NULL, "open file error");
} }main.cpp

#include <QtWidgets/QApplication> #include "file_widget.h" int main(int argc,
char *argv[]) { QApplication a(argc, argv); FileWidget win;; return
a.exec(); } The operation effect is as follows :

©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )