Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set 2 different settings in QStandardItem?

How to set 2 different settings in QStandardItem?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 895 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • T TomNow99

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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);
      
      1 Reply Last reply
      1
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by TomNow99
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • T TomNow99

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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?

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TomNow99
            wrote on last edited by
            #5

            @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 :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved