gpt4 book ai didi

c++ - 在textedit Qt C++中读取文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:59:10 25 4
gpt4 key购买 nike

我有这个文本文件:

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";
}

关于c++ - 在textedit Qt C++中读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com