[英]Read text file in textedit Qt C++
我有這個文本文件:
Name 1 Email 1
Name 2 Email 2
Name 3 Email 3
Name 4 Email 4
Name 5 Email 5
這是員工及其電子郵件的列表。 我想在對話框窗口中列出其名稱顯示在其中的列表。 我認為這是在對話框窗口上打印出文本文件的好方法,但是它不起作用。
employees_dialog.cpp
#include "employees_dialog.h"
#include "ui_employees_dialog.h"
#include <QtCore/QFile>
#include <QtCore/QTextStream>
employees_dialog::employees_dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::employees_dialog)
{
ui->setupUi(this);
getTextFile();
}
employees_dialog::~employees_dialog()
{
delete ui;
}
void employees_dialog::getTextFile()
{
QFile myFile(":/employees.txt");
myFile.open(QIODevice::ReadOnly);
QTextStream textStream(&myFile);
QString line = textStream.readAll();
myFile.close();
ui->textEdit->setPlainText(line);
}
這是頭文件。
#ifndef EMPLOYEES_DIALOG_H
#define EMPLOYEES_DIALOG_H
#include <QDialog>
namespace Ui {
class employees_dialog;
}
class employees_dialog : public QDialog
{
Q_OBJECT
public:
explicit employees_dialog(QWidget *parent = 0);
~employees_dialog();
private slots:
private:
Ui::employees_dialog *ui;
void getTextFile();
};
#endif // EMPLOYEES_DIALOG_H
因此,UI中的textEdit
應該顯示文本文件。 但這只是空白的白色。 我在Qt資源文件中有該文件。 調試器不會產生任何錯誤,應用程序本身也可以正常運行,但是文本不會出現在textEdit
。
順便說一句,我是Qt的新手。
用這個:
QFile file( "myfile.txt" );
if ( !file.exists() )
{
qDebug()<<"doesn't exist the file";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.