QTableView und ComboBox rendert falsch



  • Morgen,

    ich habe momentan ein Problem mit einem QTableView, dass ich bisher nicht lösen konnte. Ich habe in einer Spalte des QTableViews eine ComboBox mit einem Delegate. Eigentlich funktioniert auch alles so wie es soll, mit der Ausnahme, dass beim hinzufügen von neuen Zeilen, außerhalb der QTableView gerendert wird.

    Sehen kann man das hier https://imgur.com/a/GqlGZ

    Dazu mal der Code aus dem QStyledItemDelegate.

    #include <QPainter>
    #include <QLineEdit>
    #include "doctorcontactdelegate.h"
    #include "doctorcombobox.h"
    
    DoctorContactDelegate::DoctorContactDelegate(QWidget *parent) :
        QStyledItemDelegate(parent)
    {
    }
    
    void DoctorContactDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if (index.column() == 0)
        {
            painter->fillRect(option.rect, QBrush(QColor(215, 215, 215, 84)));
            QStyledItemDelegate::paint(painter, option, index);
        }
        else if (index.data().canConvert<DoctorComboBox*>())
        {
            DoctorComboBox *cbox = qvariant_cast<DoctorComboBox*>(index.data());
            cbox->setGeometry(option.rect);
            cbox->render(painter, painter->deviceTransform().map(option.rect.topLeft()));
        }
        else
            QStyledItemDelegate::paint(painter, option, index);
    }
    
    QWidget *DoctorContactDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        Q_UNUSED(option)
    
        if (index.data().canConvert<DoctorComboBox*>())
        {
            DoctorComboBox *cbox = new DoctorComboBox(parent);
            return cbox;
        }
        else if (index.column() == 1 || index.column() == 3)
        {
            QLineEdit *eline = new QLineEdit(parent);
            return eline;
        }
        else
            return nullptr;
    }
    
    void DoctorContactDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        QString value = index.model()->data(index).toString();
    
        if (index.data().canConvert<DoctorComboBox*>())
        {
            DoctorComboBox *cbox = qobject_cast<DoctorComboBox*>(editor);
            cbox->setCurrentIndex(cbox->findText(value));
        }
        else if (index.column() == 1 || index.column() == 3)
        {
            QLineEdit *eline = qobject_cast<QLineEdit*>(editor);
            eline->setText(value);
        }
    }
    
    void DoctorContactDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        if (index.data().canConvert<DoctorComboBox*>())
        {
            DoctorComboBox *cbox = qobject_cast<DoctorComboBox*>(editor);
            int value = cbox->currentIndex();
            model->setData(index, value);
        }
        else if (index.column() == 1 || index.column() == 3)
        {
            QLineEdit *eline = qobject_cast<QLineEdit*>(editor);
            QString value = eline->text();
            model->setData(index, QVariant(value));
        }
    }
    
    void DoctorContactDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if (index.data().canConvert<DoctorComboBox*>())
            editor->setGeometry(option.rect);
        else if (index.column() == 1 || index.column() == 3)
            editor->setGeometry(option.rect);
    }
    

    Wenn ich den Part kommentiere wo die cbox gerendert wird, dann habe ich das Problem nicht. Es scheint irgendwie ein Problem mit der ComboBox zu sein. Ich weiß das ich eine eigene ComboBox nutze, aber die erbt von einer Standard QComboBox. Ich habe auch schonmal eine Standard ComboBox benutzt, aber da habe ich das selbe Problem. Kennt das jemand?

    Wenn ich die ComboBox gegen ein QLabel austausche, um in der Spalte nur den Wert anzuzeigen und nur im EditMode die ComboBox anzeige, ist das Problem auch vorhanden. Wenn ich komischerweise die beiden Buttons "hinzufügen" und "entfernen" nach oben verschiebe, habe ich das Problem wieder nicht.

    Ich hatte vorher QT 5.9 benutzt und habe nun auf 5.9.1 geupdatet, weil ich gehofft habe das sich das Problem löst, aber nein.

    Kann mir jemand helfen?


Anmelden zum Antworten