Frage

I need my QTableView that is rendering items using QSqlTableModel to have in a column checkboxes and to have in other columns multiline textEdits because I need new lines when the user presses the Enter button. It's a lot of stuff, but if I can get the checkboxes in the Table I can make the rest.

I have tried this, but it doesn't work for the application crashes.

The question here that has an answer doesn't help either because they are using QStandardItemModel and I strictly need to use QSqlTableModel.

How can I accomplish the checkbox part while still using QSqlTableModel?

War es hilfreich?

Lösung

You can create a custom delegate for your checkbox like this:

#include <QItemDelegate>

#include <QCheckBox>

#include <QPainter>



class CheckBoxDelegate: public QItemDelegate
{
    Q_OBJECT
public:
    CheckBoxDelegate(QObject *parent = 0);

    void paint( QPainter *painter,
                        const QStyleOptionViewItem &option,
                        const QModelIndex &index ) const;


    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;

    mutable QCheckBox * theCheckBox;

private slots:

    void setData(bool val);


};


CheckBoxDelegate::CheckBoxDelegate(QObject *parent ):QItemDelegate(parent)
{
}

void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    drawDisplay(painter,option,option.rect,index.model()->data( index, Qt::DisplayRole ).toBool()?QString("      ").append(tr("Yes")):QString("      ").append(tr("No")));
    drawFocus(painter,option,option.rect);
}

QWidget *CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    theCheckBox = new QCheckBox( parent );
    QObject::connect(theCheckBox,SIGNAL(toggled(bool)),this,SLOT(setData(bool)));
    return theCheckBox;
}

void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int val = index.model()->data( index, Qt::DisplayRole ).toInt();

    (static_cast<QCheckBox*>( editor ))->setChecked(val);

}

void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    model->setData( index, (int)(static_cast<QCheckBox*>( editor )->isChecked() ) );
}


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

void CheckBoxDelegate::setData(bool val)
{
    emit commitData(theCheckBox);
}

Next in your code assign your custom item delegate to the desired column:

ui->myTable->setItemDelegateForColumn(5,new CheckBoxDelegate(ui->myTable));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top