Implementing QTreeView in QComboBox using Qt- Part 1

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Introduction[edit | edit source]

Here is the code snippet for the treeview implemented in combobox.In this their is file model object defined that has been set in the combobox(ui->comboBox->setModel(fsmodel);).we can navigate to the different directories by just opening the combox and selecting item.

Source Code[edit | edit source]

   QFileSystemModel *fsmodel = new QFileSystemModel(this);
   fsmodel->setRootPath("/");
   setStyleSheet("* { background-color:rgb(90,20,60);color:rgb(255,255,255); padding: 7px}}");
   QTreeView *tv = new QTreeView(ui->comboBox);
   ui->comboBox->setStyleSheet("* { background-color:rgb(225,225,225);color:rgb(255,255,255); padding: 7px}}");
   ui->comboBox->setView(tv);
   ui->comboBox->setModel(fsmodel);

Screenshot[edit | edit source]

File:Treecombo.JPG


Problem[edit | edit source]

Given code snippet works fully,but their is a problem regarding the popup of the comboBox.As we click to expand the tree the comboBox popup closes,so we are not able to select the preferred item form the treeView at a time,number of clicks are to be made to select the preferred item.

Solution[edit | edit source]

For the solution of upper given problem visit:Implementing QTreeView in QComboBox using Qt- Part 2

pt:Archived:Implementando uma TreeView em um QComboBox - Parte 1