Important: Please read the Qt Code of Conduct -

Is it possible to move icon in QStandardItem?

  • 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)");
        model->setItem(0, 0, newItem);

    My item looks like this:


    I would like to move icon like this:


    How Can I do this?

  • @TomNow99
    Subject to being corrected, I don't think you can do that (certainly there is nothing obvious). You may be able to use a QStyledItemDelegate to achieve this, assuming it can be done.

  • @JonB I try with a QItemDelegate. Have I to draw it? Or is there a better way?

  • Use a delegate:

    class StyledItemDelegate: public QStyledItemDelegate{
       using QStyledItemDelegate::QStyledItemDelegate;
        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.



Log in to reply