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. Custom QItemDelegate destroy editor on focus out

Custom QItemDelegate destroy editor on focus out

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

    I have subclassed QItemDelegate in order to show a qspinbox and a numpad to insert numbers into the table:

    @class HwIoBoardTableItemDelegate: public QItemDelegate
    {
    Q_OBJECT

    public:
    HwIoBoardTableItemDelegate(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;
    

    };@

    And the implementation:
    @QWidget *HwIoBoardTableItemDelegate::createEditor(QWidget parent,
    const QStyleOptionViewItem &/
    option */,
    const QModelIndex & index ) const
    {
    QWidget * editor = 0;

    switch (index.column()) {
    case 1:
        editor = new myQSpinBoxDirect(parent);
        break;
    case 2:
    case 3:
        editor = new myQSpinBoxDirect(parent,0,32);
        break;
    case 4:
        editor = new QComboBox(parent);
        for(int i = 0; i<Params::Instance()->hwIoBoardType.count();i++){
            ((QComboBox*)editor)->addItem(Params::Instance()->hwIoBoardType.at(i),Params::Instance()->hwIoBoardType.at(i));
        }
        break;
    default:
        break;
    }
    return editor;
    

    }

    void HwIoBoardTableItemDelegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const
    {
    myQSpinBoxDirect *spinBox;
    QComboBox *combo;
    int valueInt;
    QString valueString;

    switch (index.column()) {
    case 1:
    case 2:
    case 3:
        valueInt = index.model()->data(index, Qt::EditRole).toInt();
        spinBox = static_cast<myQSpinBoxDirect*>(editor);
        spinBox->setValue(valueInt);
        break;
    case 4:
        valueString = index.model()->data(index, Qt::EditRole).toString();
        combo = static_cast<QComboBox*>(editor);
        for(int i = 0; i<Params::Instance()->hwIoBoardType.count();i++){
            if(Params::Instance()->hwIoBoardType.at(i).compare(valueString)==0){
                combo->setCurrentIndex(i);
            }
        }
    default:
        break;
    }
    

    }

    void HwIoBoardTableItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {
    myQSpinBoxDirect *spinBox;
    QComboBox *combo;
    int valueInt;
    QString valueString;

    switch (index.column()) {
    case 1:
    case 2:
    case 3:
        spinBox = static_cast<myQSpinBoxDirect*>(editor);
        valueInt = spinBox->value();
        model->setData(index, valueInt, Qt::EditRole);
        break;
    case 4:
        combo = static_cast<QComboBox*>(editor);
        valueString = combo->itemData(combo->currentIndex()).toString();
        model->setData(index, valueString, Qt::EditRole);
    default:
        break;
    }
    

    }

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

    And the myQSpinBoxDirect extended QSpinbox in that way:
    @class myQSpinBoxDirect: public QSpinBox
    {
    Q_OBJECT

    public:
    myQSpinBoxDirect(QWidget *parent = 0,int minimum = 0, int maximum = 1000000);
    ~myQSpinBoxDirect();

    bool event(QEvent *event);
    

    protected:
    numPadLinked *num;
    };@

    @myQSpinBoxDirect::myQSpinBoxDirect(QWidget * parent, int minimum, int maximum):QSpinBox(parent)
    {
    this->setButtonSymbols(QAbstractSpinBox::NoButtons);
    this->setParent(parent);
    this->setMinimum(minimum);
    this->setMaximum(maximum);
    this->show();

    num=new numPadLinked(0,false);
    connect(num,SIGNAL(value_changed_int(int)),this,SLOT(setValue(int)));
    connect(num,SIGNAL(closing()),num,SLOT(deleteLater()));
    connect(num,SIGNAL(ok()),num,SLOT(deleteLater()));
    num->show("");
    num->raise();
    num->activateWindow();
    

    }

    myQSpinBoxDirect::~myQSpinBoxDirect()
    {
    int i = 0;
    }

    bool myQSpinBoxDirect::event(QEvent *event)
    {

    if(QSpinBox::event(event)){
        qDebug()<<event->type();
        return true;
    }else{
        return false;
    }
    

    }
    @

    The problem is when the numpad i showed the mySpinBoxDirect loose the focus(maybe) and is destroyed so I can't change the value by the numpad.

    On windows platform it works fine in linux doesn't.

    Any suggestions?

    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