c++ - QT tree that allows multiselection -
i'm making simple file explorer , ran problems qt. want show user tree view of files on computer, want able select multiple files/directories , them later on (by selecting checkboxes
or multiple select using ctrl+left click or shift+left click). i've placed qtreeview
element , set model (qfilesystemmodel
). gives me tree view, can't modify headers (column names) or add own column checkbox
in every row (for example). qt new me, i've searched few hours tips/solutions, nothing working qfilesystemmodel
. there can working?
the code short , simple:
qstring lpath = "c:/"; qstring rpath = "c:/"; lefttree_model = new qfilesystemmodel(this); righttree_model = new qfilesystemmodel(this); lefttree_model->setrootpath(lpath); righttree_model->setrootpath(rpath); //i have 2 tree views work same ui->lefttree->setmodel(lefttree_model); //ui->lefttree first tree view ui->righttree->setmodel(righttree_model); //the second
use of following:
checkstaterole add checkboxes model. this, inherit custom item model (which you're going use) qfilesystemmodel
, , reimplement data()
method, return bool
values checkstaterole
. need qabstractitemmodel::setdata
method handle changes. can check docs qabstractitemmodel see how change header texts (headerdata()
)
change selection mode of view allow multiple selections
edit: here's sample code inherit model
class myfancymodel : public qfilesystemmodel { public: myfancymodel(qobject* pparent = null) : qfilesystemmodel(pparent) { } qvariant data(const qmodelindex & index, int role = qt::displayrole ) const { if (role == qt::checkstaterole) { // stub value true return true; // here return real values // depending on item checked } return qfilesystemmodel::data(index, role); } };
Comments
Post a Comment