Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Hello!
this is the code, I wrote:
Code:
#include "widget.h"
{
this->setGeometry(30,30,800,750);
rank
= new QLineEdit("Enter the Rank",
this);
//rank->setGeometry(10,0,20,20); ok
= new QPushButton("Ok",
this); ok
->setGeometry
(150,
0,
80,
30);
calculate
= new QPushButton("calculate",
this); calculate
->setGeometry
(250,
0,
80,
30);
connect(ok,SIGNAL(clicked()),this,SLOT(ok_clicked()));
connect(calculate,SIGNAL(clicked()),this,SLOT(calculate_clicked()));
}
Widget::~Widget()
{
}
void Widget::ok_clicked()
{
rank_of_Matrix = rank->text().toDouble();
tbl->setGeometry(20,50,770,250);
tbl->show();
Action_toArray(); // wrote all information into Array A[i][j] and make 0 all elements equals to zero
}
void Widget::calculate_clicked()
{
tbl ->setGeometry(20,350,770,250);
tbl->setItem(2,2, coppy->clone());
tbl ->show();
}
void Widget::Action_toArray()
{
A= new float* [rank_of_Matrix];
for(int i=0;i<rank_of_Matrix;i++){
A[i]=new float [rank_of_Matrix];
}
for(int i=0;i<rank_of_Matrix;i++){
for(int j=0;j<rank_of_Matrix;j++){
//wrote into Array from QTableWidget
A[i][j] = tbl->item(i,j)->data(Qt::DisplayRole).toFloat();
}
}
//make all elements equals to 2
for(int i=0;i<rank_of_Matrix;i++){
for(int j=0;j<rank_of_Matrix;j++){
if(A[i][j]==2){
A[i][j]=0;
}
}
}
}
My question is: how to write changed Array into Table2 after clicking button "Calculate"? Do I get it right that I need to convert float into QTableWidgetItem and then using command tbl->setItem(rank_of_Matrix,rank_of_Matrix, coppy->clone()); in loop write it into Table?
Thank you in advance!
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Quote:
Originally Posted by
Blitzor DDD
My question is: how to write changed Array into Table2 after clicking button "Calculate"?
You only have one table, "tbl"
Quote:
Originally Posted by
Blitzor DDD
Do I get it right that I need to convert float into QTableWidgetItem
No, you format the number as a QString and set that as the table widget item's text.
You can additionally store the actual number through setData(), using Qt::EditRole.
Quote:
Originally Posted by
Blitzor DDD
and then using command tbl->setItem(rank_of_Matrix,rank_of_Matrix, coppy->clone());
No, you just create a new QTableWidgetItem, passing the text as its constructor argument.
Quote:
Originally Posted by
Blitzor DDD
in loop write it into Table?
yes
Cheers,
_
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Quote:
You only have one table, "tbl"
I meant tbl of course. I am going to use the same tbl and created it on the Widget below. Is it possible?
Quote:
setData(), using Qt::EditRole.
but setDate() has type bool. http://doc.qt.io/qt-5/qabstractitemmodel.html#setData
Quote:
No, you just create a new QTableWidgetItem, passing the text as its constructor argument. You can additionally store the actual number through setData(), using Qt::EditRole.
Could you please provide an example?
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Quote:
Originally Posted by
Blitzor DDD
I meant tbl of course. I am going to use the same tbl and created it on the Widget below. Is it possible?
If you want to create a new table every time the button is clicked and just keep the old table for viewing, sure, why not.
Quote:
Originally Posted by
Blitzor DDD
That is a different setData(), but which of the three arguments is of type bool?
I see QModelIndex, QVariant and int.
Quote:
Originally Posted by
Blitzor DDD
Could you please provide an example?
Sure, basic C++:
Code:
item->setData(Qt::EditRole, value);
Cheers,
_
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
So, I tried this, but program goes out since I click "calculate"
Code:
#include "widget.h"
#include <QAbstractItemModel>
{
this->setGeometry(30,30,800,750);
rank
= new QLineEdit("Enter the Rank",
this);
//rank->setGeometry(10,0,20,20); ok
= new QPushButton("Ok",
this); ok
->setGeometry
(150,
0,
80,
30);
calculate
= new QPushButton("calculate",
this); calculate
->setGeometry
(250,
0,
80,
30);
connect(ok,SIGNAL(clicked()),this,SLOT(ok_clicked()));
connect(calculate,SIGNAL(clicked()),this,SLOT(calculate_clicked()));
}
Widget::~Widget()
{
}
void Widget::ok_clicked()
{
rank_of_Matrix = rank->text().toDouble();
tbl->setGeometry(20,50,770,250);
tbl->show();
}
void Widget::calculate_clicked()
{
tbl_2
= new QTableWidget(rank_of_Matrix,rank_of_Matrix,
this);
tbl_2 ->setGeometry(20,350,770,250);
Action_toArray();
tbl_2 ->show();
}
void Widget::Action_toArray()
{
A= new float* [rank_of_Matrix];
for(int i=0;i<rank_of_Matrix;i++){
A[i]=new float [rank_of_Matrix];
}
//read from Table 1 tbl
for(int i=0;i<rank_of_Matrix;i++){
for(int j=0;j<rank_of_Matrix;j++){
A[i][j] = tbl->item(i,j)-> data(Qt::DisplayRole).toFloat();
}
}
//operation with Array
for(int i=0;i<rank_of_Matrix;i++){
for(int j=0;j<rank_of_Matrix;j++){
if(A[i][j]==2)
{
A[i][j]=0;
}
}
}
//write to Table2 tbl_2
for(int i=0;i<rank_of_Matrix;i++){
for(int j=0;j<rank_of_Matrix;j++){
tableData = A[i][j];
tbl_2->item(i,j)->setData(Qt::EditRole,tableData);
}
}
}
So, could you please tell me what is wrong and how I can fix it?
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
You are accessing tbl->item(i, j), where did you create these items?
You are also accessing tbl_2->item(i, j), where did you create these items?
Cheers,
_
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
I thought, that item is a function to access to element of the Table and this is not an object to create it, isn't it?
this is header-file
.h
Code:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTableWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTableWidgetItem>
{
Q_OBJECT
public:
~Widget();
private:
int rank_of_Matrix=1;
float x=0;
float **A;
private slots:
void ok_clicked();
void calculate_clicked();
void Action_toArray();
};
#endif // WIDGET_H
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Quote:
Originally Posted by
Blitzor DDD
I thought, that item is a function to access to element of the Table and this is not an object to create it, isn't it?
QTableWidget::item():
Quote:
Returns the item for the given row and column if one has been set; otherwise returns 0.
You don't seem to set items anywhere, so it is most likely that you are getting a null pointer back.
You don't check for "not null" so your crash is very likely the attempted dereferencing of that null pointer.
Cheers,
_
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Ok, I added line in .h
QTableWidgetItem * item;
and in .cpp
item = new QTableWidgetItem();
but nevetherless, here:
Code:
for(int i=0;i<rank_of_Matrix;i++){
for(int j=0;j<rank_of_Matrix;j++){
//запиÑали в маÑÑив то, что еÑть в таблице 1
A[i][j] = tbl->item(i,j)-> data(Qt::DisplayRole).toFloat();
qDebug() <<"Ð"<<i<<" "<<j<<" = "<<A[i][j];
}
}
I checked with qDebug() and it worked, data has been set into Array
and item in this loop has nothing to do with item connected to QTableWidget....
So, I still have no idea what is going on and what should I do with that...
Could you please tell me again?
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Quote:
Originally Posted by
Blitzor DDD
Ok, I added line in .h
QTableWidgetItem * item;
And you did this why?
Quote:
Originally Posted by
Blitzor DDD
and in .cpp
item = new QTableWidgetItem();
So you have a single tabel widget item stored in a member of the class.
How will that help solve your problem?
Quote:
Originally Posted by
Blitzor DDD
Code:
for(int i=0;i<rank_of_Matrix;i++){
for(int j=0;j<rank_of_Matrix;j++){
//запиÑали в маÑÑив то, что еÑть в таблице 1
A[i][j] = tbl->item(i,j)-> data(Qt::DisplayRole).toFloat();
qDebug() <<"Ð"<<i<<" "<<j<<" = "<<A[i][j];
}
}
I checked with qDebug() and it worked, data has been set into Array
Oh, maybe you have manually entered data into that table?
Quote:
Originally Posted by
Blitzor DDD
So, I still have no idea what is going on and what should I do with that...
Could you please tell me again?
Check the stack trace of your program when it crashes.
I am pretty sure it crashes in
Code:
tbl_2->item(i,j)->setData(Qt::EditRole,tableData);
because you are calling setData() on a null pointer.
Cheers,
_
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Yes, you are right.
And yes I enter it manually.
Problem was solved when I changed problem line to:
tbl_2->setItem(i,j, ptwi = new QTableWidgetItem(QString::number( A[i][j] )));
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
@BlitzorDDD: You could save yourself a lot of time and head-pounding if you used Google and looked up some of the many, many examples on how to use QTableWidget:
https://wiki.qt.io/How_to_Use_QTableWidget
http://www.codeprogress.com/cpp/libr...m=QTableWidget
and you would make many fewer mistakes if you learned how to write a good Qt application by studying the examples and tutorials on the Qt web site:
http://doc.qt.io/qt-5/qtexamplesandtutorials.html
We could spend many hours fixing each new bug you create, or you could teach yourself not to make bugs in the first place.
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Yes, yes, I would and I will.
This is just my third program on Qt.
Thank you very much for your links!
Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem
Quote:
Originally Posted by
Blitzor DDD
Yes, you are right.
And yes I enter it manually.
Which, unsurprisingly, results in items being added to the table.
Your tbl_2, on the other hand, remains empty.
Quote:
Originally Posted by
Blitzor DDD
Problem was solved when I changed problem line to:
tbl_2->setItem(i,j, ptwi = new QTableWidgetItem(QString::number( A[i][j] )));
Not sure what you have the "ptwi" in there for, but yes.
As already clarified in comment #2, btw.
Cheers,
_