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. [Solved]Get value of checkbox in a qtableview
Forum Updated to NodeBB v4.3 + New Features

[Solved]Get value of checkbox in a qtableview

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 12.0k 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.
  • T Offline
    T Offline
    toho71
    wrote on last edited by
    #4

    bq. However, it seems that you want to simply try to modify the rendering of checked items to give them a background color. That can be done more efficiently using a QItemDelegate.

    I want to check the state of the checkbox and it is in the 5 th column.
    if the state is true/checked I want to change color of that row.

    And i also want to se the total rows in the model.
    Is it possible.

    I'd like some simple exampels if there are some around.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #5

      For the background, use a [[Doc:QStyledItemDelegate]] subclass, as Andre suggested. Override the paint() method, check the row/column and data and put the changed background into the options and call the base class implementation in the end:

      @
      void MyItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index )
      {
      QStyleOptionViewItemV4 newOption(option);
      if(index.row() == 5) {
      if(index.data(Qt::CheckStateRole) == Qt::Checked) {
      newOption.backgroundBrush = QBrush(Qt::yellow);
      }
      }
      QStyledItemDelegate::paint(painter, newOption, index);
      }
      @

      (Brain to terminal, code is not tested)

      You get the total number of rows using model->rowCount().

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        toho71
        wrote on last edited by
        #6

        Thanks I'll try

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toho71
          wrote on last edited by
          #7

          Yes i worked but i want to loop thru the datamodel and check all checkboxes.
          I thought i did that with my foreach but sometimes i got the right rowcount and the loop dosent work and sometimes i go thru the loop but only on one row.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #8

            The loop is executed by the view, it calls the delegate with the needed indexes.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • T Offline
              T Offline
              toho71
              wrote on last edited by
              #9

              I'll explain this in a new way.
              After i put all my values in the model()(QStandardItemModel)
              I put the model in the tableview.

              When this is done I try to loop thru the datamodel and tries to check if the

              @item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);
              @
              is checked.

              If so I want to change color of that row.

              thats why I want to loop thru this.

              I'm sure that this is a simple task but I'm stuck now

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #10

                OK, so you want to color a complete row based on a set of conditions. Sounds like a job for a proxy model to me.

                Something like:

                @
                //hightlightproxy.h
                //in Qt 4.8, you should use QIdentityProxyModel (I think it is called, please check).

                #if QT_VERSION >= 0x040800
                #include <QIndentityProxy>
                class HighLightProxy : public QIdentityProxyModel
                {
                typedef baseClass QIdentityProxyModel;
                #else
                #include <QSortFilterProxyModel>
                class HighLightProxy : public QSortFilterProxyModel
                {
                typedef baseClass QSortFilterProxyModel;
                #endif
                Q_OBJECT

                public:
                HighLightProxy(QObject* parent = 0);

                void setRow(int row);
                void setCheckColumn(column);
                void setHighlightBrush(const QBrush& brush);
                
                virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole ) const;
                

                private:
                int m_row;
                int m_checkColumn;
                QBrush m_brush;
                }
                @

                @
                //hightlightproxy.cpp
                HighLightProxy::HighLightProxy(QObject* parent) :
                baseClass(parent),
                m_row(-1),
                m_checkColumn(-1)
                {}

                QVariant HighLightProxy::data(const QModelIndex & index, int role ) const
                {
                if (!index.isValid())
                return QVariant();

                //lets assume you are not talking about a tree-type model here, and ignore parents
                if (index.row() == m_row && role == Qt::BackGroundRole) {
                //check the checkstate
                if (m_checkColumn > -1) {
                QModelIndex sIndex = sourceModel()->index(m_row, m_checkColumn);
                if (sIndex.data(Qt::CheckStateRole).toInt() == Qt::Checked) {
                return m_highlightBrush;
                }
                }
                }
                return baseClass::data(index, role);
                }

                //implementation of setters declared in header is trivial and thus not shown.
                @

                Note: written in forum editor, not complete, nor tested in a compiler.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  toho71
                  wrote on last edited by
                  #11

                  I'll try this but I'm stuck with how to loop thru the tableview or datamodel to.
                  @foreach(datamodel,???)
                  {
                  code.....
                  ]
                  @
                  A simpel loop but Not in my head for the moment.

                  pls help

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #12

                    With the approach above, you don't need to loop through the view or the model.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      toho71
                      wrote on last edited by
                      #13

                      Thanks for the code and its OK

                      No problems after a while.

                      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