Eren Sönmez Eren Sönmez - 1 year ago 118
C++ Question

My simple QTableView code crashes

I wrote a simple code to try and learn about QTableView. When I build my project it doesnt give any errors but when I try to run it says:


C:\Users\Eren\Documents\build-QTableViewUygulama-Desktop_Qt_5_8_0_MSVC2015_64bit-Debug\debug\QTableViewUygulama.exe exited with code 255


or


The program has unexpectedly finished.

C:\Users\Eren\Documents\build-QTableViewUygulama-Desktop_Qt_5_8_0_MSVC2015_64bit-Debug\debug\QTableViewUygulama.exe crashed.


I have some knowledge about C++ but I dont know much about Qt. I have no idea why I keep getting this error. Here is my code

//Class Model(aracmodel.cpp)
#include "aracmodel.h"

AracModel::AracModel(QObject* parent = 0) : QAbstractTableModel(parent) {

}

int AracModel::rowCount(const QModelIndex &) const {

return 3;

}
int AracModel::columnCount(const QModelIndex &) const {

return Araclar.size();
}
QVariant AracModel::data(const QModelIndex &index, int role) const {

if(role != Qt::DisplayRole && role != Qt::EditRole) return 0;

const Arac& arac = Araclar[index.row()];

switch(index.column()){

case 0 : return arac.id;
case 1 : return QString::fromStdString(arac.marka);
case 2 : return QString::fromStdString(arac.model);
default : return 0;
}
return QVariant();
}

QVariant AracModel::headerData(int section, Qt::Orientation orientation, int
role){

if(orientation != Qt::Horizontal || role != Qt::DisplayRole) return 0;

switch(section){

case 0 : return "ID";
case 1 : return "Marka";
case 2 : return "Model";
default : return 0;
}
return QVariant();
}


//Class Model (aracmodel.h)
#ifndef ARACMODEL_H
#define ARACMODEL_H

#include <QAbstractTableModel>
#include "arac.h"

class Arac;

class AracModel : public QAbstractTableModel
{
public:
QList<Arac> Araclar;

AracModel(QObject*);

int rowCount(const QModelIndex&) const override;
int columnCount(const QModelIndex&) const override;
QVariant data(const QModelIndex& index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role);
};

#endif // ARACMODEL_H



//Class Header arac.h
#ifndef ARAC_H
#define ARAC_H

#include <string>

class Arac
{
public:
int id;
std::string marka;
std::string model;

Arac(int, std::string, std::string);
};

#endif // ARAC_H

Arac::Arac(int i, std::string ma, std::string mo) : id(i), marka(ma), model(mo){} //This constructor is in arac.cpp



//MainWindow.cpp (only copying the parts i've changed)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableView->setModel(model);
}




//MainWindow.hh(only copying the parts i've changed
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
AracModel* model;
~MainWindow();

private:
Ui::MainWindow *ui;
};

Answer Source

Instantiate model pointer using :

model = new AracModel(this);

before setting this model to tableView.

Have a look at this simple example: http://www.thedazzlersinc.com/source/2012/06/04/qt-qtableview-example-short-and-quick/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download