Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文本文件QT GUI C++;_C++_Qt - Fatal编程技术网

C++ 从文本文件QT GUI C++;

C++ 从文本文件QT GUI C++;,c++,qt,C++,Qt,我想从文本文件中读取矩阵的大小和值。 文本文件的示例 graph.txt 4 (the size of the matrix) 1 0 1 0 1 1 1 1 0 1 1 1 0 0 0 1 我尝试了一个代码,但不幸的是它不起作用。我得到了以下错误: error: 'class MainWindow' has no member named 'display' this->display->setText(val); error: cannot convert 'QString'

我想从文本文件中读取矩阵的大小和值。 文本文件的示例 graph.txt

4 (the size of the matrix)
1 0 1 0
1 1 1 1
0 1 1 1
0 0 0 1
我尝试了一个代码,但不幸的是它不起作用。我得到了以下错误:

error: 'class MainWindow' has no member named 'display' this->display->setText(val);
error: cannot convert 'QString' to 'int' in assignment
              matrice[ligne][i]=val;

void主窗口::remplir_matrice(int taille_mat,int matrice[][50])
{
QFile文件(“/home/yosra/degré/degré/graph.txt”);
if(file.open(QIODevice::ReadOnly))
{ 
文件中的QTextStream(&F);
int i=1;
int-ligne=1;
而(!in.atEnd())&&(lignedisplay->setText(val);
val.toInt();
矩阵[ligne][i]=val;
i++;
}
file.close();
}
}
无效主窗口::粘贴器(内部矩阵[][50],内部尾板)
{

qDebug()第二个错误消息非常清楚,不是吗?
QString
无法自动转换为
int
。我不太了解Qt,但快速的谷歌搜索会发现存在一个,因此以下可能有效:

matrice[ligne][i]=val.toInt();

对于第一条错误消息,
this->display
假设
main窗口中存在一个成员变量
。如果
display
是一个成员函数(听起来肯定像),则需要括号:
this->display()
。如果也没有该名称的成员函数,那么我们无法在您发布的代码方面为您提供太多帮助。

您的意思是:

ui->display->setText(val);
main窗口
类没有指向
display
对象的指针。可能
display
对象是使用Qt Creator编辑器作为
TextEdit
字段创建的


更新

如果您只是想在开发代码的同时看到一个值,那么最好使用
qDebug()
(文档)。您需要包含以实现此功能。当您从Qt Creator运行应用程序时,输出将显示在输出窗格中

#include <QDebug>

// ...further down in your code:

qDebug() << "Output of val:" << val;     
#包括
//…在代码的下面:

qDebug()发布一个
main窗口的头文件。谢谢,我纠正了第二个错误,但是我仍然得到了与“display”相同的错误即使我添加了括号。
display
是什么?@YosraHassad:编译器试图在
MainWindow
中找到
display
成员,但没有找到任何成员。由于您没有向我们显示
MainWindow
的定义,我们无法告诉您有什么问题。我找到了问题所在。“display”是我的界面的textbrowser的名称,我必须在其中显示文件的内容,但目前我不想在界面上显示其内容!我想做一个测试,以确保在file.txt中获取的my matrix的值及其大小受到很大影响。所以我删除了此指令,代码现在已编译但不幸的是,当我执行操作时,控制台上什么也没有显示!!@YosraHassad:这是合乎逻辑的结果:代码没有显示任何内容。如果你想看到一些东西,你必须以某种方式“把它放出来”。
setText()
方法在这方面没有错,您可以直接在UI中看到它。但是,如果您只是想打印一些东西,那么使用
qDebug()可能会更好。我使用了qDebug,但它不起作用(控制台中没有显示任何内容)。如何启动应用程序?QT+=核心gui大于(QT_MAJOR_版本,4):QT+=widgets TARGET=untitled TEMPLATE=app SOURCES+=main.cpp\mainwindow.cpp HEADERS+=mainwindow.h FORMS+=mainwindow.ui其他_文件+=\../degré/classement/graph.txt
ui->display->setText(val);
#include <QDebug>

// ...further down in your code:

qDebug() << "Output of val:" << val;