Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. French
  4. Problème checkbox avec delegue
QtWS25 Last Chance

Problème checkbox avec delegue

Scheduled Pinned Locked Moved Unsolved French
1 Posts 1 Posters 452 Views
  • 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.
  • L Offline
    L Offline
    lacombe
    wrote on 6 Jul 2019, 14:34 last edited by
    #1

    Bonjour,
    Lorsque j'appelle le delegué ma case à cocher est inactive, alors que sans le delégué ça marche. Merci de votre aide.
    Vioci le code:

    #include "paystablemodel.h"
    #include "delegue.h"
    
    PaysTableModel::PaysTableModel(const QStringList &listePays, QList<int> &listeCases, QObject *parent):QAbstractTableModel(parent)
    
     {if (listePays.count() > 0) this->listePays = listePays;
    if (listeCases.count() > 0) this->listeCases = listeCases;
    
     }
    int PaysTableModel::rowCount(const QModelIndex &parent) const
    { return parent.isValid() ? 0 : this->listePays.count(); }
    //{return 5;}
    int PaysTableModel::columnCount(const QModelIndex &parent) const
    { return parent.isValid() ? 0 : NbColonnes; }
    //{return 2;}
    
    QVariant PaysTableModel::data(const QModelIndex &index, int role) const {
    if (! index.isValid()) return QVariant() ;
    if(((unsigned int)index.row()>=this->listePays.count())||(index.column()>=NbColonnes))
    return QVariant();
    if ((role == Qt::DisplayRole || role == Qt::EditRole)) {
    if(index.column() == Pays) return this->listePays.at(index.row());}
    
    if (role == Qt::CheckStateRole && (index.column() == Cases))
    
       {
             {int checked  = listeCases.at(index.row());
             if (checked)
             {return Qt::Checked;}
             else
             {return Qt::Unchecked;}
             }
     }
     else
    
         {
    return QVariant();
    }
    
    }
    bool PaysTableModel::setData(const QModelIndex &index, const QVariant &value, int role){
         if (index.column() == Cases && role == Qt::CheckStateRole) {
     this->listeCases.replace(index.row(), value.toInt());
      if (index.column() == Cases)
          { emit(dataChanged(index,index));
        return false;}
        }
        return true;
    }
    
    Qt::ItemFlags PaysTableModel::flags(const QModelIndex &index) const {
    
    if (index.column() == Pays) 
    {
    return   Qt::ItemIsEnabled;
    }
    else if (index.column() == Cases) 
    {
    return  Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
    
    }
    return QAbstractTableModel::flags(index);
    }
    
    #include "delegue.h"
    #include "paystablemodel.h"
    
    Delegue::Delegue(QObject *parent) :
        QStyledItemDelegate(parent)
    {
    
    }
    
    QWidget *Delegue::createEditor(QWidget *parent,
        const QStyleOptionViewItem &option,
        const QModelIndex &index ) const
    {
        if (index.column() == 1)
            {
        QCheckBox *editor = new QCheckBox(parent);
     
        return false;
            }
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
    
    
    
    void Delegue::setEditorData(QWidget *editor,
                                         const QModelIndex &index) const
     {
        if (index.column() == 1)
            {
            int value = index.model()->data(index, Qt::DisplayRole).toInt();
             QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
            if(value == 1) {
                    checkBox->setCheckState(Qt::Checked);
             } else {
                    checkBox->setCheckState(Qt::Unchecked);
                    }
        }
        else
            {
                QStyledItemDelegate::setEditorData(editor, index);
            }
     }
    
    
    
    void Delegue::setModelData(QWidget *editor, QAbstractItemModel *model,
                                        const QModelIndex &index) const
     {
        if (index.column() == 1)
            {
                QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
                int value;
                if(checkBox->checkState() == Qt::Checked)
                    value = 1;
                else
                    value = 0;
                model->setData(index, value);
        }
        else
            {
                QStyledItemDelegate::setModelData(editor, model, index);
            }
     }
    
    
    void Delegue::updateEditorGeometry(QWidget *editor,
         const QStyleOptionViewItem &option, const QModelIndex &index) const
     {
        Q_UNUSED(index);
        editor->setGeometry(option.rect);
     }
    
    
    void Delegue::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if (index.column() == 1)
        {
            int value = index.model()->data(index, Qt::DisplayRole).toInt();
            QStyleOptionButton check_box;
            check_box.state = QStyle::State_Enabled;
            check_box.rect = option.rect;
            if (value == 1) {
                check_box.state = QStyle::State_On|QStyle::State_Enabled;;
            }
            else {
                check_box.state = QStyle::State_Off|QStyle::State_Enabled;;
            }
          QApplication::style()->drawControl(QStyle::CE_CheckBox, &check_box, painter);
        }
        else
            {
                QStyledItemDelegate::paint(painter, option, index);
            }
    }
    
    
    
    #include "essai0.h"
    #include "ui_essai0.h"
    
    essai0::essai0(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::essai0)
    {
        ui->setupUi(this);
    
        listeP << "France" << "Angleterre" << "Espagne" << "Italie" << "Allemagne";
        listeC << 1 << 0 << 0 << 0 << 1;
    
            PaysTableModel *modele = new PaysTableModel(listeP, listeC);
            Delegue *mydelegue = new Delegue();
            ui->tableView->setModel(modele);
      //  ui->tableView->setItemDelegate(mydelegue);
    
       }
    
    essai0::~essai0()
    {
        delete ui;
    }
    
    1 Reply Last reply
    0

    1/1

    6 Jul 2019, 14:34

    • Login

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