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

An example is given below

file_widget.h

#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 (file.open(QIODevice ::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; win.show(); return
a.exec(); } The operation effect is as follows :

Technology
©2019-2020 Toolsou All rights reserved,
ElementUI In the select Of label value Map judge key Corresponding value Does the value exist -containsKey()Vue + Element-ui Drop down box for el-select Get extra parameters ( Essence )2020 year 7 month 15 day Wechat applet template Use of vue vue-element-admin Summary of the project Share has surpassed Ningde Era !LG Chemical confirmation to spin off battery business unit [work] python read txt Last line of file Keras Summary of training data loading ( Essence )2020 year 8 month 13 day C# Basic knowledge windform Realize two color sphere Go language Array initialization and basic operations