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. Alternate-background-color
Forum Updated to NodeBB v4.3 + New Features

Alternate-background-color

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.8k 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.
  • S Offline
    S Offline
    SoQT
    wrote on last edited by
    #1

    Hi,
    I have a QTableView.
    I want to color a row with a jump of 2 rows.
    So i'd like to have:

    WHITE ROW
    WHITE ROW
    RED ROW
    WHITE ROW
    WHITE ROW
    RED ROW
    ...... and so on

    How can i do this with a stylesheet?

    I have this piece of code but it doesn't work obviously.

    QTableView {
    alternate-background-color: red;
    background: white;
    }

    Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      AFAIK, you can't do it like that through style sheet, however you can use a QIdentityProxyModel .

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SoQT
        wrote on last edited by
        #3

        Thank you for the useful information.
        How can I use QIdentityProxyModel?
        I read many websites but still can't to solve this problem.

        I'm using a QStandardItemModel with the QTableView

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Subclass it, implement the data function to return the correct color for the Qt::BackgroundRole

          Then

          @
          YourModel *model = new YourModel;
          YourIdentityProxyModel * proxyModel= new YourIdentityProxyModel;
          proxyModel->setSourceModel(model);
          yourView->setModel(proxyModel);
          @

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SoQT
            wrote on last edited by
            #5

            Something like this?

            @
            class MyProxyModel : public QIdentityProxyModel
            {
            public:
            QVariant data(const QModelIndex &index, int role){
            if (role == Qt::BackgroundRole){
            QColor color = Qt::red;
            return QBrush(color);
            }
            }
            return QIdentityProxyModel::data(index, role);
            }
            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You need to add the math for your custom color schema

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • VinorcolaV Offline
                VinorcolaV Offline
                Vinorcola
                wrote on last edited by
                #7

                Maybe you don't need the proxy model here. You can treat the BackgroundColor role directly in your original model...

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SoQT
                  wrote on last edited by
                  #8

                  @SGaist How do I add the math?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @ Vinorcola the idea behind the proxy is that if you use the same model on another view you don't necessarily want to have that kind of colors so you don't modify the original data and use the proxy between the view where you actually want to have that new background.

                    @SoQT you want me to write for you the logic that returns white for the first row and red for the 2 next ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    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