Starting from a "normal" QCombobox
I'd like to get a QCombobox
that only shows the icon when it's expanded, but not when it's collapsed.
I've found several answers to similar questions, but all of them show code for much more complex situations and I have not managed to distill the core of it.
There are two approaches I've seen: attaching a QListView
or using a QItemDelegate
(or both).
But I could not find any sample code that is straight to the point.
This is my starting point:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->iconsComboBox->addItem(QIcon(":/icons/1.png"), "red");
ui->iconsComboBox->addItem(QIcon(":/icons/2.png"), "green");
ui->iconsComboBox->addItem(QIcon(":/icons/3.png"), "pink");
auto quitAction = new QAction();
quitAction->setShortcuts(QKeySequence::Quit);
addAction(quitAction);
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
}
The full working code at that stage is here: https://github.com/aoloe/cpp-qt-playground-qcombobox/tree/simple-qcombobox
How can I hide the icon when the QCombobox is closed?
I have accepted the two pull requests by eyllanesc:
You can get the code and run it to see it in action.