ItemDelegate der nicht nur auf Edit Role angezeigt wird?

Alles rund um die Programmierung mit Qt
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

ItemDelegate der nicht nur auf Edit Role angezeigt wird?

Beitrag von gast23 »

Hi,

ich habe einen kleinen Checkbox Delegate implementiert.

Code: Alles auswählen

#include "checkboxdelegate.h"

#include <QDebug>

CheckboxDelegate::CheckboxDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

QWidget* CheckboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const {

    QCheckBox *editor = new QCheckBox(parent);
    return editor;

}

void CheckboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {

    bool value = index.model()->data(index, Qt::EditRole).toBool();

    qDebug() << value;

    QCheckBox *check = static_cast<QCheckBox*>(editor);
    check->setChecked(value);

}

void CheckboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const {

    QCheckBox *check = static_cast<QCheckBox*>(editor);
    model->setData(index, check->isChecked(), Qt::EditRole);

    qDebug() << "setModelData" << check->isChecked();

}

void CheckboxDelegate::updateEditorGeometry(QWidget *editor,
                                            const QStyleOptionViewItem &option, const QModelIndex &index) const {

    editor->setGeometry(option.rect);

}
Dieser Delegate wird nur angezeigt wenn man per doppel click in die Celle klickt. Ich möchte jedoch das die Checkbox immer angezeigt wird und man einfach die Checkbox anklicken kann.

Viele Grüße
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Für eine CheckBox braucht man doch kein eigenes Delegate.

Schau dir mal Qt::CheckStateRole und Qt::CheckState an.
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Das beantwortet nicht meine Frage...
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Der Delegate ist nur ein Test... ich werd eine eigene Checkbox später implementieren...
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Kann mir niemand helfen? :(
ScyllaIllciz
Beiträge: 200
Registriert: 9. Juli 2010 19:31

Beitrag von ScyllaIllciz »

Schau mal hier, vielleicht hilft Dir das weiter.
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Oh das hört sich nach Arbeit an. Vielen Dank.

@upsala hast du ein Beispiel mit Checkstate?

Ich bekomme es zwar hin jedoch erscheint trotzdem noch der default Editor (Eine Combobox)
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Normalerweise reicht es aus hier

Code: Alles auswählen

Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const 
zusätzlich Qt::ItemIsUserCheckable zurückzugeben.

und bei

Code: Alles auswählen

QVariant QAbstractItemModel::data(const QModelIndex &index, int role=Qt::DisplayRole) const
wenn die Role Qt::CheckStateRole abgefragt wird einen davon: Qt::Unchecked, Qt::PartiallyChecked, Qt::Checked
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Ah ok!

Läßt sich diese 'Checkbox auch stylen?

Viele Grüße
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Aus den Sourcen von Qt:

Code: Alles auswählen

style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
Somit wird des mit dem 'normalen' Widget-Style gezeichnet.
Zuletzt geändert von upsala am 6. September 2010 19:52, insgesamt 1-mal geändert.
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Ich habe in meinem Stylesheet eine QCheckbox default mäßig gestylt jedoch übernimmt er es nicht... :(

Desweiteren kann ich meine Checkboxen die über QCheckState erzeugt werden nicht ändern :(

column 4 als Beispiel:

Code: Alles auswählen

Qt::ItemFlags MyModel::flags(const QModelIndex &index) const {

    if (!index.isValid())
        return Qt::ItemIsEnabled;

    if (index.column() == 4)
        return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;

}

Code: Alles auswählen

QVariant MyModel::data(const QModelIndex &index, int role) const {

    if (!index.isValid())
        return QVariant();

    if (index.row() >= liste->size() || index.row() < 0)
        return QVariant();

    EinObjekt* b = liste->at(index.row());

    if (role == Qt::DisplayRole || role == Qt::EditRole) {

        if (index.column() == 0)
            return b->...
        if (index.column() == 1)
            return b->..
        if (index.column() == 2)
            return b->sparte;
        if (index.column() == 3)
            return b->..

    } else if(role == Qt::CheckStateRole) {

//bool
        if (index.column() == 4)
            return b->...;

    }

    return QVariant();

}

Code: Alles auswählen

bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) {

    if (index.isValid() && role == Qt::CheckStateRole) {

        if(index.column() == 4) {

            liste->at(index.row())->ist = value.toBool();
            emit dataChanged(index, index);
            return true;

        } 

    }

    return false;

}
Wäre um Hilfe dankbar!
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Hallo?
solarix
Beiträge: 1133
Registriert: 7. Juni 2007 19:25

Beitrag von solarix »

Code: Alles auswählen

QVariant MyModel::data(const QModelIndex &index, int role) const {
     ...
        if (index.column() == 4)
            return b->ist ? Qt::Checked : Qt::Unchecked;

     ...
    }
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Danke!! Es hat endlich funktioniert :)

Nun ist die Frage wie ich diese Checkboxen stylen kann.

In meinem globalen Sheet ist eine Definition jedoch nutzt er die nicht :(

Hat jemand eine Idee?
gast23
Beiträge: 103
Registriert: 11. August 2010 10:43

Beitrag von gast23 »

Niemand?
Antworten