Is it possible to move icon in QStandardItem?
Solved
General and Desktop
-
Hi,
I have QStandardItemModel and I would like to add to this model items - QStandardItems and set this model in QComboBox.
My code:
comboBox = new QComboBox(this); model = new QStandardItemModel; QStandardItem * newItem = new QStandardItem; newItem->setText("another text"); newItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); newItem->setData(Qt::Unchecked, Qt::CheckStateRole); QIcon icon(R"(pathTopng\another.png)"); newItem->setIcon(icon); newItem->setSelectable(true); model->setItem(0, 0, newItem); comboBox->setModel(model); comboBox->resize(200,20);
My item looks like this:
I would like to move icon like this:
How Can I do this?
-
Use a delegate:
class StyledItemDelegate: public QStyledItemDelegate{ public: using QStyledItemDelegate::QStyledItemDelegate; protected: void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{ QStyledItemDelegate::initStyleOption(option, index); option->decorationPosition = QStyleOptionViewItem::Right; } };
-
@eyllanesc Perfect!
@JonB @eyllanesc I have second question about similar problem.
No I would like to write our mousePressEvent() function in class which inherits QComboBox, but when I click in the popup view().
When I do that I only get clicks on dropdown menu and don't get clicks on popup (view()).
I know that I should do something with comboBox->view(), but I don't know what.
EDIT:
view()->viewport()->installEventFilter(this);