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] Get the value of Qdoublespixbox implemented in delegate
QtWS25 Last Chance

[SOLVED] Get the value of Qdoublespixbox implemented in delegate

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.6k 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.
  • A Offline
    A Offline
    advseo32
    wrote on last edited by
    #1

    Hello,

    i have a problem of getting a value of Qdoublespixbox imlemented in delegate

    @QWidget *customTableSellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    if(!index.isValid())
    return QStyledItemDelegate::createEditor(parent,option,index);
    QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
    doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
    doubleSpinBoxEditor->setSuffix(" D.A");
    doubleSpinBoxEditor->setDecimals(2);
    doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
    doubleSpinBoxEditor->setFrame(false);
    return doubleSpinBoxEditor;

    }@

    and i have added the delegate to my qtablwidget

    @ m_tableDelegate = new customTableSellDelegate(this);
    ui->tableWidget->setItemDelegate(m_tableDelegate);@

    after that when the user make some change in qtablewidget

    i tried to get a value from qtablewidget

    @double qty = ui->tableWidget->item(row,2)->text().toDouble(); @

    but it return 0 alway, even thought i write 25 , so what's the problem

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

      Hi,

      What does ui->tableWidget->item(row,2)->text() return ?

      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
      • A Offline
        A Offline
        advseo32
        wrote on last edited by
        #3

        i have writed 25 in qtablewidget but

        but ui->tableWidget->item(row,2)->text()

        returns "Ƀ砀Ƀሀ䑫\002簓Ʉ\000㬷\027Ԁȟ婟䭎儶敒瑣ㅆ椱瑮牥敳瑣摥剅卋_㖊\002ꬁɃ똀" ( using debugger )

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

          That doesn't look like a number.

          Did you completely implement the 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
          • A Offline
            A Offline
            advseo32
            wrote on last edited by
            #5

            Yes this is my custom delegate

            @#include "customtableselldelegate.h"
            #include <QDoubleSpinBox>
            #include <QComboBox>
            #include <QDebug>
            customTableSellDelegate::customTableSellDelegate(QObject *parent) :
            QStyledItemDelegate(parent)
            {
            }

            QWidget *customTableSellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
            if(!index.isValid())
            return QStyledItemDelegate::createEditor(parent,option,index);

            int col= index.column();
            
            if(col == 1)
            {
                QComboBox *comboboxEditor = new QComboBox(parent);
                comboboxEditor->addItem("Item1");
                comboboxEditor->addItem("Item2");
                comboboxEditor->addItem("Item3");
                comboboxEditor->addItem("Item4");
                comboboxEditor->addItem("Item5");
                comboboxEditor->addItem("Item6");
            
            
                return comboboxEditor;
            
            }
            // col3
            else if(col ==2 || col ==3 || col ==4 || col == 5 || col == 6 || col == 7)
            {
                QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
                doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
                doubleSpinBoxEditor->setSuffix(" D.A");
                doubleSpinBoxEditor->setDecimals(2);
                doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
                doubleSpinBoxEditor->setFrame(false);
                return doubleSpinBoxEditor;
            
            
            }else{
                return QStyledItemDelegate::createEditor(parent,option,index);
            }
            

            }

            void customTableSellDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
            {
            if(!index.isValid())
            return QStyledItemDelegate::setEditorData(editor,index);

            int col= index.column();
            
            if(col == 1)
            {
                QString data = index.model()->data(index,Qt::DisplayRole).toString();
                QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
            
                comboboxEditor->setCurrentText(data);
            }
            
            else if(col ==2 || col ==3 || col ==4 || col == 5 || col == 6 || col == 7)
            {
                double data = index.model()->data(index,Qt::DisplayRole).toDouble();
                QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
                doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
                doubleSpinBoxEditor->setSuffix(" D.A");
                doubleSpinBoxEditor->setDecimals(2);
                doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
                doubleSpinBoxEditor->setFrame(false);
                doubleSpinBoxEditor->setValue(data);
            
            }else{
                QStyledItemDelegate::setEditorData(editor,index);
            }
            

            }

            void customTableSellDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
            {
            if(!index.isValid())
            return QStyledItemDelegate::setModelData(editor,model,index);

            int col= index.column();
            
            if(col == 1)
            {
                QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
            
                model->setData(index,comboboxEditor->currentText(),Qt::EditRole);
            
                emit unlockRow(index);
            
            
            }
            // col3
            else if(col ==2 || col ==3 || col ==4 || col == 6 || col == 7)
            {
                QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
                doubleSpinBoxEditor->setInputMethodHints(Qt::ImhFormattedNumbersOnly);
                doubleSpinBoxEditor->setRange(0.000000,999999999.000000);
                doubleSpinBoxEditor->setSuffix(" D.A");
                doubleSpinBoxEditor->setDecimals(2);
                doubleSpinBoxEditor->setFrame(false);
                model->setData(index,doubleSpinBoxEditor->textFromValue(doubleSpinBoxEditor->value()),Qt::EditRole);
                if(col == 2 || col == 3)
                {
                    emit qtyPriceDataChanged(index);
                }
            }else{
                QStyledItemDelegate::setModelData(editor,model,index);}
            

            }

            void customTableSellDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
            editor->setGeometry(option.rect);

            }

            @

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

              Why are you setting the double spin box properties in each function ? You should only do that when creating the editor.

              Why ?
              @model->setData(index,doubleSpinBoxEditor->textFromValue(doubleSpinBoxEditor->value()),Qt::EditRole)@

              rather :

              @model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole)@

              As a side note, in setModelData, you don't handle column 5.

              Have a look at the Spin Box delegate example

              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
              • A Offline
                A Offline
                advseo32
                wrote on last edited by
                #7

                [quote author="SGaist" date="1380916344"]Why are you setting the double spin box properties in each function ? You should only do that when creating the editor.

                Why ?
                @model->setData(index,doubleSpinBoxEditor->textFromValue(doubleSpinBoxEditor->value()),Qt::EditRole)@

                rather :

                @model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole)@

                As a side note, in setModelData, you don't handle column 5.

                Have a look at the Spin Box delegate example[/quote]

                Yes, you're right , all mostly done , thanks a lot .

                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