Can not read from QTableWidget
-
for(int i=0; i < table->rowCount(); i++)
{
QString a=" <text>text1</text>";
QString b=" <text2>text3</text2>";
QTableWidgetItem *item1(table->item(i,0));
QTableWidgetItem *item2(table->item(i+1,0));
if (a==item1->text()) {
b = item2->text();
}
} -
@Raul200000000009 said in Can not read from QTableWidget:
The String exists but qt does not find it .
If the string
" text"
exists in the QTableWidget then it would be found. Please provide a minimal, compilable example where you first insert the string" text"
and then search for it.mainwindow.cpp
It crashes again.#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); table=new QTableWidget(this); table->setColumnCount(1); table->insertRow(0); table->insertRow(1); table->setGeometry(200,200,200,200); table->setItem(1,0,new QTableWidgetItem (" text")); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { bool prim=false; for(int i=0;i<table->rowCount();++i){ if (table->item(i,0)->text()==" text") { prim=true; } if(prim==false) { //table->insertRow(i+1); table->setItem(i,0,new QTableWidgetItem (" nope")); } if(prim==true) { table->insertRow(i+1); table->setItem(i+1,0,new QTableWidgetItem ("another text")); } } }
-
for(int i=0; i < table->rowCount(); i++)
{
QString a=" <text>text1</text>";
QString b=" <text2>text3</text2>";
QTableWidgetItem *item1(table->item(i,0));
QTableWidgetItem *item2(table->item(i+1,0));
if (a==item1->text()) {
b = item2->text();
}
}@Raul200000000009
You don't have anything in your table widget so the condition never returns true.
Your code "crashes" on the last iteration with an "index out of bounds". -
@Raul200000000009
You don't have anything in your table widget so the condition never returns true.
Your code "crashes" on the last iteration with an "index out of bounds".@JonB The tableWidget reads data from an file,wich is not empty.
-
@JonB The tableWidget reads data from an file,wich is not empty.
@Raul200000000009 Your code makes no sense and you haven't asked a question or explained what the problem is.
-
@Raul200000000009 Your code makes no sense and you haven't asked a question or explained what the problem is.
@JonB Here is the update version of the code.It crashes.
The code is reading from an .xml file.
int x=0;
bool prim=false;
QString a= " text";
QTableWidgetItem *item1(table->item(i+1,0));
for( i=0;i<table->rowCount();++i){
if (a==item1->text()) {
prim=true;
table->insertRow(i+1);
QTableWidgetItem *item2(table->item(i+1,0));
table->setItem(i+1,0,new QTableWidgetItem (" <another text"));
}
if(prim!=true)
{
;
}} -
@JonB Here is the update version of the code.It crashes.
The code is reading from an .xml file.
int x=0;
bool prim=false;
QString a= " text";
QTableWidgetItem *item1(table->item(i+1,0));
for( i=0;i<table->rowCount();++i){
if (a==item1->text()) {
prim=true;
table->insertRow(i+1);
QTableWidgetItem *item2(table->item(i+1,0));
table->setItem(i+1,0,new QTableWidgetItem (" <another text"));
}
if(prim!=true)
{
;
}}@Raul200000000009 If it crashes, use a debugger to see where exactly it is crashing.
Also please properly format your code with the code tags so everyone can read it. -
@JonB Here is the update version of the code.It crashes.
The code is reading from an .xml file.
int x=0;
bool prim=false;
QString a= " text";
QTableWidgetItem *item1(table->item(i+1,0));
for( i=0;i<table->rowCount();++i){
if (a==item1->text()) {
prim=true;
table->insertRow(i+1);
QTableWidgetItem *item2(table->item(i+1,0));
table->setItem(i+1,0,new QTableWidgetItem (" <another text"));
}
if(prim!=true)
{
;
}}@Raul200000000009 check item1 and item2 if they are nullptr…see the doc for item function.
-
@Raul200000000009 check item1 and item2 if they are nullptr…see the doc for item function.
@DerReisende ,I solved the crashing error,but the code does not work.It should add one "anothertext" row below "text" row if "text" row exists.
bool prim=false;
for( i=0;i<table->rowCount();++i){
if (table->item(i,0)->text()!=" text") {
prim=true;
}
if(prim!=true)
{
table->insertRow(i+1);
table->setItem(i+1,0,new QTableWidgetItem (" anothertext"));
}} -
@DerReisende ,I solved the crashing error,but the code does not work.It should add one "anothertext" row below "text" row if "text" row exists.
bool prim=false;
for( i=0;i<table->rowCount();++i){
if (table->item(i,0)->text()!=" text") {
prim=true;
}
if(prim!=true)
{
table->insertRow(i+1);
table->setItem(i+1,0,new QTableWidgetItem (" anothertext"));
}}Hi,
What exactly does not work ?
From the looks of it, your if condition is simply never true. If it should be once then you should really ensure that the string does really exist in your table. -
Hi,
What exactly does not work ?
From the looks of it, your if condition is simply never true. If it should be once then you should really ensure that the string does really exist in your table.@SGaist
What should happen:
The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.
What happens:
The code does not upgrade the string from the table.
So the string from the file is not upgraded. -
@SGaist
What should happen:
The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.
What happens:
The code does not upgrade the string from the table.
So the string from the file is not upgraded.@Raul200000000009 said in Can not read from QTableWidget:
The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.
Apart from the fact that you still do not properly format your code so that it's readable your code does not read anything from a file nor does it write something out to a file.
-
@SGaist
What should happen:
The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.
What happens:
The code does not upgrade the string from the table.
So the string from the file is not upgraded.Did you check that you are actually entering that loop at least once ?
-
@SGaist
The String exists but qt does not find it . -
@SGaist
The String exists but qt does not find it .@Raul200000000009 said in Can not read from QTableWidget:
The String exists but qt does not find it .
If the string
" text"
exists in the QTableWidget then it would be found. Please provide a minimal, compilable example where you first insert the string" text"
and then search for it. -
@Raul200000000009 said in Can not read from QTableWidget:
The String exists but qt does not find it .
If the string
" text"
exists in the QTableWidget then it would be found. Please provide a minimal, compilable example where you first insert the string" text"
and then search for it.mainwindow.cpp
It crashes again.#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); table=new QTableWidget(this); table->setColumnCount(1); table->insertRow(0); table->insertRow(1); table->setGeometry(200,200,200,200); table->setItem(1,0,new QTableWidgetItem (" text")); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { bool prim=false; for(int i=0;i<table->rowCount();++i){ if (table->item(i,0)->text()==" text") { prim=true; } if(prim==false) { //table->insertRow(i+1); table->setItem(i,0,new QTableWidgetItem (" nope")); } if(prim==true) { table->insertRow(i+1); table->setItem(i+1,0,new QTableWidgetItem ("another text")); } } }
-
mainwindow.cpp
It crashes again.#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); table=new QTableWidget(this); table->setColumnCount(1); table->insertRow(0); table->insertRow(1); table->setGeometry(200,200,200,200); table->setItem(1,0,new QTableWidgetItem (" text")); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { bool prim=false; for(int i=0;i<table->rowCount();++i){ if (table->item(i,0)->text()==" text") { prim=true; } if(prim==false) { //table->insertRow(i+1); table->setItem(i,0,new QTableWidgetItem (" nope")); } if(prim==true) { table->insertRow(i+1); table->setItem(i+1,0,new QTableWidgetItem ("another text")); } } }
Because there's no item in your row 0.
-
@SGaist The code is working but I need to remove an "invisible" new line character from my text file.Any ideas?
-
-
@SGaist The code is working but I need to remove an "invisible" new line character from my text file.Any ideas?
@Raul200000000009 said in Can not read from QTableWidget:
Any ideas?
Take a look into the QString documentation would be a good start.