尝试为我的 qlistview 实现自定义模型。我一直在阅读与我类似的过去帖子的链接,但我无法让它工作。
我想列出我应该由用户通过单击添加按钮动态创建的对象。要删除列表视图上的项目,用户应选择该项目,然后单击删除按钮。
编辑-我正在尝试使用继承自QAbstractListModel的自定义模型创建一个 qlistview 。qlistview 将显示 Qlist,所有项目都应在 qlistview 中列出。我还希望用户创建一个新的 MyCustomObject 并将其添加到 Qlist。
我尝试按照可以通过谷歌搜索找到的示例和帖子进行操作,但此时我迷路了。
单击添加按钮时应用程序崩溃。
主窗口.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()
{
}
主窗口.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();
}
自定义列表模型.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
我的自定义对象.cpp
#include "mycustomobject.h"
MyCustomObject::MyCustomObject(QObject *parent) :
QObject(parent)
{
this->Name = "TestName";
}
主文件
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.ui