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. How to use QTableView & Model
Forum Updated to NodeBB v4.3 + New Features

How to use QTableView & Model

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtableview
26 Posts 3 Posters 12.1k 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.
  • C Offline
    C Offline
    cherple
    wrote on last edited by
    #16

    This is my data:

    if (role == Qt::DisplayRole)
    {
    Return data[row][col];
    }
    Else if (role = Qt::DecorationRole)
    {
    //if ( selectionVar == 0 )
    return QVariant(QColor(Qt::red));
    //else if( .... )
    }

    For now I just wanted to test the functionality so I just set it as red irregardless of my selection mode. I wanted to see if only my selected cell will be red.. But all the cells become a checkbox with red back ground.. I tried the checkstaterole before and it's the same.. The cells become a check box with the colored background.. I will try to take a screenshot later

    A 1 Reply Last reply
    0
    • C cherple

      This is my data:

      if (role == Qt::DisplayRole)
      {
      Return data[row][col];
      }
      Else if (role = Qt::DecorationRole)
      {
      //if ( selectionVar == 0 )
      return QVariant(QColor(Qt::red));
      //else if( .... )
      }

      For now I just wanted to test the functionality so I just set it as red irregardless of my selection mode. I wanted to see if only my selected cell will be red.. But all the cells become a checkbox with red back ground.. I tried the checkstaterole before and it's the same.. The cells become a check box with the colored background.. I will try to take a screenshot later

      A Offline
      A Offline
      alex_malyu
      wrote on last edited by alex_malyu
      #17

      @cherple

      I am a bit surprised handling decoration role has such effect (checkbox),
      bu t I believe overriding
      virtual Qt::ItemFlags flags ( const QModelIndex & index ) const
      should solve it. Something like :

      Qt::ItemFlags myClass::flags ( const QModelIndex & index ) const
      {
      return Qt::ItemIsSelectable|Qt::ItemIsEnabled; // make sure not to return Qt::ItemIsUserCheckable
      }

      As for color, model can define cell color, but it does not have any idea what is selected in specific view.

      So, if you want to change color of only selected cell you need to do it on QTableView level.
      Or at least you can let model know that this cell is selected and need custom color every time
      selection is changed. For example add array or map which will keep color per cell and set values there, use this container in data to define cell color.
      In this case it had to be used with a single view, but I do not think it is a problem for you.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cherple
        wrote on last edited by
        #18

        I'm unable to link the image at the moment. I've reimplemented the flags function as u suggested but it remains the same. In my cell, there will be a check box on the left and the whole cell will be painted red.

        Just to confirm, all I had to do was to include the flags function in my model's header file and the flags function in the cpp right? I'm not sure why this is happening :/

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cherple
          wrote on last edited by
          #19

          This is what i see, and what i typed. I have also included the Flags function as u mentioned(not shown).

          Code
          Result

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by alex_malyu
            #20

            Sorry that was my fault - DecorationRole displays icon/picmap.
            So these are not checkboxes.

            Replace Qt::DecorationRole with role == Qt::BackgroundColorRole.
            Something like:

            QVariant MyModel::data ( const QModelIndex & index, int role )  const
            {
            	if ( index.isValid() )
            	{
            		if( role == Qt::DisplayRole )
            		{
            			QString str = QString("%1:%2").arg (index.row()).arg (index.column());
            			return QVariant( str );
            		}
            		else if( role == Qt::BackgroundColorRole )
            		{
            			if( index.row() == index.column() )
            				return QVariant(QColor(Qt::red));
            			else if( index.row() < index.column() )
            				return QVariant(QColor(Qt::yellow));
            			else 
            				return QVariant(QColor(Qt::green));
            		}
            
            	}
            	return QVariant();
            }
            

            And you do not need flags anymore.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cherple
              wrote on last edited by
              #21

              No worries, it isn't your fault, because I tried backgroundcolorrole and it returned the same results before.

              After entering ur code, this is what it looks like:
              http://img.photobucket.com/albums/v483/dragonlancer/D6781CF8-2598-46CA-9290-167CB12FF98C.jpg
              The code:
              http://img.photobucket.com/albums/v483/dragonlancer/64707ED7-6081-466D-AE35-48263E2A959D.jpg
              My table view settings:
              http://img.photobucket.com/albums/v483/dragonlancer/74A8A89E-97ED-4A1F-8E68-92AF0101CFA9.jpg

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alex_malyu
                wrote on last edited by alex_malyu
                #22

                Could you post all code for whole model class and may be table view subclass?

                As for now I can only see one potential problem -
                you do not check that index passed to data is valid,
                which you should do,

                Code I posted works for me when my model subclass is set to QTableView.
                Check if it works for you,

                cpp:

                #include "mymodel.h"
                #include<QColor>
                #include<QVariant>
                
                
                MyModel::MyModel(QObject *parent)
                	: QAbstractTableModel(parent)
                {
                
                }
                
                MyModel::~MyModel()
                {
                
                }
                
                int MyModel::rowCount ( const QModelIndex & parent ) const
                {
                	return 5;
                }
                
                int MyModel::columnCount ( const QModelIndex & parent ) const
                {
                	return 7;
                }
                
                QVariant MyModel::data ( const QModelIndex & index, int role )  const
                {
                	if ( index.isValid() )
                	{
                		if( role == Qt::DisplayRole )
                		{
                			QString str = QString("%1:%2").arg (index.row()).arg (index.column());
                			return QVariant( str );
                		}
                		else if( role == Qt::BackgroundColorRole )
                		{
                			if( index.row() == index.column() )
                				return QVariant(QColor(Qt::red));
                			else if( index.row() < index.column() )
                				return QVariant(QColor(Qt::yellow));
                			else 
                				return QVariant(QColor(Qt::green));
                		}
                
                	}
                	return QVariant();
                }
                

                .h:

                #pragma once
                
                #ifndef MYMODEL_H
                #define MYMODEL_H
                
                #include <QAbstractTableModel>
                
                class MyModel : public QAbstractTableModel
                {
                	Q_OBJECT
                
                public:;
                	MyModel(QObject *parent=NULL);
                	~MyModel();
                
                	int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
                	int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
                	QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
                
                private:
                
                };
                
                #endif // MYMODEL_H
                
                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cherple
                  wrote on last edited by
                  #23

                  The only difference between our code is the number of row and column returned, as well as this:

                  MyModel::MyModel(QObject *parent)
                  : QAbstractTableModel(parent)
                  {

                  My code uses QWidget instead of QObject. My constructor only sets data into gridNum[r][c] which is being returned in data(). Other than that I don't see any difference.. Damn this is frustrating..

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alex_malyu
                    wrote on last edited by alex_malyu
                    #24
                    1. Try to replace your data() , rowCount, columnCount with mine.
                    2. Did you override QTableView? If yes try to see what happens if you use QTableView class instead of your view with default options set.
                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      cherple
                      wrote on last edited by
                      #25

                      I copied your code exactly and it's the same. Could the difference I. at version be the problem? I'm using Qt 4.8.1

                      A 1 Reply Last reply
                      0
                      • C cherple

                        I copied your code exactly and it's the same. Could the difference I. at version be the problem? I'm using Qt 4.8.1

                        A Offline
                        A Offline
                        alex_malyu
                        wrote on last edited by alex_malyu
                        #26

                        @cherple It works with Qt 4.8.6. I do not have earlier version to check .
                        It might be a Qt problem. But I doubt it.
                        Have you tried my suggestion to use my model as it is together with QTableView?
                        All you need on top of code above is main like:

                        #include "mymodel.h"
                        #include <QtGui/QApplication>
                        #include <QTableView>
                        
                        int main(int argc, char *argv[])
                        {
                        	QApplication a(argc, argv);
                        	QTableView w;
                            MyModel* model = new MyModel();
                         	w.setModel( model );
                        
                        	w.show();
                        	return a.exec();
                        }
                        
                        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