Import data from txt file to QTableWidget in Qt
-
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.
-
Very similar topic was discussed recently. I would advice you to look there.
-
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.
@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 ofQTableWidget::colorCount()
!! I was sure you hadtable->columnCount()
.......Which now raises a question. Isn't your code incorrect because you do intend
columnCount()
here but have pickedcolorCount()
perhaps accidentally from code autocompletion...? :) -
@JonB said in Import data from txt file to QTableWidget in Qt:
Either your code will not compile,
It does :D
-
@JonB said in Import data from txt file to QTableWidget in Qt:
Either your code will not compile,
It does :D
@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 intendedtable->columnCount()
.... -
Hi,
@hael1902 said in Import data from txt file to QTableWidget in Qt:
TableWidgetDisplay
When is that method supposed to be called ?