简单的QT从文本文件程序中读取不编译

Simple Qt Read From Text File Program not Compiling

本文关键字:读取 编译 程序 文件 QT 文本 简单      更新时间:2023-10-16

基本上我制作了一个简单的程序来读取一行文本,但是每当我运行程序时,我都会收到一个错误

no match for 'operator>>' (operand types are 'QFile' and 'QString')
while(file >> name >> month >> day >> year >> subject >> level >> apages >> total >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten)

和另一个错误:

expected unqualified-id before '<<' token
         QDebug << QString(name);

我已经尝试在网上找到一个小时,但是我真的找不到有效或能理解的东西。我真的很感谢可以提供的任何输入。

相关代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QCoreApplication>
#include <QtCore>
#include <QFile>
#
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    QString name, month,  subject, level;
    int day, year, apages, total, one, two, three, four, five, six, seven, eight, nine, ten;
    QFile file("C:/Users/brandan/Desktop/GUIPrograms/Kumon.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    while(file >> name >> month >> day >> year >> subject >> level >> apages >> total >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten)
    {
        QDebug << name << month << day << year << subject << level << apages << total << one << two << three << four << five << six << seven << eight << nine << ten;
    }
}

从qfile的API信息中,似乎应该使用,例如QTextStream对象使用<<>>操作员。关于调试错误:QDebug是类的名称,而不是内存中的实例。从QDebug页面中,您可以改用qDebug()

QDebug是一种类型,您需要使用QDebug的实例,该实例从函数qDebug()检索:

qDebug() << a << b << c;
^^^^^^^^