Relation entre Qtableview et modele



  • Bonjour,

    Comment colorier des lignes de ma vue avec un modèle. Je ne vois pas du tout comment faire.

    Merci de votre aide, voici mon code complet. Je pense que ça manque de cohérence.

    fenetre.h

    #ifndef FENETRE_H
    #define FENETRE_H

    #include <QWidget>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QSqlQueryModel>
    #include <QSqlTableModel>
    #include "monmodele.h"

    namespace Ui {
    class Fenetre;
    }

    class monModele;

    class Fenetre : public QWidget
    {
    Q_OBJECT

    public:
    explicit Fenetre(QWidget *parent = 0);
    ~Fenetre();

    void ouvrirBase();
    
    
    private:
    Ui::Fenetre *ui;
    
    QSqlQuery *mRequete;
        QSqlQueryModel *sqlModele;
        QSqlDatabase mDatabase;
         
        };
    

    #endif // FENETRE_H

    fenetre.cpp

    #include "fenetre.h"
    #include "monmodele.h"
    #include "ui_fenetre.h"

    Fenetre::Fenetre(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Fenetre)
    {
    ui->setupUi(this);
    }

    Fenetre::~Fenetre()
    {
    delete ui;
    }

    void Fenetre::ouvrirBase()
    {
    QString file = "C:\test\bd1.accdb";
    mDatabase = QSqlDatabase::addDatabase("QODBC");
    QString dataBaseString = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=" + file;
    mDatabase.setDatabaseName(dataBaseString);

              mDatabase.open();
    
              mRequete = new QSqlQuery();
                  mRequete->prepare("SELECT Numero, Nom FROM Compte WHERE Numero > 10 and Numero < 25");
    

    mRequete->exec();

    sqlModele = new QSqlTableModel;
    sqlModele->setQuery(*mRequete);

    ui->tableView->setModel(sqlModele);

     ui->tableView->show();
    

    }

    monmodele.h

    #ifndef MONMODELE_H
    #define MONMODELE_H

    #include <QObject>
    #include <QAbstractTableModel>
    #include <QColor>

    class monModele : public QAbstractTableModel
    {

    public:

       monModele(QObject *parent = 0);
    
       QVariant data ( const QModelIndex & index, int role)const;
    

    private:

    };

    #endif // MONMODELE_H

    monmodele.cpp

    #include "monmodele.h"
    #include <QSqlTableModel>

         monModele::monModele(QObject *parent) :  QAbstractTableModel(parent)
         {
              }
    
           QVariant monModele::data ( const QModelIndex & index, int role)const
          {
             if(role==Qt::BackgroundColorRole)
             {
                const QVariant value(data(index,Qt::DisplayRole));
                return QVariant(QColor(Qt::red));
             }
             return monModele::data(index,role);
          }
    

    [koahnig: moved to French forum]


  • Lifetime Qt Champion

    Bonjour,

    Vous avez déjà reçu plusieurs réponses sur ce cette question ci. Les avez-vous testées ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.