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. Deselect an ItemDelegate in QTableView
Qt 6.11 is out! See what's new in the release blog

Deselect an ItemDelegate in QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.5k Views 1 Watching
  • 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.
  • Fuel 0F Offline
    Fuel 0F Offline
    Fuel 0
    wrote on last edited by
    #1

    Good Morning everyone,

    i have a QTableView with Stylesheets and a special ItemDelegate. What i now want is, when i select something else, i want to clear the Selection of the QTableView. I have done some research how i do this and i found something. Its not so easy as i wish, but its something i can work with. At the Moment im struggling with one Thing in this Solution.

    Here is an Image how it looks, when i select the ComboBox on the Top and a Row was selected.

    Unbenannt.png

    I read that i have to change the Paint Event of the ItemDelegate, but i dont know how i do that, but i tried following.

    First i changed my Stylesheet.

    setStyleSheet("QTableView { margin-bottom: 5px; border: 1px solid black; }"
                 "QTableView:!active { selection-color: black; selection-background-color: rgba(72, 169, 166, 1); }"
                 "QHeaderView::section { background-color: rgba(22, 15, 42, 1); color: white; }");
    

    then i added some lines to my paint Event in the ItemDelegate

    RatingDiaryTableWidget *rating = qvariant_cast<RatingDiaryTableWidget*>(index.data());
    
            if (option.state & QStyle::State_Selected)
                painter->fillRect(option.rect, option.palette.highlight());
    
            if (option.state & !QStyle::State_Active)
                painter->fillRect(option.rect, option.palette.NoRole);
    
            painter->setBackground(QBrush(Qt::transparent));
            rating->render(painter, painter->deviceTransform().map(option.rect.topLeft()), QRegion(QRect(2, 9, 135, 25)));
    

    As you can see i addded a !QStyle::State_Active Case, but that didnt fixed it.
    Someone can help me?

    JonBJ 1 Reply Last reply
    0
    • Fuel 0F Fuel 0

      Good Morning everyone,

      i have a QTableView with Stylesheets and a special ItemDelegate. What i now want is, when i select something else, i want to clear the Selection of the QTableView. I have done some research how i do this and i found something. Its not so easy as i wish, but its something i can work with. At the Moment im struggling with one Thing in this Solution.

      Here is an Image how it looks, when i select the ComboBox on the Top and a Row was selected.

      Unbenannt.png

      I read that i have to change the Paint Event of the ItemDelegate, but i dont know how i do that, but i tried following.

      First i changed my Stylesheet.

      setStyleSheet("QTableView { margin-bottom: 5px; border: 1px solid black; }"
                   "QTableView:!active { selection-color: black; selection-background-color: rgba(72, 169, 166, 1); }"
                   "QHeaderView::section { background-color: rgba(22, 15, 42, 1); color: white; }");
      

      then i added some lines to my paint Event in the ItemDelegate

      RatingDiaryTableWidget *rating = qvariant_cast<RatingDiaryTableWidget*>(index.data());
      
              if (option.state & QStyle::State_Selected)
                  painter->fillRect(option.rect, option.palette.highlight());
      
              if (option.state & !QStyle::State_Active)
                  painter->fillRect(option.rect, option.palette.NoRole);
      
              painter->setBackground(QBrush(Qt::transparent));
              rating->render(painter, painter->deviceTransform().map(option.rect.topLeft()), QRegion(QRect(2, 9, 135, 25)));
      

      As you can see i addded a !QStyle::State_Active Case, but that didnt fixed it.
      Someone can help me?

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

      @Fuel-0 said in Deselect an ItemDelegate in QTableView:

      if (option.state & !QStyle::State_Active)

      I haven't looked into this for you, but do you understand that with bitwise & operator using logical not !something is likely to be wrong? Do you intend bitwise not: & ~QStyle::State_Active here by any chance?

      Fuel 0F 1 Reply Last reply
      0
      • JonBJ JonB

        @Fuel-0 said in Deselect an ItemDelegate in QTableView:

        if (option.state & !QStyle::State_Active)

        I haven't looked into this for you, but do you understand that with bitwise & operator using logical not !something is likely to be wrong? Do you intend bitwise not: & ~QStyle::State_Active here by any chance?

        Fuel 0F Offline
        Fuel 0F Offline
        Fuel 0
        wrote on last edited by
        #3

        @JonB said in Deselect an ItemDelegate in QTableView:

        @Fuel-0 said in Deselect an ItemDelegate in QTableView:

        if (option.state & !QStyle::State_Active)

        I haven't looked into this for you, but do you understand that with bitwise & operator using logical not !something is likely to be wrong? Do you intend bitwise not: & ~QStyle::State_Active here by any chance?

        Yes you are right. Im not very familiar with Bitwise Operators. I changed my Code to following.

        if (option.state & QStyle::State_Selected)
                    painter->fillRect(option.rect, option.palette.highlight());
        
        if (option.state & ~QStyle::State_Active)
                    painter->fillRect(option.rect, option.palette.background());
        

        But it seems that the second State overwrites the first State. So it always has now the background Color. How can i append both States?

        kshegunovK 1 Reply Last reply
        0
        • Fuel 0F Fuel 0

          @JonB said in Deselect an ItemDelegate in QTableView:

          @Fuel-0 said in Deselect an ItemDelegate in QTableView:

          if (option.state & !QStyle::State_Active)

          I haven't looked into this for you, but do you understand that with bitwise & operator using logical not !something is likely to be wrong? Do you intend bitwise not: & ~QStyle::State_Active here by any chance?

          Yes you are right. Im not very familiar with Bitwise Operators. I changed my Code to following.

          if (option.state & QStyle::State_Selected)
                      painter->fillRect(option.rect, option.palette.highlight());
          
          if (option.state & ~QStyle::State_Active)
                      painter->fillRect(option.rect, option.palette.background());
          

          But it seems that the second State overwrites the first State. So it always has now the background Color. How can i append both States?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @Fuel-0 said in Deselect an ItemDelegate in QTableView:

          Yes you are right. Im not very familiar with Bitwise Operators.

          I hope you don't get this the wrong way, but you should get familiar. It's basic data representation (in computers) and fundamental C/C++, so while not too often you're going to stumble upon it.

          I changed my Code to following.

          if (option.state & QStyle::State_Selected)
                      painter->fillRect(option.rect, option.palette.highlight());
          
          if (option.state & ~QStyle::State_Active)
                      painter->fillRect(option.rect, option.palette.background());
          

          But it seems that the second State overwrites the first State. So it always has now the background Color. How can i append both States?

          With bitwise or. If I understand correctly what you want to do it'd be something like this:

          if ((option.state & QStyle::State_Active) == QStyle::State_Active)
              painter->fillRect(option.rect, option.palette.background());
          else if ((option.state & QStyle::State_Selected) == State_Selected)
              painter->fillRect(option.rect, option.palette.highlight());
          else
              // No active, no selected
          

          As a note often when working with bit masks you want to check the exact bits, because a given mask may span more than a single bit and they can overlap. E.g.: (option.state & QStyle::State_Selected) == QStyle::State_Selected
          would mean "I want all the raised bits of QStyle::State_Selected be present in option.state."

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          3
          • Fuel 0F Offline
            Fuel 0F Offline
            Fuel 0
            wrote on last edited by
            #5

            Thanks Guys. I found out that there is no State that will work for me. State_Active selects every ItemDelegate in my TableView.

            At the Moment i found something that works with QPalette. Its not exactly what i want, but i will try to fix this for my needs later.

            Thanks for the Help.

            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