Qt - adding elements to map and memory leaks - c++

I am working on a matrix calculator and I encountered a really annoying problem, have been trying to fix it for 3 hours now, but it's getting worse instead of getting better. Maybe you will be able to help.
This is my MainWindow class:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include "addmatrix.h"
#include "memory.h"
#include "matrixcreation.h"
#include <QMap>
#include "matrix.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(MainWindow *parent = 0);
~MainWindow();
private slots:
void on_createButton_clicked();
void setMatrix(Matrix *matrix);
private:
Matrix getMatrixFromMemory(QString &name);
void addMatrixToMemory();
Ui::MainWindow *ui;
AddMatrix *add;
MatrixCreation *matrixCreate;
QMap <QString, Matrix> matrixMap;
};
#endif // MAINWINDOW_H
.cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(MainWindow *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Matrix Calculator");
add = NULL;
matrixCreate = NULL;
}
MainWindow::~MainWindow()
{
delete ui;
delete add;
delete matrixCreate;
matrixMap.clear();
}
void MainWindow::on_createButton_clicked()
{
if (add != NULL)
{
delete add;
add = new AddMatrix;
}
else
add = new AddMatrix;
if (matrixCreate != NULL)
{
ui->label->setText(getMatrixFromMemory(matrixCreate->getMatrix()->getName()).getName());
delete matrixCreate;
matrixCreate = new MatrixCreation;
}
else
matrixCreate = new MatrixCreation;
// Polaczenie sygnalow ze slotami
// Sluzy ustawieniu liczby wierszy w matrixCreate
connect(add->getCombo1(), SIGNAL(currentIndexChanged(QString)), matrixCreate, SLOT(setRows(QString)));
// Jak wyzej, tylko kolumn
connect(add->getCombo2(), SIGNAL(currentIndexChanged(QString)), matrixCreate, SLOT(setColumns(QString)));
// Ustawienie pola name w matrixCreate
connect(add->getEdit(), SIGNAL(textChanged(QString)), matrixCreate, SLOT(setName(QString)));
// Po ustawieniu liczby wierszy i kolumn oraz nazwy macierzy - wywola sie slot updateTable
// ktory sluzy do ustawienia rozmiaru okna i tabeli
connect(add, SIGNAL(setupSuccessful()), matrixCreate, SLOT(updateTable()));
// Po ustawieniu wierszy, kolumn, ustawieniu nazwy, rozmiarow, wywola sie slot show matrixCreate
connect(add, SIGNAL(setupSuccessful()), matrixCreate, SLOT(show()));
// Sluzy dodaniu macierzy do pamieci (mapy)
connect(matrixCreate, SIGNAL(matrixReady(Matrix*)), this, SLOT(setMatrix(Matrix*)));
add->show();
}
void MainWindow::setMatrix(Matrix *matrix)
{
matrixMap[matrix->getName()] = *matrix;
}
Matrix MainWindow::getMatrixFromMemory(QString &name)
{
return matrixMap[name];
}
As you can see I have a QMap in my MainWindow. I also have a an instance of MatrixCreation class called matrixCreate.
Here is MatrixCreation class:
#ifndef MATRIXCREATION_H
#define MATRIXCREATION_H
#include <QDialog>
#include <QTableView>
#include <QPushButton>
#include <QStandardItem>
#include <QTableWidget>
#include <QMainWindow>
#include "matrix.h"
#include "memory.h"
#include "addmatrix.h"
#include <windows.h>
namespace Ui {
class MatrixCreation;
}
class MatrixCreation : public QWidget
{
Q_OBJECT
public:
explicit MatrixCreation(QWidget *parent = 0);
~MatrixCreation();
QTableWidget *getTable();
Matrix *getMatrix();
private slots:
void on_okButton_clicked();
void updateTable();
void setRows(QString rows);
void setColumns(QString columns);
void setName(QString name);
signals:
void matrixReady(Matrix *matrix);
private:
Ui::MatrixCreation *ui;
int rows;
int columns;
int **matrix;
QString name;
Matrix *newMatrix;
};
#endif // MATRIXCREATION_H
.cpp file
#include "matrixcreation.h"
#include "ui_matrixcreation.h"
MatrixCreation::MatrixCreation(QWidget *parent) :
QWidget(parent),
ui(new Ui::MatrixCreation)
{
ui->setupUi(this);
this->resize(150, 50);
// Ustawienie rozmiaru wysokosci wiersza na 40 piksele
ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
// Ustawienie rozmiaru szerokosci kolumny na 94 piksele
ui->tableWidget->horizontalHeader()->setDefaultSectionSize(94);
// Dopasowanie rozmiaru kolumn do rozmiaru tabeli
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// Wylaczenie scrollbarow tabeli
ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
rows = 1;
columns = 1;
matrix = NULL;
newMatrix = new Matrix;
}
MatrixCreation::~MatrixCreation()
{
delete ui;
if (matrix != NULL)
{
for (int i = 0; i < rows; i++)
delete matrix[i];
}
delete newMatrix;
}
void MatrixCreation::setRows(QString rows)
{
this->rows = rows.toInt();
}
void MatrixCreation::setColumns(QString columns)
{
this->columns = columns.toInt();
}
// Metoda odpowiedzialna za utworzenie odpowiedniej ilosci wierszy i kolumn
// oraz ustawienie odpowiedniej wielkosci okna i rozmiaru tabeli
void MatrixCreation::updateTable()
{
// stale roznice miedzy szerokoscia i wysokoscia okna i tabeli
int w = 323 - 305;
int h = 300 - 227 + 15;
for(int i = 0; i < rows; i++)
{
ui->tableWidget->insertRow(i);
h += 35;
}
for(int j = 0; j < columns; j++)
{
ui->tableWidget->insertColumn(j);
w += 50;
}
this->resize(w, h);
this->setMinimumSize(w, h);
ui->retranslateUi(this);
this->setWindowTitle("Matrix Creation");
}
QTableWidget *MatrixCreation::getTable()
{
return ui->tableWidget;
}
void MatrixCreation::on_okButton_clicked()
{
matrix = new int *[rows];
for(int j = 0; j < rows; j++)
{
matrix[j] = new int[columns];
}
for(int i = 0; i<rows; i++)
{
for(int j = 0; j<columns; j++)
{
matrix[i][j] = ui->tableWidget->item(i, j)->text().toInt();
}
}
// Ustawienie pol skladowych w zmiennej newMatrix klasy Matrix
newMatrix->setColumns(columns);
newMatrix->setRows(rows);
newMatrix->setName(name);
newMatrix->setMatrix(matrix);
emit matrixReady(newMatrix);
this->close();
}
void MatrixCreation::setName(QString name)
{
this->name = name;
}
Matrix *MatrixCreation::getMatrix()
{
return newMatrix;
}
What is the problem:
I want to add a created matrix to a QMap by emiting this signal:
emit matrixReady(newMatrix);
a Matrix class is used to hold all the elements of matrix (rows, cols, values of cells and name).
However, Matrix objects are not being added to QMap. But, when I delete this line in MatrixCreation destructor:
delete newMatrix;
It works.
Second problem:
When I am closing my application and MainWindow destructor gets called, while destroying a map it shows an error : BLOC_TYPE_IS_VALID bla bla...
I don't know how to fix it, however I will keep on trying. Any help will be appreciated.

