0

I have a tableView example:

enter image description here

Like when the data column status is S. The background color of the table would be green and when N would be red.

Example of what I want:

enter image description here

My code:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
       //Con Db...

        model = new QSqlTableModel(this);
        model->setTable("app");
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model->select();
        model->setHeaderData(0, Qt::Horizontal, tr("number"));
        model->setHeaderData(1, Qt::Horizontal, tr("status"));
        ui->tableView->setModel(model);
}

Could someone help me?

user628298
  • 287
  • 2
  • 14

1 Answers1

2

Reimplement, i.e. subclass QSqlTableModel and provide an implementation of the data function as per below. Note that the MySubClassedSqlTableModel implementation needs to be placed in a header file that is MOC'ed (normally automatically done).

  #include <QSqlTableModel>
    class MySubClassedSqlTableModel : public QSqlTableModel
    {
        Q_OBJECT
        public:
           MySubClassedSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
           : QSqlTableModel(parent,db) {;}
           QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
           {
              if(role==Qt::BackgroundColorRole)
              {
                 const QVariant value(data(index,Qt::DisplayRole));
                 return QVariant(QColor(value.toString()=="S"?Qt::green:Qt::red));
              }
              return QSqlTableModel::data(index,role);
           }
    };

    model = new MySubClassedSqlTableModel(this); //<==== use your model
            model->setTable("app");
            model->setEditStrategy(QSqlTableModel::OnManualSubmit);
            model->select();
            model->setHeaderData(0, Qt::Horizontal, tr("number"));
            model->setHeaderData(1, Qt::Horizontal, tr("status"));
            ui->tableView->setModel(model);

Btw subclassing is something you need to do in most cases when you want to customize the behavior.

user2672165
  • 2,986
  • 19
  • 27
  • @user628298: Haven't compiled it. Come back if it doesn't compile. – user2672165 Mar 10 '14 at 14:56
  • Erro: qvariant.h:480: error: 'QVariant::QVariant(Qt::GlobalColor)' is private QVariant(Qt::GlobalColor) Q_DECL_EQ_DELETE; ^ – user628298 Mar 10 '14 at 15:14
  • @user628298: Fixed I hope. – user2672165 Mar 10 '14 at 15:25
  • How to increase the size of the table columns, you know? example: 300px each column, with 14px font. – user628298 Mar 11 '14 at 15:02
  • This is not quite correct way, I think. As far as this feature is all about View, you'd better subclass QStyledItemDelegate and reimplement paint method. After that you can apply you delegate to column with method ui->tableView->setItemDelegateForColumn(ColumnId, new QMyItemDelegate(this)); – Artem E Nov 12 '14 at 12:09