@DanielCastro touched on this in the main comment, but I am expanding on it as I think it might be your actual crash.
In your MainWindow, you declare a private member for the model:
private:
...
CustomListModel *lmodel;
But in your MainWindow constructor, you initialize a brand new model and set it on your view:
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);
}
This leaves your private lmodel
as a null pointer. Then you have a slot that tries to access it:
void MainWindow::on_addpushButton_clicked()
{
MyCustomObject *object = new MyCustomObject(this);
// CRASH because you access a NULL pointer
lmodel->AddmycustomObject(object);
}
So the fix would probably be to make sure you are initializing the private member and not a brand new model in the MainWindow constructor:
lmodel = new CustomListModel(this);
You are doing the same thing with your m_list
, so also:
m_list = new QList<MyCustomObject*>;
it also seems like you might have potential crashes now or down the line from using a QList
pointer instead of just a QList
in your classes. I would think it would be better to make sure your model has a fully initialized list object, and then you can set into it or clear it from other lists.
private:
QList<MyCustomObject*> Model_list;
And then you would pass them like:
void setmylist(QList<MyCustomObject*> &list);