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>
{
Q_OBJECT
public:
explicit mainWindow
(QWidget *parent
= nullptr
);
~mainWindow();
signals:
public slots:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "treecombobox.h"
#include <QStandardItem>
#include <QStandardItemModel>
#include <QList>
#include <QString>
{
QList<QStandardItem *> list;
list << item;
parentItem-> appendRow(list);
parentItem = item;
list.clear();
QList<QStandardItem *> list;
list << item;
parentItem->appendRow(list);
list.clear();
list << item2;
parentItem->appendRow(list);
TreeComboBox *t = new TreeComboBox(this);
t->setModel (& model);
t->expandAll();
}
mainWindow::~mainWindow(){
}
#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(){
}
To copy to clipboard, switch view to plain text mode
treecombobox.h
#ifndef TREECOMBOBOX_H
#define TREECOMBOBOX_H
#include <QComboBox>
#include <QTreeView>
#include <QWidget>
{
public:
TreeComboBox
(QWidget *parrent
= nullptr
);
void showPopup () override;
void hidePopup () override;
void hideColumn (int n);
void expandAll ();
private:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
treecombobox.cpp
#include "treecombobox.h"
#include <QHeaderView>
{
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 ()
{
}
void TreeComboBox :: hidePopup ()
{
setRootModelIndex (m_view-> currentIndex (). parent ());
setCurrentIndex (m_view-> currentIndex (). row ());
}
#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 ();
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char * argv[]){
mainWindow *w = new mainWindow();
w->resize(100, 100);
w->show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks