Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Import data from txt file to QTableWidget in Qt

Import data from txt file to QTableWidget in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hael1902
    wrote on last edited by
    #1

    I am trying to import a sudoku game saved in a txt file to QTableWidget for a sudoku projekt, here is my txt file:

    000891470
    003200006
    009300080
    281000060
    360908015
    050000834
    030009600
    100006700
    072185000
    

    and here is my code in Qt:

    void MainWindow::TableWidgetDisplay()
    {
        QTableWidget *table = new QTableWidget(this);
        table->setRowCount(9);
        table->setColumnCount(9);
    
        this->setCentralWidget(table);
        table->horizontalHeader()->hide();
        table->verticalHeader()->hide();
    
        //Inseret data
        if(!file){
            std::cerr << "Unable to open file sudoku.txt";
            exit(1);   // call system to stop
        }
    
        //savedGames game (10);
    
        //importGames(game);
    
        //int Num = 1;
    
        graph matrix;
        char ch;
        matrix.resize(9,std::vector<int>(9));
        for(int i = 0; i <= 8; i++){
            for(int j = 0; j <= 8;){
                file >> std::noskipws >> ch;
                int a = ch - '0';
                if(a >= 0 && a <= 9){
                    matrix[i][j]= a;
                    j++;
                }
            }
        }
    
        for ( int row = 0; row < table->rowCount(); ++row ) {
            QTableWidgetItem *item;
            for ( int column = 0; column < table->colorCount(); ++column ) {
                item = new QTableWidgetItem;
                if(matrix[row][column]!=0) {
                    //table->setItem(row, column, new QTableWidgetItem(game[Num][row][column]));
                    item->setText(QString::number(matrix[row][column]));
                    table->setItem(row, column, item);
                }
            }
        }
    }
    

    file is a global variable

    std::fstream file("/Users/hassan/OneDrive/Documents/Civilingenjör/Å.K 2/Period 2/Programmeringsmetodik/Laborationer/lab6/Qt/mac/Sudoku", std::fstream::in);
    

    What am I doing wrong? The program does compile but the preview windows does not start.

    JonBJ 1 Reply Last reply
    0
    • S Offline
      S Offline
      StarterKit
      wrote on last edited by
      #2

      Very similar topic was discussed recently. I would advice you to look there.

      1 Reply Last reply
      3
      • H hael1902

        I am trying to import a sudoku game saved in a txt file to QTableWidget for a sudoku projekt, here is my txt file:

        000891470
        003200006
        009300080
        281000060
        360908015
        050000834
        030009600
        100006700
        072185000
        

        and here is my code in Qt:

        void MainWindow::TableWidgetDisplay()
        {
            QTableWidget *table = new QTableWidget(this);
            table->setRowCount(9);
            table->setColumnCount(9);
        
            this->setCentralWidget(table);
            table->horizontalHeader()->hide();
            table->verticalHeader()->hide();
        
            //Inseret data
            if(!file){
                std::cerr << "Unable to open file sudoku.txt";
                exit(1);   // call system to stop
            }
        
            //savedGames game (10);
        
            //importGames(game);
        
            //int Num = 1;
        
            graph matrix;
            char ch;
            matrix.resize(9,std::vector<int>(9));
            for(int i = 0; i <= 8; i++){
                for(int j = 0; j <= 8;){
                    file >> std::noskipws >> ch;
                    int a = ch - '0';
                    if(a >= 0 && a <= 9){
                        matrix[i][j]= a;
                        j++;
                    }
                }
            }
        
            for ( int row = 0; row < table->rowCount(); ++row ) {
                QTableWidgetItem *item;
                for ( int column = 0; column < table->colorCount(); ++column ) {
                    item = new QTableWidgetItem;
                    if(matrix[row][column]!=0) {
                        //table->setItem(row, column, new QTableWidgetItem(game[Num][row][column]));
                        item->setText(QString::number(matrix[row][column]));
                        table->setItem(row, column, item);
                    }
                }
            }
        }
        

        file is a global variable

        std::fstream file("/Users/hassan/OneDrive/Documents/Civilingenjör/Å.K 2/Period 2/Programmeringsmetodik/Laborationer/lab6/Qt/mac/Sudoku", std::fstream::in);
        

        What am I doing wrong? The program does compile but the preview windows does not start.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @hael1902
        Apart from following @StarterKit's good link, and putting in your own debug statements/stepping through under debugger, which is always the first port-of-call when you have unexpected behaviour during development:

        for ( int column = 0; column < table->colorCount(); ++column ) {

        Either your code will not compile, or you have not copied & pasted your actual code but instead typed in by hand. Please do not do that, people here waste much time analysing questioners' code only to find they say "Oh, I didn't type it in correct, I have something else"....

        UPDATE
        My huge apologies! I had never heard of QTableWidget::colorCount()!! I was sure you had table->columnCount()....

        ...Which now raises a question. Isn't your code incorrect because you do intend columnCount() here but have picked colorCount() perhaps accidentally from code autocompletion...? :)

        1 Reply Last reply
        1
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB said in Import data from txt file to QTableWidget in Qt:

          Either your code will not compile,

          It does :D

          https://doc.qt.io/qt-5/qpaintdevice.html#colorCount

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          JonBJ 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @JonB said in Import data from txt file to QTableWidget in Qt:

            Either your code will not compile,

            It does :D

            https://doc.qt.io/qt-5/qpaintdevice.html#colorCount

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            Thank you for pointing out my mistake :D I have UPDATEd my post above to apologise. But I wonder what the chances are that this is the cause of the OP's problem, because I would have thought he intended table->columnCount()....

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              @hael1902 said in Import data from txt file to QTableWidget in Qt:

              TableWidgetDisplay

              When is that method supposed to be called ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved