How do I get my QTableView with QSqlTableModel to have checkboxes and multiline?

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?

Answers


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

Need Your Help

Django migration being killed

python django postgresql migration

I'm quite confident with Django, but have mostly relied on generated migrations until recently. I wrote a small custom migration, and shortly after my CI started complaining about timeouts and it e...

Is it safe to "send" passwords via javascript to another website

javascript security user-interface interface

The web hosting service I am using has a bit of an ugly UI for logging into domain emails, so I just wanted to put a thin interface over the login screen. My goal is to have users submit their login