Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

SOLVED - QT 4.7 QComboBox Custom Item Delegate Doesn't affect the current item displayed



  • Hello all,

    I've written a Custom Class extending QAbstractItemDelegate that draws some widget instead of the regular text.

    when the combo is closed, it doesn't use the custom delegate paint to draw the current selection, only when opening the drop down list does the delegate paint gets called.

    any idea how to make the current select displayed using the delegate?

    THX
    G



  • Hope this helps
    @
    #ifndef DITEMDELEGATE_H
    #define DITEMDELEGATE_H

    #include <QtGui/QItemDelegate>

    class QtMySQLGui;

    class DItemDelegate : public QItemDelegate
    {
    public:
    DItemDelegate(QtMySQLGui *parent, QString delegateType);
    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:
    QString delegateType;
    QtMySQLGui *parentWidget;
    };

    #endif // DITEMDELEGATE_H

    /***************************************************************************/

    #include <QtGui/QSpinBox>
    #include <QtGui/QComboBox>
    #include <QtGui/QDateTimeEdit>
    #include <QtGui/QLineEdit>

    #include "ditemdelegate.h"
    #include "qtmysqlgui.h"
    #include "staticfunctions.h"

    DItemDelegate::DItemDelegate(QtMySQLGui *parent, QString delegateType)
    {
    parentWidget = parent;
    this->delegateType = delegateType;
    }

    QWidget *DItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    Q_UNUSED(option);
    Q_UNUSED(index);
    if (delegateType == StaticFunctions::DelegateTypeNumber()) {
    QSpinBox *widEditor = new QSpinBox(parent);
    return widEditor;
    }
    if (delegateType == StaticFunctions::DelegateTypeComboEngines()) {
    QComboBox *widEditor = new QComboBox(parent);
    widEditor->addItems(parentWidget->serverConnection->getEngines());
    return widEditor;
    }
    if (delegateType == StaticFunctions::DelegateTypeComboCollations()) {
    QComboBox *widEditor = new QComboBox(parent);
    widEditor->addItems(parentWidget->serverConnection->getCollations());
    return widEditor;
    }
    if (delegateType == StaticFunctions::DelegateTypeDateTime()) {
    QDateTimeEdit *widEditor = new QDateTimeEdit(parent);
    widEditor->setCalendarPopup(true);
    widEditor->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
    return widEditor;
    }
    if (delegateType == StaticFunctions::DelegateTypePassword()) {
    QLineEdit *widEditor = new QLineEdit(parent);
    widEditor->setEchoMode(QLineEdit::Password);
    return widEditor;
    }
    }

    void DItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
    QVariant value = index.model()->data(index, Qt::EditRole);
    if (delegateType == StaticFunctions::DelegateTypeNumber()) {
    QSpinBox widEditor = static_cast<QSpinBox>(editor);
    widEditor->setValue(value.toInt());
    }
    if (delegateType == StaticFunctions::DelegateTypeComboEngines() || delegateType == StaticFunctions::DelegateTypeComboCollations()) {
    QComboBox widEditor = static_cast<QComboBox>(editor);
    widEditor->setCurrentIndex(widEditor->findText(value.toString()));
    }
    if (delegateType == StaticFunctions::DelegateTypeDateTime()) {
    QDateTimeEdit widEditor = static_cast<QDateTimeEdit>(editor);
    widEditor->setCalendarPopup(true);
    widEditor->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
    widEditor->setDateTime(QDateTime::fromString(value.toString(), "yyyy-MM-dd hh:mm:ss"));
    }
    if (delegateType == StaticFunctions::DelegateTypePassword()) {
    QLineEdit widEditor = static_cast<QLineEdit>(editor);
    widEditor->setText(value.toString());
    }
    }

    void DItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
    if (delegateType == StaticFunctions::DelegateTypeNumber()) {
    QSpinBox widEditor = static_cast<QSpinBox>(editor);
    // spinBox->interpretText();
    model->setData(index, widEditor->value(), Qt::EditRole);
    }
    if (delegateType == StaticFunctions::DelegateTypeComboEngines() || delegateType == StaticFunctions::DelegateTypeComboCollations()) {
    QComboBox widEditor = static_cast<QComboBox>(editor);
    model->setData(index, widEditor->currentText(), Qt::EditRole);
    }
    if (delegateType == StaticFunctions::DelegateTypeDateTime()) {
    QDateTimeEdit widEditor = static_cast<QDateTimeEdit>(editor);
    model->setData(index, widEditor->dateTime().toString("yyyy-MM-dd hh:mm:ss"), Qt::EditRole);
    }
    if (delegateType == StaticFunctions::DelegateTypePassword()) {
    QLineEdit widEditor = static_cast<QLineEdit>(editor);
    model->setData(index, widEditor->text());
    }
    }

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

    @



  • Solved it,
    actually, the delegate is not the problem,
    when painting the current selection selection QCombobox uses a re-implantation of
    @
    void paintEvent(QPaintEvent *e)
    @

    so here is what I did: I created a class derived from QCombobox and re-implantation that func.:
    @
    void MyCombo::paintEvent(QPaintEvent *e)
    {
    //this part is a copy of the orgonal
    Q_UNUSED(e)
    QStylePainter painter(this);
    painter.setPen(palette().color(QPalette::Text));

    // draw the combobox frame, focusrect and selected etc.
    QStyleOptionComboBox opt;
    initStyleOption(&opt);
    painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    
    
    //here is where the customization start!!!
    QRect rect = opt.rect.adjusted(1,1, -20, -2); //compensate for frame and arrow 
    ...
    

    }
    @

    I might totally redo this func.... and get some nice results. this is what I like about Qt!


Log in to reply