Fill a QTableWidget
-
Hello everyone,
I'm trying to fill a QTablewidget with matrix of vectors, I have the following code. I'm sure that my Qt code is good but I don't know my it doesn't work. Is there something I missed out?I would really appreciate your help!
@
#include "qtablewidget.h"
#include <vector>@@class Test
{
public:
Test(){};
MainClass obj;
void setMatrix(std::vector<std::vector<double>> a){
obj.GetMatrix(a);
}
void createMatrix(){
/*
/ some code to fill the private variable matrix;
*/
this->setMatrix(this->matrix);
}
private:
std::vector<std::vector<double>> matrix;}@
@class MainClass : public QMainWindow
{
Q_OBJECT
public:
MainClass(QWidget *parent = 0, Qt::WFlags flags = 0);void GetMatrix(std::vector<std::vector<double>> p){ this->widget.tableWidget->setRowCount(p.size()); this->widget.tableWidget->setColumnCount(p[0].size()); for(unsigned int row=0;row<p.size();row++){ for(unsigned int column=0;column<p[0].size();column++){ QTableWidgetItem* newItem = new QTableWidgetItem(); newItem->setText(QString::number(p[row][column])); this->widget.tableWidget->setItem(row,column,newItem); } } }
private:
Ui::MainClass widget;
}@ -
Hi,
Where do you fill your matrix ? Are you sure it contains something ?
-
Hello both! *Thank you for reply! *
Yes, my code compile and I'm sure that my matrix contains values in void createMatrix() I can display them in the output box. My Qt code seems correct because I have tried to create a random matrix in the constructor* MainCalss()* using the same Qt code as GetMatrix and it works. But when I try to call the void GetMatrix(std::vector<std::vector<double>> p) function from *calss Test * it just doesn't display. I mean I can display the values using cout<< but my Qt code doesn't respond.
I'm wondering if declaring an object MainClass in the test.h file is correct or not??Again thank you for your HELP!!!
-
I have tried to inherit the Test class from the MainClass to avoid the use of object but it still doesn't work!
-
You don't call
@obj.show()@anywhere in you Test class. Without that no widget will be shown.
-
Thank you for your quick reply!!!
I'm sorry for taking your time. Yes, in my main I have@int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainClass w;
w.show();
return a.exec();
}@Any code from another class than MainCalss doesn't execute????
-
In that code you don't have your Test class anywhere
-
Here is the whole code and I have connected a signal to void CallMatrix() in the main class.
@
#include "MainCalss.h"
class Test
{
public:
Test(){};
static Test *New(){return new Test;}MainClass obj; void setMatrix(std::vector<std::vector<double>> a){ obj.GetMatrix(a); } void createMatrix(){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ this->row.push_back(j); } this->matrix.push_back(row); } this->setMatrix(this->matrix); }
private:
std::vector<std::vector<double>> matrix;
std::vector<double> row;
}#include "Test.h"
class MainClass : public QMainWindow
{
Q_OBJECT
public:
MainClass(QWidget *parent = 0, Qt::WFlags flags = 0);void GetMatrix(std::vector<std::vector<double>> p){ this->widget.tableWidget->setRowCount(p.size()); this->widget.tableWidget->setColumnCount(p[0].size()); for(unsigned int row=0;row<p.size();row++){ for(unsigned int column=0;column<p[0].size();column++){ QTableWidgetItem* newItem = new QTableWidgetItem(); newItem->setText(QString::number(p[row][column])); this->widget.tableWidget->setItem(row,column,newItem); } } }
public slots:
void CallMatrix(){
Test *call = Test::New();
call->createMatrix();
}private:
Ui::MainClass widget;
}@@
#include "MainClass.h"
#include "Test.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainClass w;
w.show();
return a.exec();
}@ -
In this case, I don't need to call Test class. m'I right??
-
You are updating the MainClass obj in your Test class not the one you create in main
-
I'm not familiar with OOP, is there another way how can I figure out with this.
Thank you for your help! -
Hi,
I tried to inherit Test class from MainClass to avoid the use of obj. But the problem still persist! My QTablewidget display nothing.Any hint or advice would be appreciated. Thank you for your help!!!!
@class Test : public MainClass
{
public:
Test(){};
static Test *New(){return new Test;}void setMatrix(std::vector<std::vector<double>> a){ this->GetMatrix(a); } void createMatrix(){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ this->row.push_back(j); } this->matrix.push_back(row); } this->setMatrix(this->matrix); }
private:
std::vector<std::vector<double>> matrix;
std::vector<double> row;
}@ -
Your logic is flowed,
If you really want to set this matrix from another object:
-
First rename GetMatrix to setMatrix -> GetXXX is for a getter which your function is clearly not
-
Since your using Qt I would encourage your to use Qt's container classes (it's not mandatory)
-
Refactor your test class
@
#include "MainClass.h"
namespace Test
{
void updateMainClassMatrix(MainClass *mainClass)
{
if (!mainClass) {
qCritical("Null pointer given");
}
QVector<double> row;
for(int i=0;j<4;i++){
row.push_back(i);
}
QVector<QVector<double>> matrix(4, row);
mainClass->setMatrix(matrix);
}
}@ -
Update your main.cpp
@
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainClass w;
w.show();
Test::updateMainClassMatrix(&w);
return a.exec();
}@
WARNING: code not compiled or tested but should work.
This is one way of doing what you want, maybe not the best but this should give you a starting point.
If you're not familiar with OOP, get some good books about it and read through the examples and demos from Qt, they are a good source of ideas. And since you seem to want to do testing, I would also encourage you to use TDD, it helps to learn how a framework works.
Hope it helps
-