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. [solved] openPersistentEditor(index) not working
Forum Updated to NodeBB v4.3 + New Features

[solved] openPersistentEditor(index) not working

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.4k 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.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    Dear friends,

    Can you help me please?

    I'm trying to apply QSpinbox delegate to my application.

    I tried to imitate Spin Box Delegate Example in Qt creator and "QItemDelegate tutorial video":http://www.youtube.com/watch?v=EJf-vZ6FQfc.

    I successfully created the delegates. But openPersistentEditor() method not working. Which use to display the data already in edit mode.

    What is missing?

    Here my codes.

    @ QStandardItemModel *item_model;
    ComponentDelegate *component_delegate;

        item_model = new QStandardItemModel(4,2,this);
        component_delegate = new ComponentDelegate(this);
    
        for (int row = 0; row < 4; ++row) {
            for (int col = 0; col < 2; ++col) {
                QModelIndex index = item_model->index(row,col,QModelIndex());
                ui->height_amperate_tableview->openPersistentEditor(index);
                item_model->setData(index,0);
            }
        }
    
        ui->height_amperate_tableview->horizontalHeader()->setStretchLastSection(true);
        ui->height_amperate_tableview->setModel(item_model);
        ui->height_amperate_tableview->setItemDelegate(component_delegate);@
    

    @class ComponentDelegate:public QItemDelegate
    {
    public:
    ComponentDelegate(QObject *parent): QItemDelegate(parent){}

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QSpinBox *editor=new QSpinBox(parent);
        editor->setMinimum(0);
        editor->setMaximum(100);
        return editor;
    }
    
    void setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        int value = index.model()->data(index,Qt::EditRole).toInt();
    
        QSpinBox *spinbox = static_cast<QSpinBox*>(editor);
        spinbox->setValue(value);
    }
    
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        QSpinBox *spinbox = static_cast<QSpinBox*>(editor);
        spinbox->interpretText();
        int value = spinbox->value();
        model->setData(index,value,Qt::EditRole);
    }
    
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        editor->setGeometry(option.rect);
    }
    

    };@

    Thanks in advance.

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

      Hi,

      One thing from your code that look strange is that you are opening the persistent editor before setting your delegate.

      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
      • H Offline
        H Offline
        Hareen Laks
        wrote on last edited by
        #3

        Thank you for replying me.

        As you mentioned I set the delegate before opening the persistent editor.

        @
        QStandardItemModel *item_model;
        ComponentDelegate *component_delegate;

        item_model = new QStandardItemModel(4,2,this);
        component_delegate = new ComponentDelegate(this);

        ui->height_amperate_tableview->horizontalHeader()->setStretchLastSection(true);

        ui->height_amperate_tableview->setModel(item_model);

        ui->height_amperate_tableview->setItemDelegate(component_delegate);

        for (int row = 0; row < 4; ++row) {
        for (int col = 0; col < 2; ++col) {
        QModelIndex index = item_model->index(row,col,QModelIndex());
        ui->height_amperate_tableview->openPersistentEditor(index);
        item_model->setData(index,0);
        }
        }

        @

        It is working Now.

        Thank you very much for your help.

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

          You're welcome !

          Since your view is working please update the thread title prepending [solved] so other forum users may know a solution has been found :)

          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
          • H Offline
            H Offline
            Hareen Laks
            wrote on last edited by
            #5

            just forgot. :)

            thanks.

            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