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

Deselect an ItemDelegate in QTableView



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



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



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


  • Qt Champions 2017

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



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


Log in to reply