mypeeka
mypeeka

Reputation: 57

reading rows and columns from a textfile QT GUI C++

I want to read the size and the values of my matrix from a text file. an example of a text file graphe.txt

4 (the size of the matrix)
1 0 1 0
1 1 1 1
0 1 1 1
0 0 0 1

I tried a code but unfortunately it didn't work .I got this errors:

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 MainWindow::remplir_matrice(int taille_mat,int matrice[][50] )
 {

  QFile file("/home/yosra/degré/degré/graphe.txt");
 if (file.open(QIODevice::ReadOnly))
  { 
     QTextStream in(&file);
     int i=1;
     int ligne=1;
     while ((!in.atEnd())&&(ligne<=taille_mat))
        {
           ligne++;
          QString line = in.readLine();
          QStringList list = line.split(" ");
          QString val = list.at(i);
          this->display->setText(val);
          val.toInt();
          matrice[ligne][i]=val;
           i++;
        }
   file.close();
  }


 }
  void MainWindow::afficher(int matrice[][50],int taille_mat)
    {
      qDebug()<<" les elements de matrice";
      for(int i=0;i<taille_mat;i++)
       {
          for(int j=0;j<taille_mat;j++)
             qDebug()<<"M "<<matrice[i][j]<<endl;
       }
    }
 void MainWindow::parourir_fichier(int matrice[50][50],int taille_mat)

   {


    QFile file("/home/y/degré/classement/graphe.txt");

    if (file.open(QIODevice::ReadOnly))
      {
         QTextStream in(&file);
         QStringList list;
         QString line = in.readLine();
         QString val = list.at(0);
         this->display->setText(val);
         val.toInt();
         taille_mat=val;
         qDebug() << "taille_mat=" << taille_mat<<endl;
         file.close();

     }
      remplir_matrice(taille_mat,matrice);
      afficher(matrice,taille_mat);


  }

this is my MainWindow's header

  #include <QMainWindow>

  namespace Ui {
  class MainWindow;
               }

  class MainWindow : public QMainWindow
    {
      Q_OBJECT

    public:
     explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
     void remplir_matrice(int taille_mat,int matrice[][50] );
     void parourir_fichier(int matrice[][50],int taille_mat);
     void afficher(int matrice[][50],int taille_mat);

    private:
     Ui::MainWindow *ui;
     int matrice[50][50];
     int taille_mat;
  };

Upvotes: 1

Views: 679

Answers (2)

user23573
user23573

Reputation: 2569

Could it be that you mean:

ui->display->setText(val);

The MainWindow class does not have a pointer to the display object. Perhaps the display object was created with Qt Creator editor as a TextEdit field?


Update

If you just want to see a value while you are still developing your code, you are probably better off using qDebug() (documentation here). You will need to include to make this work. The output will be shown in the output pane when you run the application from Qt Creator.

#include <QDebug>

// ...further down in your code:

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

Upvotes: 1

Christian Hackl
Christian Hackl

Reputation: 27528

The second error message is pretty clear, isn't it? A QString cannot be automatically converted to an int. I don't really know Qt, but a quick Google search reveals the existence of a toInt member function, so the following probably works:

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

As for the first error message, this->display supposes the existence of a member variable in MainWindow. If display is a member function (it certainly sounds like one), then you need parentheses: this->display(). If there is no member function of that name either, then we cannot help you much with the code that you have posted.

Upvotes: 0

Related Questions