putting QTableWidgetItem (s) in a boolean array
Hello,
As the title is saying , I am trying to put the result of a user input , in a QTableWidget, and take this QTableWidget to fillin an array.
I have read the documentation but infortunetly I can not do it .
Here is the code:
Code:
//the header
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QtGui>
#include <vector>
class FenPrincipale
: public QWidget{
Q_OBJECT
public:
FenPrincipale();
bool M[2][2];
private slots:
private:
};
#endif
//FenPrincipale.cpp
#include "qDebug.h"
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale()
{
int o,p;
for (o=0; o<2; o++)
{
for (p=0; p<2; p++)
{
(M[o][p])=false;
}
}
for (o=0; o<2; o++)
{
for (p=0; p<2; p++)
{
element=(tableWidget->itemAt((o+1),(p+1)));
if ((element->toString)="1")
{
M[o][p]=true;
}
else
{
M[o][p]=false;
}
}
}
}
//main.cpp
#include <QApplication>
#include "FenPrincipale.h"
int main(int argc, char* argv[])
{
FenPrincipale fenetre;
fenetre.show();
return app.exec();
}
Thank you very much for the attention given to this message.
Re: putting QTableWidgetItem (s) in a boolean array
What is the error? isn`t the program going out of boundries with that +1??Are you sure its a custom type bool(as you are passing the 1000 to the constructor)?
Re: putting QTableWidgetItem (s) in a boolean array
Thank you for your answer :
here is a modification of the Fenprincipale.cpp , but it is still not working:
//FenPrincipale.cpp
#include "qDebug.h"
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale()
{
int o,p;
tableWidget = new QTableWidget(2,2,this);
element = new QTableWidgetItem (1000);
for (o=0; o<2; o++)
{
for (p=0; p<2; p++)
{
(M[o][p])=false;
}
}
for (o=0; o<2; o++)
{
for (p=0; p<2; p++)
{
element=(tableWidget->item((o+1),(p+1)));
if ((element->text())=="1")
{
M[o][p]=true;
}
else
{
M[o][p]=false;
}
}
}
}
Thank you in advance for your help.
Re: putting QTableWidgetItem (s) in a boolean array
Hi,
Use QTableWidget->item,
Because QTableWidget->itemAt use coordinat location.
Please check this code :
Code:
ui->tableWidget->setRowCount(2);
ui->tableWidget->setColumnCount(2);
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
ui->tableWidget->setItem(i,j,item);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
item = ui->tableWidget->item(i,j);
ui->textEdit->append(item->text());
if (item->text()="1")
{
M[o][p]=true;
}
else
{
M[o][p]=false;
}
}
}
you can modified with your problem. This code return text value from QTableWidget
Thank you
Best regards,
Myta
Re: putting QTableWidgetItem (s) in a boolean array
Thank you very much ,
I tried this code but an error occured because of ui .
Can you please tell me what "ui" means?
Thank you
Re: putting QTableWidgetItem (s) in a boolean array
basically UI represents all the objects created with the QDesigner...
http://developer.qt.nokia.com/doc/qt...-concepts.html
a recommendation: google what you dont know, ask what you cant find :D
Re: putting QTableWidgetItem (s) in a boolean array
Thanks,
I tried to put "this" (which means fenetre in my case ) instead of "ui" , but it is still not working.
Thank you again.
Re: putting QTableWidgetItem (s) in a boolean array
this->
should work because you have the table widget declared in your .h :/
post the code
remember to post with [CODE ] [/CODE] please
Re: putting QTableWidgetItem (s) in a boolean array
Thanks a lot,
Please check also the header maybe the error came from there.
Here is the code :
Code:
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QtGui>
#include <vector>
class FenPrincipale
: public QWidget{
Q_OBJECT
public:
FenPrincipale();
bool M[2][2];
private slots:
private:
};
#endif
//FenPrincipale.cpp
//FenPrincipale.cpp
#include "qDebug.h"
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale()
{
this->tableWidget->setRowCount(2);
this->tableWidget->setColumnCount(2);
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
this->tableWidget->setItem(i,j,item);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
item = this->tableWidget->item(i,j);
this->textEdit->append(item->text());
if (item->text()="1")
{
M[o][p]=true;
}
else
{
M[o][p]=false;
}
}
}
}
//main.cpp
#include <QApplication>
#include "FenPrincipale.h"
int main(int argc, char* argv[])
{
FenPrincipale fenetre;
fenetre.show();
return app.exec();
}
Thanks one more time.
The errors are:
- C:\Users\SHADUS\fortementc\tabl\FenPrincipale.cpp: 33: erreur : 'class FenPrincipale' has no member named 'textEdit'
- C:\Users\SHADUS\fortementc\tabl\FenPrincipale.cpp: 35: erreur : could not convert 'QTableWidgetItem::text() const().QString::operator=(((const char*)"1"))' to 'bool'
the error after correction of error at line 35 (one"=" was missing) is:
- 'class FenPrincipale' has no member named 'textEdit'
Re: putting QTableWidgetItem (s) in a boolean array
Well if you read that error carefully it means you havent declared textEdit... try delcaring QString textEdit
The seccond error one equals means declare, two equals (==) means comparison ;)
Re: putting QTableWidgetItem (s) in a boolean array
Thank you for your time,
I only modified the header :
Code:
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QtGui>
#include <vector>
class FenPrincipale
: public QWidget{
Q_OBJECT
public:
FenPrincipale();
bool M[2][2];
private slots:
private:
};
#endif
after compiling , the program crash and stops.
Re: putting QTableWidgetItem (s) in a boolean array
Well of course, just think for a moment what you are doing...you are trying to retrieve an item from a table that its not initialized...you just declare the size of the table but the fields are never set to a valid value...
Re: putting QTableWidgetItem (s) in a boolean array
Thanks for your response ,
I am a beginner in Qt and C++ so please tell me exactly what is wrong.
Re: putting QTableWidgetItem (s) in a boolean array
It`s like real life, you claim to have a millon dollars but actually you dont (I guess, lol) in this case you delcare a table but you never declare it...YOU CANNOT USE ANYTHING YOU HAVENT DECLARED BEFORE ....try adding this before setting the rowCount...
Quote:
tableWidget = new QTableWidget (this);
its a very common mistake dont worry but try to read some more about c++ specially
Re: putting QTableWidgetItem (s) in a boolean array
Your original code creates a table widget at line 34 and a new element at line 35. The code never puts anything in the table widget. The table widget will not be seen by the user before (line 46) you start trying to extract items from the unfilled widget. At line 50 you are using the itemAt() function, which gives the cell at certain screen coordinates (it isn't visible here), when you want the item() function that returns the item in a particular table cell. Accessing the unfilled table will, at best, give you an item containing a null QVariant() value and at worst a null pointer. As KillGabio pointed out, you also try to retrieve elements from row/column 1 or 2 of a table (line 50) that only has to row/column 0 or 1. This will return a null pointer (line 50) and your subsequent (line 51) use of the pointer will crash the program.
myta212 has provided an example of loading the table widget with data..
You really need to step back from the problem, polish up your C++, look at how Qt is supposed to operate, and then break the problem up into pieces. Here is an example that show one way to approach your matrix editor (I have left your matrix data structure alone)..
Code:
#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
setRowCount(2);
setColumnCount(2);
// Default population
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
it->setData(Qt::EditRole, 0);
setItem(r, c, it);
}
}
}
void setData(bool M[2][2]) {
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
Q_ASSERT(it);
int val = M[r][c]? 1: 0;
it->setData(Qt::EditRole, val);
}
}
}
void getData(bool M[2][2]) {
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
Q_ASSERT(it);
int val = it->data(Qt::EditRole).toInt();
M[r][c] = (val == 1)? true: false;
}
}
}
};
int main(int argc, char *argv[])
{
bool M[2][2] = { {false, true}, {true, false} };
MatrixWidget m;
m.setData(M);
m.show();
return app.exec();
}
#include "main.moc"
Re: putting QTableWidgetItem (s) in a boolean array
Hello,
I tried to think differently and I came up with the fact that after filling the QTableWidget I push a QPushButton to generate the slot "FillIn" to fill the matrixe M but it does not work.
The error is : QTableWidget: cannot insert an item that is already owned by another QTableWidget
Here is the code:
Code:
//The modified header
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QtGui>
#include <vector>
class FenPrincipale
: public QWidget{
Q_OBJECT
public:
FenPrincipale();
bool M[2][2];
private slots:
void FillIn();
private:
};
#endif
//FenPrincipale.cpp
#include "qDebug.h"
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale()
{
tabM->setRowCount(2);
tabM->setColumnCount(2);
generer->move(50,50);
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
tabM->setItem(r, c, it);
it->setData(Qt::EditRole, 0);
//qDebug() <<it;
}
}
connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
}
void FenPrincipale::FillIn()
{
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
Q_ASSERT(it);
int val = it->data(Qt::EditRole).toInt();
M[r][c] = (val == 1)? true: false;
}
}
}
//main.cpp
#include <QApplication>
#include "FenPrincipale.h"
int main(int argc, char* argv[])
{
FenPrincipale fenetre;
fenetre.show();
return app.exec();
}
By the way , why after compiling only the first cell of the QTableWidget is initializes with "0" and not the others.
Thank you very much for your attention and time.
Re: putting QTableWidgetItem (s) in a boolean array
Think for a moment the error: its telling you that the *it var is used before...so basically it explains what you are doing..you need to create a new item for every cell:
Code:
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 2; c++) {
tabM->setItem(r, c, it);
it->setData(Qt::EditRole, 0);
}
}
and remove *it from the header (.h) if you are going to do it that way...and do r++/c++ instead of what you have if you are using the variables inside the for...
Re: putting QTableWidgetItem (s) in a boolean array
Hello,
Thank tou for your help , I really apreciate it.
I change the program.
Unfortunetly the variable "val" stay at "0" even if I inser "1" in the correspondant cell in the QTableWidget.
Code:
//the modified header
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QtGui>
#include <vector>
class FenPrincipale
: public QWidget{
Q_OBJECT
public:
FenPrincipale();
bool M[2][2];
private slots:
void FillIn();
private:
//QTableWidgetItem *it;
};
#endif
//FenPrincipale.cpp
//FenPrincipale.cpp
#include "qDebug.h"
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale()
{
tabM->setRowCount(2);
tabM->setColumnCount(2);
generer->move(50,50);
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 2; c++) {
tabM->setItem(r, c, it);
it->setData(Qt::EditRole, 0);
//qDebug() <<it;
}
}
connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
}
void FenPrincipale::FillIn()
{
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 2; c++) {
Q_ASSERT(it);
int val = it->data(Qt::EditRole).toInt();
M[r][c] = (val == 1)? true: false;
qDebug() <<val;
}
}
}
Can you please tell me why the value of val is not updated.
Thank you.
Re: putting QTableWidgetItem (s) in a boolean array
What item are you reading the value from? What is line 54 doing?
I suggest you go back and read my example's getData() function more closely.
Re: putting QTableWidgetItem (s) in a boolean array
Hi,
I think you must read documentation about C++ and Qt first. Your problem is very simple, and we send you code how to solve your problem.
The problem for your question is not "putting QTableWidgetItem (s) in a boolean array" but "How to Fill And Get Value from QTableWidget".
If you want to get full code for your problem, you can get at Qt Documentation.
But, please read the other answer above. You can use :
Code:
tableWidget->setItem(i,j,item);
to set Item in QTableWidget and
to get item from QTableWidget.
Please try and you will get how simple your problem.
Good luck.
Best regards,
Myta212