Unsolved Assign QStringList model object to listview
-
Hello everyone,
I have created a class :
fili .h#ifndef LISTVIEW_H #define LISTVIEW_H #include <QWidget> #include <QDialog> #include <QStringListModel> #include <QStringList> #include <QAbstractItemView> #include <QListView> class listview : QDialog { public: listview(const QStringList &leaders, QWidget *parent); void insert(); void del(); QStringList leaders() const; private: QListView *listView; QStringListModel *model; };
.cpp file
#include "listview.h" listview::listview(const QStringList &leaders, QWidget *parent):QDialog(parent){ model = new QStringListModel(this); model->setStringList(leaders); listView = new QListView; listView->setModel(model); listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked); } void listview::insert(){ int row = listView->currentIndex().row(); model->insertRows(row,1); QModelIndex index = model->index(row); listView->setCurrentIndex(index); } void listview::del(){ model->removeRows(listView->currentIndex().row(),1); } QStringList listview::leaders() const{ return leaders(); }
Know in my view I have a QListWidget, with 3 buttons Add, delete and insert buttons, I want them to run with methods of my class, then in my Mainwindow I should instantiate an object of this class, but I don't know how to link my object to my QListView, can someone help me?
Thanks
-
class MainWindow... { private: listview *view; } ... MainWindow::MainWindow(...) { view = new listview(); connect(ui->insertButton, &QPushButton::clicked, view, &listview::insert); // same for other 2 buttons }
-
@jss193 said in Assign QStringList model object to listview:
Know in my view I have a QListWidget, with 3 buttons Add
QListView
!=QListWidget
.- you never give a parent, add to a layout or show your view so I'm not sure how the list view is displayed at all
-
Hello, thanks for your help, but problem remains still the same,
I think that I did not explain good enough, I have created that generic class shown above and afterwards I created a second view which prompts from a first ui, this second ui view contains a listView Widget whose I want to glue with my base class listview, so, First thing I tried was to create an object listview *aaaa;, then tried this, ui->listView->setModel(model), but I can't cause of model var from listview class is private.How could I do this?
thanks!
-
Hi,
Then why not make the model a parameter of your dialog ? That way you can more easily share it between several classes.
-
You mean creating a new model for my second view and pass it to my listviewclass constructor instead of making it a private var of listview class?
Thanks
-
I mean creating one model and use it for both dialogs.
-
@SGaist you mean remove it from class and create the model in main function, true? Its a good idea. Thanks