×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    Problem with an editor of a delegate in a tablefield.

    Hello all,

    In a project. I have a tablefield with a QSqlTableModel. Depending on the columns in the select I have different in a the tablefield column.
    The editor for that column can be a QLineEdit or a QTimeEdit.
    This worked well on the desktop but now I've ported the to the N900.
    What I see on the N900 is a thin line in the editor that supose to be my data. The editor accepts input correctly. But I only see this thin line.
    Question what am I doing wrong?

    Code:
    class MyDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        explicit MyDelegate(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;
    
    };
    
    MyDelegate::MyDelegate(QObject *parent) :
        QStyledItemDelegate(parent)
    {
    }
    
    QWidget* MyDelegate::createEditor(QWidget *parent,
                                             const QStyleOptionViewItem &option,
                                             const QModelIndex &index) const
    {
        const MyModel* coModel = qobject_cast<const MyModel*> (index.model());
        if(coModel == 0)
        {
            return QStyledItemDelegate::createEditor(parent, option, index);
        }
        // only columns 1 & 2 are editable. 0 is not editable
        switch(index.column())
        {
        case 1: // the other table
        {
            MyModel* oModel = const_cast<MyModel*>(coModel);
            QComboBox* oBox(new QComboBox(parent));
            oBox->setModel(oModel->otherTable());
            oBox->setModelColumn(otherTableModel::NAME);
            return oBox;
        }
            break;
        case 2: // the choice fields
        {
            QWidget* oEditor(0);
            if(coModel->lastColumnName() == "Texts")
            {
                oEditor = new QLineEdit(parent);
            }
            else
            {
                oEditor = new QTimeEdit(parent);
                oEditor->setDisplayFormat("hh:mm:ss");
            }
            return oEditor;
        }
            break;
        }
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
    
    void MyDelegate::setEditorData(QWidget *editor,
                                   const QModelIndex &index) const
    {
        const MyModel* coModel = qobject_cast<const MyModel*> (index.model());
        if(coModel == 0)
        {
            QStyledItemDelegate::setEditorData(editor, index);
            return;
        }
    
        switch(index.column())
        {
        case 1: // the other table
        {
            QComboBox* oBox = qobject_cast<QComboBox*>(editor);
            if(oBox == 0)
            {
                return;
            }
            oBox->setCurrentIndex(coModel->data(index, Qt::EditRole).toInt());
            break;
    
        }
        case 2: // the choice fields
        {
            QTimeEdit* oTimeEdit = qobject_cast<QTimeEdit*>(editor);
            if(oTimeEdit != 0)
            {
                // In Qt::DisplayRole the time is a QString
                oTimeEdit->setTime(coModel->data(index, Qt::EditRole).toTime());
                return;
            }
    
            QLineEdit* oLineEdit = qobject_cast<QLineEdit*>(editor);
            if(oLineEdit != 0)
            {
                // In Qt::DisplayRole the text is the same
                oLineEdit->setText(coModel->data(index, Qt::EditRole).toInt());
                return;
            }
    
        }
            break;
        default:
            QStyledItemDelegate::setEditorData(editor, index);
        }
    }
    
    void MyDelegate::setModelData(QWidget *editor,
                                  QAbstractItemModel *model,
                                  const QModelIndex &index) const
    {
        MyModel* oModel = qobject_cast<MyModel*> (model);
        if(oModel == 0)
        {
            QStyledItemDelegate::setModelData(editor, model, index);
            return;
        }
    
        switch(index.column())
        {
        case 1: // the other table
        {
            QComboBox* oBox = qobject_cast<QComboBox*>(editor);
            if(oBox == 0)
            {
                return;
            }
            int iTableId(oModel->otherTable()->id(oBox->currentIndex()));
            oModel->setData(index, iTableId, Qt::EditRole);
            break;
        }
        case 2: // the choice fields
        {
            QTimeEdit* oTimeEdit = qobject_cast<QTimeEdit*>(editor);
            if(oTimeEdit != 0)
            {
                oModel->setData(index, oTimeEdit->time(), Qt::EditRole);
            }
    
            QLineEdit* oLineEdit = qobject_cast<QLineEdit*>(editor);
            if(oLineEdit != 0)
            {
                oModel->setData(index, oLineEdit->text(), Qt::EditRole);
            }
        }
            break;
        default:
            QStyledItemDelegate::setEditorData(editor, index);
            break;
        }
    }
    Last edited by theokvdm; 2011-01-28 at 13:51. Reason: simplified code sample

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    Re: Problem with an editor of a delegate in a tablefield.

    I forgot to mention that the QComboBox shows its data correctly. The problem is only with the QLineEdit and the QTimeEdit.
    Last edited by theokvdm; 2011-01-29 at 09:59.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    Re: Problem with an editor of a delegate in a tablefield. The solution

    I was looking at all the posts I did and this one was still hanging.
    I found a 'solution' for it:

    Code:
    void MyDelegate::updateEditorGeometry(QWidget *editor,
                                                 const QStyleOptionViewItem &option,
                                                 const QModelIndex &index) const
    {
        if(index.column() != 2)
        {
            QStyledItemDelegate::updateEditorGeometry(editor, option, index);
            return;
        }
    
        QRect rectNewGeometry = option.rect;
        QTimeEdit* oTimeEdit = qobject_cast<QTimeEdit*>(editor);
        const int iNewWidth(oTimeEdit == 0 ? option.rect.width() * 1.25 : editor->sizeHint().width());
    
        rectNewGeometry.setSize(QSize(iNewWidth, editor->sizeHint().height()));
        rectNewGeometry.translate(option.rect.width() - iNewWidth,
                                  (option.rect.height() - editor->sizeHint().height())/2);
    
        editor->setGeometry(rectNewGeometry);
    }
    What it does it makes the editor bigger than the field and displays the editor on top of the field.
    This way the editor is editable.

    Regards,

    Theo

  4. #4
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Problem with an editor of a delegate in a tablefield.

    It could help if you publish complete sample application demonstrating the problem.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    Re: Problem with an editor of a delegate in a tablefield.

    If you want, then I will do that. I'm very busy right now but next week I will come back to this with working sample code.

    Regards,

    Theo

  6. #6
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Problem with an editor of a delegate in a tablefield.

    I believe there is nothing to excuse, we are all busy people and replying when you have time is a normal practice.
    For example, I was ignoring this thread because it seems to be a lot of work to find a problem and I'm not even sure that your snippet contains it.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    20

    Re: Problem with an editor of a delegate in a tablefield.

    So from what I understand you want your editor widget to be wider.
    I didn't test, but I think editor widget comes in this method with parent set to table cell, hence cell size restriction. It might be a solution to set parent to 0 and set sizes as you want.
    Again this might be foobar, but you can try.

Similar Threads

  1. problem with delegate paintmethod
    By srinivas.symbian in forum Qt
    Replies: 1
    Last Post: 2010-12-21, 07:26
  2. problem in delegate paint..
    By srinivas.symbian in forum Qt
    Replies: 1
    Last Post: 2010-12-08, 11:52
  3. Replies: 6
    Last Post: 2010-06-03, 17:56
  4. Replies: 0
    Last Post: 2010-04-08, 11:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •