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. QItemDelegate
Forum Updated to NodeBB v4.3 + New Features

QItemDelegate

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 678 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.
  • lincolnL Offline
    lincolnL Offline
    lincoln
    wrote on last edited by lincoln
    #1

    Hello friends, I have this code, I managed to put a QSpinBox in the first column of my QTableView, my question is how can I
    do to put a certain color to the value, or to the bottom of the cell according to the value that is placed, for example if it is greater than
    100 is painted red, any suggestion would be appreciated.

    Code:

    #ifndef SPINBOXDELEGATE_H
    #define SPINBOXDELEGATE_H
    
    #include <QObject>
    #include <QItemDelegate>
    #include <QSpinBox>
    
    class SpinBoxDelegate : public QItemDelegate
    {
      Q_OBJECT
    public:
      explicit SpinBoxDelegate(QObject *parent = nullptr);
    
    
      // QAbstractItemDelegate interface
    public:
      virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const override;
      virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override;
      virtual void setModelData(QWidget *editor, QAbstractItemModel *model,
                                const QModelIndex &index) const override;
      virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const override;
    };
    
    #endif // SPINBOXDELEGATE_H
    
    
    
    
    
    
    
    #include "spinboxdelegate.h"
    
    SpinBoxDelegate::SpinBoxDelegate(QObject *parent) : QItemDelegate(parent)
    {
    
    }
    
    QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
                                           const QStyleOptionViewItem &option,
                                           const QModelIndex &index) const
    {
      QSpinBox *sbControl=new QSpinBox(parent);
      sbControl->setMinimum(INT_MIN);
      sbControl->setMaximum(INT_MAX);
      (void)option;
      (void)index;
      return sbControl;
    
    }
    
    void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
      QSpinBox *sbControl=static_cast<QSpinBox *>(editor);
      int values=index.data().toInt();
      sbControl->setValue(values);
    
    }
    
    void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                       const QModelIndex &index) const
    {
      QSpinBox *sbControl=static_cast<QSpinBox *>(editor);
      model->setData(index,sbControl->value(),Qt::EditRole);
    
    }
    
    void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
                                               const QStyleOptionViewItem &option,
                                               const QModelIndex &index) const
    {
      editor->setGeometry(option.rect);
      (void)index;
    }
    
    
    
    Main form:
    
    #ifndef QITEMDELEGATEDIALOG_H
    #define QITEMDELEGATEDIALOG_H
    
    #include <QDialog>
    #include "spinboxdelegate.h"
    #include <QStandardItemModel>
    
    namespace Ui {
      class QItemDelegateDialog;
    }
    
    class QItemDelegateDialog : public QDialog
    {
      Q_OBJECT
    
    public:
      explicit QItemDelegateDialog(QWidget *parent = nullptr);
      ~QItemDelegateDialog();
    
    private:
      Ui::QItemDelegateDialog *ui;
      SpinBoxDelegate *spDelegate;
      QStandardItemModel *model;
    };
    
    #endif // QITEMDELEGATEDIALOG_H
    
    
    #include "qitemdelegatedialog.h"
    #include "ui_qitemdelegatedialog.h"
    
    QItemDelegateDialog::QItemDelegateDialog(QWidget *parent) :
      QDialog(parent), ui(new Ui::QItemDelegateDialog)
    {
      ui->setupUi(this);
      spDelegate=new SpinBoxDelegate(this);
      model=new QStandardItemModel(5,3,this);
      ui->tableView->setModel(model);
      ui->tableView->setItemDelegateForColumn(0,spDelegate);  
    }
    
    QItemDelegateDialog::~QItemDelegateDialog()
    {
      delete ui;
    }
    

    Solitary wolf

    1 Reply Last reply
    0
    • B Bonnie

      You could subclass QStandardItemModel like this:

      #ifndef SPINBOXITEMMODEL_H
      #define SPINBOXITEMMODEL_H
      
      #include <QStandardItemModel>
      
      class SpinBoxItemModel : public QStandardItemModel
      {
          Q_OBJECT
      public:
          explicit SpinBoxItemModel(QObject *parent = nullptr);
          SpinBoxItemModel(int rows, int columns, QObject *parent = nullptr);
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
      };
      
      #endif // SPINBOXITEMMODEL_H
      
      
      #include "spinboxitemmodel.h"
      
      SpinBoxItemModel::SpinBoxItemModel(QObject *parent)
          : QStandardItemModel(parent)
      {
      }
      
      SpinBoxItemModel::SpinBoxItemModel(int rows, int columns, QObject *parent)
          : QStandardItemModel(rows, columns, parent)
      {
      }
      
      QVariant SpinBoxItemModel::data(const QModelIndex &index, int role) const
      {
          if(role == Qt::BackgroundRole && index.data().toInt() > 100) {
              return QBrush(Qt::red);
          }
          return QStandardItemModel::data(index, role);
      }
      
      
      
        model=new SpinBoxItemModel(5,3,this);
      

      Please note that after you just finish editing a spinbox, the background color of the cell is a "Highlighted" color (maybe blue). It might not be red even the value is greater than 100. You need to select another cell to see the actual background color.

      lincolnL Offline
      lincolnL Offline
      lincoln
      wrote on last edited by
      #7

      @Bonnie thanks my friend, it is what I was looking for, solve my problem, greetings and success.

      Solitary wolf

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You can use a QIdentityProxyModel and in the data method return the color you want for the ForegroundRole.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • lincolnL Offline
          lincolnL Offline
          lincoln
          wrote on last edited by
          #3

          ok thank you, but i'm not sure about using that class.

          Solitary wolf

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Not sure about what ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • lincolnL Offline
              lincolnL Offline
              lincoln
              wrote on last edited by
              #5

              I mean how to apply that class, I don't know where to start, a practical example would help me, if possible of course. thanks

              Solitary wolf

              SGaistS 1 Reply Last reply
              0
              • B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #6

                You could subclass QStandardItemModel like this:

                #ifndef SPINBOXITEMMODEL_H
                #define SPINBOXITEMMODEL_H
                
                #include <QStandardItemModel>
                
                class SpinBoxItemModel : public QStandardItemModel
                {
                    Q_OBJECT
                public:
                    explicit SpinBoxItemModel(QObject *parent = nullptr);
                    SpinBoxItemModel(int rows, int columns, QObject *parent = nullptr);
                    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
                };
                
                #endif // SPINBOXITEMMODEL_H
                
                
                #include "spinboxitemmodel.h"
                
                SpinBoxItemModel::SpinBoxItemModel(QObject *parent)
                    : QStandardItemModel(parent)
                {
                }
                
                SpinBoxItemModel::SpinBoxItemModel(int rows, int columns, QObject *parent)
                    : QStandardItemModel(rows, columns, parent)
                {
                }
                
                QVariant SpinBoxItemModel::data(const QModelIndex &index, int role) const
                {
                    if(role == Qt::BackgroundRole && index.data().toInt() > 100) {
                        return QBrush(Qt::red);
                    }
                    return QStandardItemModel::data(index, role);
                }
                
                
                
                  model=new SpinBoxItemModel(5,3,this);
                

                Please note that after you just finish editing a spinbox, the background color of the cell is a "Highlighted" color (maybe blue). It might not be red even the value is greater than 100. You need to select another cell to see the actual background color.

                lincolnL 1 Reply Last reply
                0
                • B Bonnie

                  You could subclass QStandardItemModel like this:

                  #ifndef SPINBOXITEMMODEL_H
                  #define SPINBOXITEMMODEL_H
                  
                  #include <QStandardItemModel>
                  
                  class SpinBoxItemModel : public QStandardItemModel
                  {
                      Q_OBJECT
                  public:
                      explicit SpinBoxItemModel(QObject *parent = nullptr);
                      SpinBoxItemModel(int rows, int columns, QObject *parent = nullptr);
                      QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
                  };
                  
                  #endif // SPINBOXITEMMODEL_H
                  
                  
                  #include "spinboxitemmodel.h"
                  
                  SpinBoxItemModel::SpinBoxItemModel(QObject *parent)
                      : QStandardItemModel(parent)
                  {
                  }
                  
                  SpinBoxItemModel::SpinBoxItemModel(int rows, int columns, QObject *parent)
                      : QStandardItemModel(rows, columns, parent)
                  {
                  }
                  
                  QVariant SpinBoxItemModel::data(const QModelIndex &index, int role) const
                  {
                      if(role == Qt::BackgroundRole && index.data().toInt() > 100) {
                          return QBrush(Qt::red);
                      }
                      return QStandardItemModel::data(index, role);
                  }
                  
                  
                  
                    model=new SpinBoxItemModel(5,3,this);
                  

                  Please note that after you just finish editing a spinbox, the background color of the cell is a "Highlighted" color (maybe blue). It might not be red even the value is greater than 100. You need to select another cell to see the actual background color.

                  lincolnL Offline
                  lincolnL Offline
                  lincoln
                  wrote on last edited by
                  #7

                  @Bonnie thanks my friend, it is what I was looking for, solve my problem, greetings and success.

                  Solitary wolf

                  1 Reply Last reply
                  0
                  • lincolnL lincoln

                    I mean how to apply that class, I don't know where to start, a practical example would help me, if possible of course. thanks

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @lincoln said in QItemDelegate:

                    I mean how to apply that class, I don't know where to start, a practical example would help me, if possible of course. thanks

                    There's an example in the documentation of the class that I linked to.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    lincolnL 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @lincoln said in QItemDelegate:

                      I mean how to apply that class, I don't know where to start, a practical example would help me, if possible of course. thanks

                      There's an example in the documentation of the class that I linked to.

                      lincolnL Offline
                      lincolnL Offline
                      lincoln
                      wrote on last edited by
                      #9

                      @SGaist ok, thanks

                      Solitary wolf

                      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