Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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)");
        newItem->setIcon(icon);
    
        newItem->setSelectable(true);
        model->setItem(0, 0, newItem);
    
        comboBox->setModel(model);
        comboBox->resize(200,20);
    

    My item looks like this:

    comboBox.png

    I would like to move icon like this:

    comboBox2.png

    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{
    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);
    

Log in to reply