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 - QT 4.7 QComboBox Custom Item Delegate Doesn't affect the current item displayed
Forum Updated to NodeBB v4.3 + New Features

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

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

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dvillalobos
      wrote on last edited by
      #2

      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);
      }

      @

      Regards

      David

      1 Reply Last reply
      0
      • G Offline
        G Offline
        galrub
        wrote on last edited by
        #3

        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!

        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