gpt4 book ai didi

c++ - 我的简单 QTableView 代码崩溃了

转载 作者:行者123 更新时间:2023-11-28 01:44:32 25 4
gpt4 key购买 nike

我写了一个简单的代码来尝试和了解 QTableView。当我构建我的项目时它没有给出任何错误但是当我尝试运行它时它说:

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

The program has unexpectedly finished.

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

我对 C++ 有一些了解,但对 Qt 了解不多。我不知道为什么我一直收到这个错误。这是我的代码

//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;
};

最佳答案

实例化模型指针使用:

model = new AracModel(this);

在将此模型设置为 tableView 之前。

看看这个简单的例子: http://www.thedazzlersinc.com/source/2012/06/04/qt-qtableview-example-short-and-quick/

关于c++ - 我的简单 QTableView 代码崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45696015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com