Icon in QComboBox is not visible
-
I have added an entry to the editable QComboBox, but it's icon is not visible in the linedit field.
auto icon_val = model->data(firstIndex, QFileSystemModel::FileIconRole); comboBox->addItem(icon_val.value<QIcon>(), name);
-
Hi,
How are you setting up that combobox ?
Where does these images come from ? -
@SGaist Hello,
I have a QTableView, which takes its data from proxied QFileSystemModel.
Once a cell is selected, I add an entry with the icon from the selected row and current text from this combobox.void addItem(const QModelIndex& index) { auto firstIndex = model->index(index.row(), 0); auto index_val = model->data(firstIndex); auto icon_val = model->data(firstIndex, QFileSystemModel::FileIconRole); auto name = comboBox->currentText(); comboBox->addItem(icon_val.value<QIcon>(), name); } connect(tableView, &QTableView::doubleClicked, [](const QModelIndex& const index) { addItem(index); });
-
I think you'll have to implement that yourself.
See the code proposed in this stack overflow answer to get started.
-
I think you'll have to implement that yourself.
See the code proposed in this stack overflow answer to get started.
@SGaist Thanks. This solution seems to work pretty nicely. I have set QComboBox to my custom QLineEdit and styled it to appear transparent.
The only thing that is bothering me now is that QComboBox automatically offsets it's lineedit as soon as an icon entry is added.
What can I do about that?
-
Hi
Do you set the LineEditIcon icon first as true empty icon ? ( so it dont have 2)ui->comboBox->setLineEdit( new LineEditIcon(QIcon(), this) );
It looks like you have a transparent image in front so maybe you gave it a default size empty icon and not just invalid one?
-
Hi
Do you set the LineEditIcon icon first as true empty icon ? ( so it dont have 2)ui->comboBox->setLineEdit( new LineEditIcon(QIcon(), this) );
It looks like you have a transparent image in front so maybe you gave it a default size empty icon and not just invalid one?
@mrjj Hello,
Here's my code. I can't figure out what I'm missing. I would be grateful if you could take a look.
myWidget::myWidget(QWidget* parent) : lineEdit{ new LineEditIcon{QIcon(), this} } { ui->setupUi(this); ui->comboBox_2->setLineEdit(lineEdit); ui->comboBox_2->setInsertPolicy(QComboBox::NoInsert); ... connect(tableView, &QTableView::doubleClicked, this, &myWidget::addOrUpdateItem); connect(ui->comboBox_2, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &myWidget::changeIcon); } void myWidget::addOrUpdateItem(const QModelIndex& index) { auto firstIndex = model->index(index.row(), 0); auto index_val = model->data(firstIndex); auto icon_val = model->data(firstIndex, QFileSystemModel::FileIconRole); auto name = comboBox->currentText(); ... if (something) // add new item { ui->comboBox_2->addItem(icon_val.value<QIcon>(), name); lineEdit->setIcon(icon_val.value<QIcon>()); } else // change current item { auto current_index = ui->comboBox_2->currentIndex(); ui->comboBox_2->setItemIcon(current_index, icon_val.value<QIcon>()); lineEdit->setIcon(icon_val.value<QIcon>()); } } void myWidget::changeIcon(int index) { auto icon = ui->comboBox_2->itemIcon(index); lineEdit->setIcon(icon); } class LineEditIcon : public QLineEdit { Q_OBJECT public: LineEditIcon(const QIcon icon, QWidget* parent = nullptr) { setIcon(icon); setPlaceholderText("HDRI"); setStyleSheet("font-size: 11pt;" "background-color: transparent;" "border-color: transparent;"); setFrame(false); }; ~LineEditIcon(){}; void setIcon(QIcon icon) { m_icon = icon; if (m_icon.isNull()) setTextMargins(1, 1, 1, 1); else setTextMargins(36, 1, 1, 1); }; protected: virtual void paintEvent(QPaintEvent* event) { QLineEdit::paintEvent(event); if (!m_icon.isNull()) { QPainter painter(this); auto pxm = m_icon.pixmap(height() * 2 - 3, height() - 6); painter.drawPixmap(0, 3, pxm); } }; private: QIcon m_icon; };
-
What OS are you on ?
-
@SGaist Windows 10
-
I managed to solve the problem with the following:
ui->comboBox->setIconSize(QSize{0, 0}); ui->comboBox_2->setLineEdit(lineEditIcon); ui->comboBox_2->view()->setIconSize(QSize{ 48,24 });
then I can simply call:
combobox->addItem(icon, name); lineEditIcon->setIcon(icon);
-
I have added an entry to the editable QComboBox, but it's icon is not visible in the linedit field.
auto icon_val = model->data(firstIndex, QFileSystemModel::FileIconRole); comboBox->addItem(icon_val.value<QIcon>(), name);
@krzysieklfc said in Icon in QComboBox is not visible:
I have added an entry to the editable QComboBox, but it's icon is not visible in the linedit field. TelltheBell
auto icon_val = model->data(firstIndex, QFileSystemModel::FileIconRole); comboBox->addItem(icon_val.value<QIcon>(), name);
Same issue ... except I could not get it right in any dimensions.