void MainWindow::setMatrix(Matrix *matrix)
{
matrixMap[matrix->getName()] = *matrix;
}
You're adding copy of the matrix to your map. So here you have a leak.
void MainWindow::setMatrix(Matrix *matrix)
{
matrixMap[matrix->getName()] = *matrix;
delete matrix;
}
This should fix your problem.
Use valgrind to profile your application and find memory leaks: http://valgrind.org/docs/manual/quick-start.html

Related

Signals/slots Qt5 C++

I have established a connection with the slot, the function is called, and the values ​​via qDebug () are output, but the table does not change, what is wrong?
mainwindow.cpp
<pre>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->horizontalHeader()->hide();
ui->tableWidget->verticalHeader()->hide();
//Matrix *matr=new Matrix;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_updateTbl(int **mas, int n){
for(int i=0;i<n;i++){
ui->tableWidget->insertRow(ui->tableWidget->rowCount());
for(int j=0;j<n;j++){
ui->tableWidget->insertColumn(ui->tableWidget->columnCount());
ui->tableWidget->setItem(i,j,new QTableWidgetItem( QString::number(mas[i][j])));
}
}
}
</pre>
main.cpp
<pre>
#include "mainwindow.h"
#include <QApplication>
#include "matrix.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
Matrix matr;
MainWindow mywnd;
QObject::connect(&matr,SIGNAL(updateTbl(int**,int)), &mywnd, SLOT(on_updateTbl(int**,int)));
matr.upTable();
return a.exec();
}
</pre>
matrix.cpp
<pre>
#include "matrix.h"
#include <QFile>
#include <QDebug>
#include <QString>
#include <QTextStream>
Matrix::Matrix()
{
QFile file("mas.txt");
this->mas=alloc_mem(n,n);
array_to_file(file,n,n);
fill_array(file,mas,n,n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if (mas[i][j]!=mas[n-j-1][n-i-1]) {
symmetrical=false;
break;
}
}
}
if(symmetrical){
for(int i=0;i<n;i++){
mas[i][i]=0;
mas[i][n-i-1]=0;
}
//print(mas,n,"Измененная");
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mas[i][j]) vec.append(mas[i][j]);
}
}
}
int** Matrix::alloc_mem(int height, int width){
int** mas = new int*[height];
for (int i = 0; i < height; i++) {
mas[i] = new int[width];
}
return mas;
}
void Matrix::array_to_file(QFile &file, int height, int width){
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
int to_mas;
QTextStream out(&file);
for (int i = 0; i < height; i++){
for (int j = i + 1; j < width; j++){
to_mas=rand()%100-50;
out<<to_mas<<" ";
}
out<<1<<" ";
out<<"\n";
}
file.close();
}
void Matrix::fill_array(QFile &file, int **mas, int height, int width){
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QStringList matr = in.readAll().split("\n");
for (int i = 0; i < height; i++){
for (int j = 0; j < width-i; j++){
mas[i][j]=matr.at(i).split(" ").at(j).toInt();
mas[height-1-j][width-1-i]=mas[i][j];
}
}
file.close();
}
void Matrix::print(int **mas, int n, QString name){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
}
}
}
void Matrix::upTable(){
emit updateTbl(mas,n);
}
</pre>
mainwindow.h
<pre>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_updateTbl(int**,int);
};
#endif // MAINWINDOW_H
</pre>
matrix.h
<pre>
#ifndef MATRIX_H
#define MATRIX_H
#include <QVector>
#include <QFile>
#include <QString>
#include <QObject>
class Matrix:public QObject
{
Q_OBJECT
public:
Matrix();
~Matrix(){}
//bool check_symmetrical(int **mas,int n);
void print_vector(QVector<int> vector);
int** alloc_mem(int height,int width);
void array_to_file(QFile& file,int height,int width);
void fill_array(QFile& file,int **mas,int height,int width);
void print(int** mas,int n,QString name);
void upTable();
private:
int n=10;
int **mas;
bool symmetrical=true;
QVector<int> vec;
signals:
void updateTbl(int**,int);
};
#endif // MATRIX_H
</pre>
If I copy this code into the constructor, then everything is fine and everything changes, but nothing works from the function
How do you want to see table with values when widget with updated table is not displayed?
MainWindow w;
w.show(); // <--- w is displayed, tableWidget is not modified in this widget
Matrix matr;
MainWindow mywnd; // <--- mywnd is not displayed
QObject::connect(&matr,SIGNAL(updateTbl(int**,int)), &mywnd, SLOT(on_updateTbl(int**,int)));
// mywnd is updated
matr.upTable();
add
mywnd.show();
you will see the other widget with updated content.

