Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView: add delete button to each row
Forum Updated to NodeBB v4.3 + New Features

QTableView: add delete button to each row

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 8.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    galrub
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        galrub
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved