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 11.9k 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 20 Oct 2011, 06:54 last edited by
    #1

    Hello
    I need some help to get the checked value in a tableview.

    My code looks like this
    @ QModelIndexList selectedList= ui->testTable->selectionModel()->selectedRows(5);//colum som läggs till listan
    QModelIndex index = ui->testTable->currentIndex();

    QBrush cyan;
    cyan.setColor(Qt::cyan);
    cyan.setStyle(Qt::SolidPattern);
    
    foreach (index,selectedList )
    {
        if (dataModel->data(index))
          dataModel->setData(index, cyan, Qt::BackgroundRole);
    }
    

    @

    But i need to loop thrue the hole datamodel and check every box.

    A hint someone

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on 20 Oct 2011, 07:36 last edited by
      #2

      Hi,

      from your code, it's not clear what you want to achieve. It does not fir to the question.
      Inside the code, you iterate all selected cells in column 5 and set the background color. Where comes the checked state into the game?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 20 Oct 2011, 07:54 last edited by
        #3

        If you want to query the checked state of a model, simply use the correct data role Qt::CheckStateRole, which returns a QVariant containing a Qt::CheckState.

        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 [[doc:QItemDelegate]].

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toho71
          wrote on 20 Oct 2011, 08:06 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 20 Oct 2011, 08:27 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 20 Oct 2011, 08:40 last edited by
              #6

              Thanks I'll try

              1 Reply Last reply
              0
              • T Offline
                T Offline
                toho71
                wrote on 20 Oct 2011, 08:57 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 20 Oct 2011, 09:04 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 20 Oct 2011, 09:18 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 20 Oct 2011, 09:27 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 20 Oct 2011, 09:37 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 20 Oct 2011, 10:16 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 20 Oct 2011, 10:44 last edited by
                            #13

                            Thanks for the code and its OK

                            No problems after a while.

                            1 Reply Last reply
                            0

                            1/13

                            20 Oct 2011, 06:54

                            • Login

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