QTableView: add delete button to each row



  • Hello all,
    I have a QTableView with QSqlRelationalTableModel as a model, I'm trying to add a button with a minus sign at the end of each row (a column of minus...), this button will delete the specific row and refresh the table.

    Any ideas?
    Thanks,
    G



  • Two approaches:

    Create a proxy model that adds a column in which you show a small image depicting the - sign button

    Create a custom delegate for your last column that draws this button.

    I think I would go with option 1, as that is a bit easier to do I think. Drawing code is always tricky to get right.



  • 1st option sounds good enough, but how do I create such a proxy model... also, as far as option two, I'm trying to understand how to implement the QItemDelegate ::paint() so, what I have now is:
    @
    /header portion/
    class RemoveRowButoonItemDelegate : public QItemDelegate {
    Q_OBJECT

    public:
    RemoveRowButoonItemDelegate(QObject *parent = 0);

    RemoveRowButoonItemDelegate(QString & iconResourceId, QObject *parent = 0);

    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;
    

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

    public slots:
    void removeRow();
    private:
    QString _iconResourceId;
    QToolButton * btn;

    /*implementation portion */

    RemoveRowButoonItemDelegate::RemoveRowButoonItemDelegate(QObject *parent) : QItemDelegate(parent){
    _iconResourceId = ":/myApp/IDB_MINUS";
    btn = new QToolButton(parent);
    QIcon icon;
    icon.addFile(_iconResourceId, QSize(), QIcon::Normal, QIcon::Off);
    btn->setIcon(icon);
    connect(btn, SIGNAL(clicked()), this, SLOT(removeRow()));
    }

    RemoveRowButoonItemDelegate::RemoveRowButoonItemDelegate(QString & _icon,QObject *parent) : QItemDelegate(parent), _iconResourceId(_icon){
    btn = new QToolButton(parent);
    QIcon icon;
    icon.addFile(_iconResourceId, QSize(), QIcon::Normal, QIcon::Off);
    btn->setIcon(icon);
    connect(btn, SIGNAL(clicked()), this, SLOT(removeRow()));
    }

    QWidget *RemoveRowButoonItemDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem & options,
    const QModelIndex &index) const {
    return btn;
    }

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

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

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

    void RemoveRowButoonItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const {
    if (option.state & QStyle::State_Selected)
    painter->fillRect(option.rect, option.palette.highlight());
    //?... how do I put btn inside the cell?
    }
    @
    I will add the filterEvent portion later, it will return false in all signals so, no editing will be allowed.

    so, how do I paint the button inside the rec of the cell?

    Thanks,
    G


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.