QListview与使用qabstractlistmodel的自定义模型的问题

时间:2012-12-10 21:36:34

标签: qt qt4.7 qlistview qlist qabstractlistmodel

尝试为我的qlistview实现自定义模型。我一直在阅读类似于我的过去帖子的链接,但我还没有能够让它发挥作用。

我想列出我的对象,应该由用户通过单击“添加”按钮动态创建。要删除列表视图中的项目,用户应选择该项目,然后单击删除按钮。

编辑 - 我尝试使用继承自QAbstractListModel的自定义模型创建qlistview。 qlistview将显示Qlist,所有项目都应列在qlistview中。我还希望用户创建一个新的MyCustomObject并将其添加到Qlist中。

我尝试按照我可以通过谷歌搜索找到的示例和帖子,但此时此刻我迷路了。

单击添加按钮时应用程序崩溃。

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QList<MyCustomObject*> *m_list = new QList<MyCustomObject*>;

    CustomListModel *lmodel = new CustomListModel(this);

    MyCustomObject *object = new MyCustomObject(this);

    m_list->append(object);

    ui->listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    lmodel->setmylist(m_list);

    //Initialize the Listview
    ui->listView->setModel(lmodel);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_addpushButton_clicked()
{
    MyCustomObject *object = new MyCustomObject(this);

    lmodel->AddmycustomObject(object);
}

void MainWindow::on_removepushButto_clicked()
{

}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QListView>
#include <customlistmodel.h>
#include "mycustomobject.h"


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_addpushButton_clicked();

    void on_removepushButto_clicked();

private:
    Ui::MainWindow *ui;
    QList<MyCustomObject*> *m_list;
    CustomListModel *lmodel;


};

#endif // MAINWINDOW_H

customlistmodel.cpp

#include "customlistmodel.h"

CustomListModel::CustomListModel(QObject *parent) :
    QAbstractListModel(parent)
{
}
QVariant CustomListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= Model_list->size())
        return QVariant();

    if (role == Qt::DisplayRole)
       return QVariant(QString("row [%1]").arg((Model_list->at(index.row()))->GetName() ));
    else
        return QVariant();
}
int CustomListModel::rowCount(const QModelIndex &parent) const
{
        Q_UNUSED(parent);
        return Model_list->size();
}
//bool CustomListModel::insertRows(int row, int count, const QModelIndex &parent)
//{
//    beginInsertRows(QModelIndex(), row, row+count-1);


//    endInsertRows();

//    return true;
//}


//bool CustomListModel::removeRows(int row, int count, const QModelIndex &parent)
//{
//    beginRemoveRows(QModelIndex(), row, row+count-1);


//    endRemoveRows();

//    return true;

//}





//bool CustomListModel::setData(const QModelIndex &index, const QVariant &value, int role)
//{
//   if (index.isValid() && role == Qt::EditRole) {

//      MyCustomObject *item = Model_list->at(index.row());
//      item->SetNam(value.toString());

//     // Model_list->at(index.row())->SetNam(value.toString());
//      emit dataChanged(index,index);
//      return true;
//   }
//   return false;

//}
//Custom Methods
void CustomListModel::setmylist(QList<MyCustomObject*> *list)
{
    beginResetModel();

    Model_list = list;

    endResetModel();
    reset();

}
void CustomListModel::AddmycustomObject(MyCustomObject *object)
{

    int first = Model_list->count();
    int last = first;

          beginInsertRows(QModelIndex(), first, last);
          Model_list->append(object);
          endInsertRows();

}

CustomListModel.h

#ifndef CUSTOMLISTMODEL_H
#define CUSTOMLISTMODEL_H

#include <QAbstractListModel>
#include <QList>
#include "mycustomobject.h"

class CustomListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit CustomListModel(QObject *parent = 0);
    QVariant data(const QModelIndex &index, int role) const;
    int rowCount(const QModelIndex &parent) const;
   // bool setData(const QModelIndex &index, const QVariant &value, int role);

   // bool insertRows(int row, int count, const QModelIndex &parent);
    //bool removeRows(int row, int count, const QModelIndex &parent);

    //Custom Methods
    void setmylist(QList<MyCustomObject*> *list);
    void AddmycustomObject(MyCustomObject *object);
private:
    QList<MyCustomObject*>* Model_list;

signals:

public slots:

};
#endif // CUSTOMLISTMODEL_H

MyCustomObject.h

#ifndef MYCUSTOMOBJECT_H
#define MYCUSTOMOBJECT_H

#include <QObject>

class MyCustomObject : public QObject
{
    Q_OBJECT
public:
    explicit MyCustomObject(QObject *parent = 0);
    QString Name;
    QString GetName() {return Name;}
    void SetNam(QString Value){ Name = Value;}
signals:

public slots:

};

#endif // MYCUSTOMOBJECT_H

mycustomobject.cpp

#include "mycustomobject.h"

MyCustomObject::MyCustomObject(QObject *parent) :
    QObject(parent)
{
    this->Name = "TestName";
}

的main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.ui enter image description here

1 个答案:

答案 0 :(得分:3)

@DanielCastro在主评论中提到了这一点,但我正在扩展它,因为我认为这可能是你真正的崩溃。

在MainWindow中,您声明模型的私有成员:

private:
    ...
    CustomListModel *lmodel;

但是在您的MainWindow构造函数中,您初始化一个全新的模型并将其设置在您的视图上:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ...
    // Not actually initializing the private member
    CustomListModel *lmodel = new CustomListModel(this);
    ...

    lmodel->setmylist(m_list);

    //Initialize the Listview
    ui->listView->setModel(lmodel);
}

这使您的私有lmodel成为空指针。然后你有一个试图访问它的插槽:

void MainWindow::on_addpushButton_clicked()
{
    MyCustomObject *object = new MyCustomObject(this);
    // CRASH because you access a NULL pointer
    lmodel->AddmycustomObject(object);
}

所以修复可能是为了确保你在MainWindow构造函数中初始化私有成员而不是一个全新的模型:

lmodel = new CustomListModel(this);

您与m_list做同样的事情,所以:

m_list = new QList<MyCustomObject*>;

现在看起来您可能会在您的课程中使用QList指针而不仅仅是QList时遇到潜在的崩溃。我认为最好确保你的模型有一个完全初始化的列表对象,然后你可以设置它或从其他列表中清除它。

private:
    QList<MyCustomObject*> Model_list;

然后你会把它们传递给:

void setmylist(QList<MyCustomObject*> &list);