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. Qtableview delegate

Qtableview delegate

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

    Hi,
    I'm using Qtableview (QStandardItemModel ) and i used delegate in columns so only double type data is used, so its working but one issue is when i'm try to del data from my columns which i used delegate then its not deleting i can change the data why not delete happening ?
    shared the code--

    #include< QStyledItemDelegate>
    class IntDelegate : public QStyledItemDelegate
    {
     public:
     IntDelegate(QObject* parent = 0);
    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, 
    const QModelIndex& index) const;
    void setEditorData(QWidget* editor, const QModelIndex& index) const;
    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
    void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& 
    option, const QModelIndex& index) const;
     private:
     };
    //cpp file
    #include "IntDelegate.h"
    #include <QDoubleValidator>
     IntDelegate::IntDelegate(QObject* parent) :QStyledItemDelegate(parent)
     {
     }
    QWidget* IntDelegate::createEditor(QWidget* parent, const 
    QStyleOptionViewItem& option, const QModelIndex& index) const
    {
    QLineEdit* editor = new QLineEdit(parent);
    editor->setValidator(new QDoubleValidator);
    return editor;
    }
    void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) 
     const
      {
    double value = index.model()->data(index, Qt::EditRole).toDouble();
    QLineEdit* line = static_cast<QLineEdit*>(editor);
    line->setText(QString().setNum(value));
    }
    void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, 
    const QModelIndex& index) const
    {
    QLineEdit* line = static_cast<QLineEdit*>(editor);
    QString value = line->text();
    model->setData(index, value);
      }
     void IntDelegate::updateEditorGeometry(QWidget* editor, const 
    QStyleOptionViewItem& option, const QModelIndex& index) const
    {
    editor->setGeometry(option.rect);
    }
    //use 
     IntDelegate* intdelegate;
     intdelegate = new IntDelegate();
    ui.tableView->setItemDelegateForColumn(5, intdelegate);
    ui.tableView->setItemDelegateForColumn(4, intdelegate);
    ui.tableView->setItemDelegateForColumn(8, intdelegate);
    

    Any change req. in delegate file ? or I done something wrong ?

    VRoninV 1 Reply Last reply
    0
    • N n-2204

      Hi,
      I'm using Qtableview (QStandardItemModel ) and i used delegate in columns so only double type data is used, so its working but one issue is when i'm try to del data from my columns which i used delegate then its not deleting i can change the data why not delete happening ?
      shared the code--

      #include< QStyledItemDelegate>
      class IntDelegate : public QStyledItemDelegate
      {
       public:
       IntDelegate(QObject* parent = 0);
      QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, 
      const QModelIndex& index) const;
      void setEditorData(QWidget* editor, const QModelIndex& index) const;
      void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
      void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& 
      option, const QModelIndex& index) const;
       private:
       };
      //cpp file
      #include "IntDelegate.h"
      #include <QDoubleValidator>
       IntDelegate::IntDelegate(QObject* parent) :QStyledItemDelegate(parent)
       {
       }
      QWidget* IntDelegate::createEditor(QWidget* parent, const 
      QStyleOptionViewItem& option, const QModelIndex& index) const
      {
      QLineEdit* editor = new QLineEdit(parent);
      editor->setValidator(new QDoubleValidator);
      return editor;
      }
      void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) 
       const
        {
      double value = index.model()->data(index, Qt::EditRole).toDouble();
      QLineEdit* line = static_cast<QLineEdit*>(editor);
      line->setText(QString().setNum(value));
      }
      void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, 
      const QModelIndex& index) const
      {
      QLineEdit* line = static_cast<QLineEdit*>(editor);
      QString value = line->text();
      model->setData(index, value);
        }
       void IntDelegate::updateEditorGeometry(QWidget* editor, const 
      QStyleOptionViewItem& option, const QModelIndex& index) const
      {
      editor->setGeometry(option.rect);
      }
      //use 
       IntDelegate* intdelegate;
       intdelegate = new IntDelegate();
      ui.tableView->setItemDelegateForColumn(5, intdelegate);
      ui.tableView->setItemDelegateForColumn(4, intdelegate);
      ui.tableView->setItemDelegateForColumn(8, intdelegate);
      

      Any change req. in delegate file ? or I done something wrong ?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Because an empty string is not a vaid double.

      Change editor->setValidator(new QDoubleValidator); to

      const QRegularExpression doubleExpression(QStringLiteral(R"***(^(?:[-+]?\d*%1?\d+(?:%2[-+]?\d+)?)?$)***")
          .arg(QString(QRegularExpression::escape(editor->locale().decimalPoint())),QString(QRegularExpression::escape(editor->locale().exponential()))),QRegularExpression::CaseInsensitiveOption);
      editor->setValidator(new QRegularExpressionValidator(doubleExpression,editor));
      

      Few other fixes:

      • in setEditorData, line->setText(QString().setNum(value)); should become line->setText(line->locale().toString(value)); to work on languages that use different decimal separators from the USA
      • in setModelData, QString value = line->text(); model->setData(index, value); should become model->setData(index, line->locale().toDouble(line->text())); to store the numbers as numbers and not strings

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      JoeCFDJ 1 Reply Last reply
      3
      • N Offline
        N Offline
        n-2204
        wrote on last edited by VRonin
        #3

        Thanku , is any other chnage in code you shared bcz i'm getting error

             QWidget* IntDelegate::createEditor(QWidget* parent, const 
             QStyleOptionViewItem& option, const QModelIndex& index) const
            {
        	QLineEdit* editor = new QLineEdit(parent);
             const QRegularExpression doubleExpression(QStringLiteral(R"***(^(?:[-+]?\d*%1?\d+(?:%2[-+]?\d+)?)?$)***")
            .arg(QString(QRegularExpression::escape(locale().decimalPoint())), QS 
             tring(QRegularExpression::escape(locale().exponential()))), 
            QRegularExpression::CaseInsensitiveOption);
        	editor->setValidator(new QRegularExpressionValidator(doubleExpression, editor));
        	return editor;
        //here getting  error identifier locale is undefined
        
            }
             void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
            {
        	double value = index.model()->data(index, Qt::EditRole).toDouble();
        	QLineEdit* line = static_cast<QLineEdit*>(editor);
        	line->setText(line->locale().toString(value));
            } 
             void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, 
            const QModelIndex& index) const
             {
        	QLineEdit* line = static_cast<QLineEdit*>(editor);
        	QString value = line->text();
        	model->setData(line->locale().toDouble(line->text()), value);
        //error on line no suitable constructor exists to convert from "double" to "QModelIndex"
        
             }
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          They are both just silly things. I corrected the previous post

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • N Offline
            N Offline
            n-2204
            wrote on last edited by
            #5

            ok,,but using this code whats happening is when i click on cell (in column where delegate applied) but i will not enter data then also it will set to cell data to 0.
            can't be possible to leave empty ?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              sure.
              instead of model->setData(index, line->locale().toDouble(line->text()));
              put

              if(line->text().isEmpty())
              model->setData(index,QVariant())
              else
              model->setData(index, line->locale().toDouble(line->text()));
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              4
              • N Offline
                N Offline
                n-2204
                wrote on last edited by
                #7

                Ok Thankyou

                1 Reply Last reply
                0
                • VRoninV VRonin

                  Because an empty string is not a vaid double.

                  Change editor->setValidator(new QDoubleValidator); to

                  const QRegularExpression doubleExpression(QStringLiteral(R"***(^(?:[-+]?\d*%1?\d+(?:%2[-+]?\d+)?)?$)***")
                      .arg(QString(QRegularExpression::escape(editor->locale().decimalPoint())),QString(QRegularExpression::escape(editor->locale().exponential()))),QRegularExpression::CaseInsensitiveOption);
                  editor->setValidator(new QRegularExpressionValidator(doubleExpression,editor));
                  

                  Few other fixes:

                  • in setEditorData, line->setText(QString().setNum(value)); should become line->setText(line->locale().toString(value)); to work on languages that use different decimal separators from the USA
                  • in setModelData, QString value = line->text(); model->setData(index, value); should become model->setData(index, line->locale().toDouble(line->text())); to store the numbers as numbers and not strings
                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #8

                  @VRonin QString().setNum(value) ==>QString::number( value ) is ok as well.

                  VRoninV 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @VRonin QString().setNum(value) ==>QString::number( value ) is ok as well.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    @JoeCFD nope! In most of Europe, for example, the decimal part follows ,, QString::number will fail in that case. Always use QLocale when dealing with things presented to the user

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    3

                    • Login

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