How to use QTableView & Model
-
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 -
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 -
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..
-
- Try to replace your data() , rowCount, columnCount with mine.
- Did you override QTableView? If yes try to see what happens if you use QTableView class instead of your view with default options set.
-
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
@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(); }