Can not read from QTableWidget
-
@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.