How to do (dynamic) offscreen render with QOffscreenSurface in Qt5 correctly?

I am try to do some offscreen rendering jobs in Qt5.3 and I want to use QOpenGLFramebufferObject::toImage to output each pictures(I want to output a few pictures when render() draws different things).
Following the instructions of this, I succeeded in offscreen rendering my first pic and outputing it. So to future on, I write an example as the following code and here is a package in Google. I can get the first fbo content(and its output file correctly, but from the second time, the fbo was not re-render() and it always output the same pictures).
So I want to know what should I do after I finish one time offscreen render to make sure the next time would be correct in qt? Or is there anyone could tell me how to set animation correctly in QOffscreenSurface?
qtestofffscreen.h:
#ifndef QTESTOFFSCREEN_H
#define QTESTOFFSCREEN_H
#include <QOffscreenSurface>
#include <QWindow>
#include <QtGui/QOpenGLFunctions_3_3_Core>
#include <QImage>
#include <QGLFramebufferObject>
#include <QOpenGLPaintDevice>
#include <QOpenGLFunctions>
#include <QMutex>
#include <QMutexLocker>
class QTestOffScreen : public QOffscreenSurface,
protected QOpenGLFunctions_3_3_Core
{
Q_OBJECT
public:
explicit QTestOffScreen(
QScreen* targetScreen = nullptr,
const QSize& size = QSize (1, 1));
~QTestOffScreen();
virtual void render();
virtual void initialize();
void renderNow();
void setAnimating(bool animating);
bool event(QEvent *) override;
void renderLater();
int counts;
private:
QGLFramebufferObject *fbo;
bool m_animating;
bool m_update_pending;
QOpenGLContext *m_context;
QOpenGLPaintDevice *m_device;
QSize m_size;
QMutex mutex;
signals:
void doneImg(int index);
};
#endif // QTESTOFFSCREEN_H
qtestofffscreen.cpp:
#include "qtestoffscreen.h"
#include <QTime>
#include <QDebug>
#include <QCoreApplication>
#include <QOpenGLFramebufferObject>
QTestOffScreen::QTestOffScreen(QScreen *targetScreen,
const QSize &size):
QOffscreenSurface(targetScreen),
m_size(size),
fbo(Q_NULLPTR),
m_context(Q_NULLPTR),
m_device(Q_NULLPTR),
counts(100)
{
requestedFormat().setVersion(3,3);
setFormat(requestedFormat());
create();
m_context = new QOpenGLContext(this);
m_context->setFormat(format());
if (m_context->create())
{
m_context->makeCurrent(this);
m_context->functions()->initializeOpenGLFunctions();
}else
{
delete m_context;
m_context = Q_NULLPTR;
throw ("Still wrong here");
}
//To make sure m_context was initialized
//in first time entering renderNow()
delete m_context;
m_context = Q_NULLPTR;
}
QTestOffScreen::~QTestOffScreen()
{
delete m_context;
delete m_device;
if (fbo)
delete fbo;
}
void QTestOffScreen::render()
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
| GL_TEXTURE_BIT);
glViewport(0,0,1920,1080);
glOrtho(0,1920,0,1080,0,1);
glColor3f(1.0,0.0,0.0);
float tmp = float(qrand()%1000);
float count = (float)counts * 10.0f;
glLineWidth(3.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(count ,count );
glVertex2f(count + 100,count);
glVertex2f(count + 50,count + 100);
glEnd();
qDebug()<<QString("current tmp is %1").arg(count);
}
void QTestOffScreen::initialize()
{
if (!fbo)
{
fbo = new QGLFramebufferObject(1920,1080,GL_TEXTURE_2D);
}
fbo->bind();
}
void QTestOffScreen::renderNow()
{
bool needsInitialize = false;
if (!m_context)
{
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
if (m_context->isValid())
{
qDebug()<<"Right Here when creating m_context in renderNow";
}
needsInitialize = true;
}
if ( !m_context->makeCurrent(this) )
{
qDebug()<<"This fails in makeCurrent";
}
if (needsInitialize)
{
initializeOpenGLFunctions();
initialize();
}
render();
qDebug()<<counts;
counts--;
fbo->toImage().save(QString::number(counts) + QString(".png"));
m_context->doneCurrent();
if (counts >= 0)
{
m_update_pending = false;
emit doneImg(counts);
}
}
void QTestOffScreen::setAnimating(bool animating)
{
m_animating = animating;
m_update_pending = false;
if (m_animating)
renderLater();
}
bool QTestOffScreen::event(QEvent *event)
{
switch (event->type())
{
case QEvent::UpdateRequest:
m_update_pending = true;
renderNow();
return true;
default:
return QOffscreenSurface::event(event);
}
}
void QTestOffScreen::renderLater()
{
if (!m_update_pending)
{
m_update_pending = true;
QCoreApplication::postEvent(this,new QEvent(QEvent::UpdateRequest));
}
}
void QTestOffScreen::generateImg(QImage *tmp_img_pointer)
{
GLint viewPort[4]={0};
glGetIntegerv(GL_VIEWPORT,viewPort);
int win_width,win_height;
win_width = 1920;
win_height = 1080;
GLubyte *colorArr=new GLubyte[win_width*win_height*3];
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadBuffer (GL_FRONT);
int tmp_x,tmp_y;
tmp_x = 0;
tmp_y = 0;
glReadPixels(tmp_x,tmp_y,win_width,win_height,
GL_RGB,GL_UNSIGNED_BYTE,colorArr);
int winrows = tmp_img_pointer->height();
int wincols = tmp_img_pointer->width ();
for(int ii=0; ii < winrows * wincols * 3; ii ++)
{
if((colorArr[ii] <0)|(colorArr[ii] >255))
{ colorArr[ii] = 255; }
}
for(int j=0;j<winrows;j++)
for(int i=0;i<wincols;i++)
{
int index=(j*wincols+i)*3;
QRgb value=qRgb(colorArr[index+2],
colorArr[index+1],
colorArr[index ]);
tmp_img_pointer->setPixel(i,winrows-j-1,value);
}
delete colorArr;
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qtestoffscreen.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTestOffScreen *scr;
private slots:
void ReceiveCurrentIndex(int);
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSurfaceFormat format;
format.setSamples(1);
format.setRenderableType(QSurfaceFormat::OpenGL);
scr = new QTestOffScreen();
connect(scr,SIGNAL(doneImg(int)),this,SLOT(ReceiveCurrentIndex(int)));
scr->setFormat(format);
scr->setAnimating(true);//Start rendering animation here.
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ReceiveCurrentIndex(int)
{
Sleep(100);
scr->renderLater();
}

Errors connected with creating QMathGL class in application

I have a problem with MathGL library and creating MathGL instance in the application. Every time I try to run it there is an error saying that QApplication must be constructed before QWidget (from which the class QMathGL inherits). Below you can find my code of main and all the functions connected with the MainWindow class:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include "surface.h"
#include "error.h"
#include <QString>
#include <QFileDialog>
#include <GL/glut.h>
#include <cmath>
#include <fstream>
#include <mgl2/qmathgl.h>
surface graph;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_draw_clicked()
{
string s;
bool correct = 1;
error *errorWindow = new error(this);
s = ui->eqEdit->text().toStdString();
period fPeriod = period(ui->upEdit->text().toDouble(),ui->lowEdit->text().toDouble());
functionSum fSum = functionSum(s,fPeriod,ui->accEdit->text().toDouble());
for (int i = 0; i < fSum.accuracy; i++)
{
for (int j = 0; j < fSum.accuracy; j++)
{
if(isnan(fSum.zValues[i][j]) || isinf(fSum.zValues[i][j]))
{
correct = 0;
}
}
}
if(!correct)
{
errorWindow->show();
}
graph = surface(fSum);
}
void MainWindow::on_save_clicked()
{
QString plik;
string splik;
plik = QFileDialog::getSaveFileName(this,"Zapisz plik",QString(),"Pliki tekstowe (*.txt)");
splik = plik.toStdString();
ofstream saver;
saver.open(splik.c_str());
if (saver.is_open())
{
for(int i = 0; i < graph.points.size(); i++)
{
saver << graph.points[i].x << " " << graph.points[i].y << " " << graph.points[i].z << endl;
}
saver.close();
}
}
void MainWindow::on_load_clicked()
{
QString plik;
string splik;
plik = QFileDialog::getOpenFileName(this,"Otwórz plik",QString(),"Pliki tekstowe (*.txt)");
splik = plik.toStdString();
ifstream loader;
loader.open(splik.c_str());
graph.points.clear();
if(loader.is_open())
{
while(!loader.eof())
{
point a(0,0,0);
loader >> a.x >> a.y >> a.z;
graph.points.push_back(a);
}
loader.close();
}
}
void MainWindow::on_plot_clicked()
{
int a = sqrt(graph.points.size());
mglData x(a,a);
mglData y(a,a);
mglData z(a,a);
long long int k = 0;
for (long long int i = 0;i<sqrt(graph.points.size());i++)
{
for (long long int j = 0;j<sqrt(graph.points.size());j++)
{
x.a[i+a*j] = graph.points[k].x;
y.a[i+a*j] = graph.points[k].y;
z.a[i+a*j] = graph.points[k].z;
k++;
}
}
mglGraph plot;
plot.Rotate(50,60);
plot.Light(true);
plot.Surf(x,y,z);
QMathGL plotter;
plotter.setGraph(&plot);
plotter.show();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

QT QStandardItemModel segfaulting

I'm pretty new to QT, and I'm trying to take a list from a text file and output it into QT with nice formatting.
I managed to get the list printed on the window, but it has to be able to be sorted.
I have the radio buttons set up right now so that one of them displays the list and the other clears the list.
The problem is that when I switch from the list to the cleared list back to the list the program segfaults and I don't understand why.
The files are here.
winelist.cpp
#include "winelist.h"
#include "ui_winelist.h"
#include <QFile>
#include <QString>
#include <QStandardItemModel>
wineList::wineList(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wineList)
{
ui->setupUi(this);
ui->ratingButton->setChecked(true);
fillList();
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Wine Name")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Vintage")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Rating")));
model->setHorizontalHeaderItem(3, new QStandardItem(QString("Price")));
ui->listOutput->setModel(model);
}
wineList::~wineList()
{
delete ui;
}
void wineList::on_sortButton_clicked()
{
if( ui->ratingButton->isChecked())
{
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
model->setItem(i,1,wList[i].vintage);
model->setItem(i,2,wList[i].rating);
model->setItem(i,3,wList[i].price);
}
}
else
{
for(int i = 0; i < 100; i++) {
for(int j = 0; j < 4; j++) {
model->setItem(i, j, new QStandardItem(QString("")));
}
}
}
ui->listOutput->resizeColumnsToContents();
ui->listOutput->resizeRowsToContents();
}
void wineList::fillList()
{
Wine wine;
QString line;
QStringList lineElements;
QFile wineText(":/winelist.txt");
if (wineText.open(QIODevice::ReadOnly))
{
while ((line = line.fromUtf8(wineText.readLine())) != "")
{
lineElements = line.split(";");
lineElements[0].replace("\t", "");
lineElements[1].replace("\t", "");
wine.wineName = new QStandardItem(QString(lineElements.at(0)));
wine.vintage = new QStandardItem(QString(lineElements.at(1)));
wine.rating = new QStandardItem(QString::number(lineElements.at(2).toInt()));
wine.price = new QStandardItem(QString::number(lineElements.at(3).toInt()));
wList.append(wine);
}
}
wineText.close();
}
winelist.h
#ifndef WINELIST_H
#define WINELIST_H
#include <QMainWindow>
#include <QStandardItem>
#include <QStandardItemModel>
namespace Ui {
class wineList;
}
struct Wine {
QStandardItem* wineName;
QStandardItem* vintage;
QStandardItem* rating;
QStandardItem* price;
};
class wineList : public QMainWindow
{
Q_OBJECT
public:
explicit wineList(QWidget *parent = 0);
~wineList();
private slots:
void on_sortButton_clicked();
private:
Ui::wineList *ui;
QVarLengthArray<Wine> wList;
QStandardItemModel *model = new QStandardItemModel(100, 4, this);
void fillList();
void printList(QStandardItemModel *model);
};
#endif // WINELIST_H
main.cpp
#include "winelist.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
wineList w;
w.show();
return a.exec();
}
Clicking sort the first time
Switching Radio Button and clicking sort again
Switching Radio Button back and clicking sort again
Any Help is appreciated, I am completely lost here.
In the on_sortButton_clicked you're trying to read data from a list, but not doing any range checks. Instead, you've hardcoded 100 there.
You should rewrite this:
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
to this:
for (int i = 0; i < wList.size(); i++) {
model->setItem(i,0,wList[i].wineName);
--upd---
When you initially populate your model, it takes ownership over items from wList. When you replace model items with empty ones, it deletes initial items from wList. After this your wList is no move valid, because it contains Wine structs with dangling pointers. That's why when you try to populate your model second time, it crashes.

Qt / C++ - qDebug << class

How do I make qDebug print whether my class exists or not, or information about that class?? Can't believe there's nothing about this on the internet. I need to make sure that my ink = new InkSpot(this;) is actually returning something valid.
ink = new InkSpot(this);
qDebug << ink;
X:\Development\InkPuppet\inkpuppet.cpp:70: error: C3867: 'QMessageLogger::debug': function call missing argument list; use '&QMessageLogger::debug' to create a pointer to member
I've tried using QMessageLogger but just get various errors.
My program is crashing because I do something to ink.widget, so I'll post the code for ink
The code calling it is this:
header:
InkSpot *ink;
void InkPuppet::testButton()
{
ink = new InkSpot(this);
qDebug() << ink->widget;
ui->testButton->setText("working");
}
inkspot.h
#ifndef INKSPOT_H
#define INKSPOT_H
#include <QObject>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QLabel>
namespace Ui {
class InkSpot;
}
class InkSpot : public QWidget
{
Q_OBJECT
public:
explicit InkSpot(QWidget *parent = 0);
void draw(QPainter *painter);
QWidget *widget;
QLabel *label;
signals:
public slots:
protected:
void paintEvent(QPaintEvent *event);
private:
Ui::InkSpot *ui;
};
#endif // INKSPOT_H
inkspot.cpp
#include "inkspot.h"
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
InkSpot::InkSpot(QWidget *parent) :
QWidget(parent)
{
}
void InkSpot::paintEvent(QPaintEvent *event)
{
QFile *brushInput; //takes raw 8 bit grayscale image, 8 bit values only
char *brushProto;
uchar *brushData;
brushInput = new QFile("x:\\Development\\InkPuppet\\brush.raw"); //open the raw file
brushInput->open(QIODevice::ReadOnly);
QDataStream in;
in.setDevice(brushInput);
int size = brushInput->size(); //set size to length of raw file
brushProto = new char[size];
in.readRawData(brushProto, size); //read file into prototype
brushData = new uchar[size];
for(int i = 0; i < size; ++i)
{
brushData[i] = (uchar)brushProto[i]; //copy char to uchar array
}
QImage test(brushData, 128, 128, QImage::Format_Indexed8);
QImage test2(128, 128, QImage::Format_ARGB32);
QVector<QRgb> vectorColors(256); //create color table
for(int c = 0; c < 256; c++)
{
vectorColors[c] = qRgb(c, c, c);
}
test.setColorTable(vectorColors);
for(int iX = 0; iX < 100; ++iX)
{
for(int iY = 0; iY < 100; ++iY)
{
test2.setPixel(iX, iY, qRgba(255 - (qrand() % 100), 0 + (qrand() % 100), 0 + (qrand() % 100), qAbs((int)test.pixel(iX, iY)-255)));
}
}
//final conversion for stencil and color brush
QPixmap testPixmap = QPixmap::fromImage(test2);
QPixmap testPixmap2 = QPixmap::fromImage(test);
QPainter painter(this);
painter.drawPixmap(150, 50, 100, 100, testPixmap);
painter.drawPixmap(50, 50, 100, 100, testPixmap2);
delete[] brushProto;
delete[] brushData;
delete brushInput;
}
Not trivial what you want to do. That qDebug() displays something useful you have to implement the 'operator<<'. The following is a rough example, how this could look like:
QDebug inline operator<<(QDebug d, const InkSpot &f){
QDebug nsp = d.nospace();
nsp << f.label->text();
nsp << "\n";
return d;
}
This prints your InkSpot's label text. Add whatever else information your need. Put above code in your InkSpot.h file, but outside the class definition. If your need to access InkSpots private data, you must make QDebug operator<< a friend of InkSpot.