Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set 2 different settings in QStandardItem?



  • Hi,

    I have QComboBox, QStandardItemModel and QStandardItems. I setItems in this model:

    model->setItem(0,0,item);
    

    and next I set this model in comboBox:

    comboBox->setModel(model);
    

    In this items, which I add to model ( and next to comboBox ) I set:

    item->setFlags(Qt::ItemIsEnabled);
    

    I need this flag.

    I would like to see the blue color when my mouse cursor is on another item ( like hover ).

    When I don't set flag "ItemIsEnabled" everything is ok - I see blue color. When I set this flag I don't see it.

    I try add other flag:

    item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    

    Now I see blue color ( hover ), but my popup is hidden ( I don't want it - I use flag ItemIsEnabled to block this hide ).

    How can I get this two things:

    • don't hide popup when I click on it
    • see blue hover
      In the same time.

    I would like to do it with delegate or with flags. I would like to avoid reimplement hover event.



  • @TomNow99
    I don't totally understand which you do/do not want. Read through https://doc.qt.io/qt-5/qt.html#ItemFlag-enum. Maybe you also want Qt::ItemIsEditable, I'm not sure:

    item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
    


  • @JonB I solved this problem by add paint method in delegate.

    My code:

    void FocusControlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
            QStyleOptionViewItem optionV4 = option;
            initStyleOption(&optionV4, index);
    
            QStyle *style = optionV4.widget ? optionV4.widget->style() : QApplication::style();
    
            if (option.state & QStyle::State_Selected) { //////////// new
                painter->setPen(Qt::white);
                painter->setBrush(option.palette.highlightedText());
            }
    
            style->drawControl(QStyle::CE_ItemViewItem, &option, painter, option.widget); // slightly changed
    
            QStyledItemDelegate::paint(painter,option,index);
    }
    

    Here I just replace code @mrjj from here:
    https://forum.qt.io/topic/104677/when-using-a-custom-delegate-to-paint-the-items-of-a-qtablewidget-how-can-the-delegate-be-configured-to-use-the-correct-background-colour-for-selected-items/2

    And I have other question ( maybe to @mrjj or somebody else ):
    How can I change hover color in this function paint() in this delegate from lightblue to red?

    I try change:

    painter->setBrush(QBrush(Qt::red));
    

    but with no result.

    EDIT: @JonB In my last question I ask about:
    QComboBox works like this: I click on QComboBox lineEdit ( that part where is arrow ), popup is shown, I click in one item on this popup list, popup is hidden, text in lineEdit is changed to item's text, which I clicked.

    I would like to have situation with comboBox, when the popup is shown and I click on one of the item on the list, I would like don't hide popup ( like in combobox with checkboxes - I don't have to open many times combobox's popup to check many items ). And I can do that with flag:

    Qt::ItemIsEnabled 
    

    But when I set this flag I don't see the blue hover on item in this list, where I have mouse cursor.

    jon.png

    The black arrow in the picture is this blue hover select. When I set flag Qt::ItemIsEnabled I don't see that hover blue color. So I tried return that hover color, but still have Qt::ItemIsEnabled flag.



  • @TomNow99 said in How to set 2 different settings in QStandardItem?:

    How can I change hover color in this function paint() in this delegate from lightblue to red?

    Is there a reason to want this way rather than :hover CSS selector?



  • @JonB I would like to do that with delegate. I also add EDIT in my last post for you. Now I think you understand my last problem :)


Log in to reply