PDA

View Full Version : QComboBox with custom tree view is not showing its items



INeedADollar
18th September 2019, 18:00
Hello! I try to make a QComboBox with custom tree view, but combo box is not showing its items when clicking on it. Can you help me please?

My code:

mainWindow.h



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>

class mainWindow : public QWidget
{
Q_OBJECT
public:
explicit mainWindow(QWidget *parent = nullptr);
~mainWindow();

signals:

public slots:
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"
#include "treecombobox.h"
#include <QStandardItem>
#include <QStandardItemModel>
#include <QList>
#include <QString>

mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
{
QStandardItemModel model;
QStandardItem * parentItem = model.invisibleRootItem ();
QStandardItem * item = new QStandardItem (QString ("item 0");
QList<QStandardItem *> list;
list << item;
parentItem-> appendRow(list);

parentItem = item;
QStandardItem * item = new QStandardItem (QString ("item 1") ;
list.clear();
QList<QStandardItem *> list;
list << item;
parentItem->appendRow(list);
list.clear();
QStandardItem * item2 = new QStandardItem (QString ("item 2");
list << item2;
parentItem->appendRow(list);
TreeComboBox *t = new TreeComboBox(this);
t->setModel (& model);
t->expandAll();
}

mainWindow::~mainWindow(){

}


treecombobox.h


#ifndef TREECOMBOBOX_H
#define TREECOMBOBOX_H

#include <QComboBox>
#include <QTreeView>
#include <QWidget>

class TreeComboBox final: public QComboBox
{
public:
TreeComboBox (QWidget *parrent = nullptr);
void showPopup () override;
void hidePopup () override;
void hideColumn (int n);
void expandAll ();
void selectIndex (const QModelIndex & index);
private:
QTreeView * m_view = nullptr;
};

#endif // TREECOMBOBOX_H


treecombobox.cpp


#include "treecombobox.h"
#include <QHeaderView>

TreeComboBox :: TreeComboBox (QWidget *parent) : QComboBox(parent)
{
m_view = new QTreeView;
m_view-> setFrameShape (QFrame :: NoFrame);
m_view-> setEditTriggers (QTreeView :: NoEditTriggers);
m_view-> setAlternatingRowColors (true);
m_view-> setSelectionBehavior (QTreeView :: SelectRows);
m_view-> setRootIsDecorated (false);
m_view-> setWordWrap (true);
m_view-> setAllColumnsShowFocus (true);
m_view-> setItemsExpandable (false);
setView (m_view);
m_view-> header () -> setVisible (false);
}

void TreeComboBox :: hideColumn (int n)
{
m_view-> hideColumn (n);
}

void TreeComboBox :: expandAll ()
{
m_view-> expandAll ();
}

void TreeComboBox :: selectIndex (const QModelIndex & index)
{
setRootModelIndex (index.parent ());
setCurrentIndex (index.row ());
m_view-> setCurrentIndex (index);
}

void TreeComboBox :: showPopup ()
{
setRootModelIndex (QModelIndex ());
QComboBox :: showPopup ();
}

void TreeComboBox :: hidePopup ()
{
setRootModelIndex (m_view-> currentIndex (). parent ());
setCurrentIndex (m_view-> currentIndex (). row ());
QComboBox :: hidePopup ();
}


main.cpp


#include <QApplication>
#include "mainwindow.h"

int main(int argc, char * argv[]){
QApplication app(argc, argv);
mainWindow *w = new mainWindow();
w->resize(100, 100);
w->show();
return app.exec();
}

ChristianEhrlicher
19th September 2019, 10:37
mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
{
QStandardItemModel model;
...
}


How long do you think lives your model?

INeedADollar
19th September 2019, 17:40
And what I need to do?

Added after 19 minutes:


And what I need to do?

I found what is wrong. Thank you very much!