2

I have a QTableView of 4 rows and 4 columns. I am trying to add a QPushButton to all cells of the last column only, with the exception of the first row. When I run my code I am able to see the table but when I click on each cell of the 4 column I see the QPushButton but it disappears instantly. Only the word "Detail" on the QPushButton remains visible. And I still see the QPushButton on the first row. Any ideas?

This is the buttoncolumndelegate.cpp

#include "buttoncolumndelegate.h"

ButtonColumnDelegate::ButtonColumnDelegate(QObject *parent) :
    QItemDelegate(parent)
{

}
QWidget * ButtonColumnDelegate::createEditor(QWidget *parent,
            const QStyleOptionViewItem &option,
            const QModelIndex &index) const
{
    QPushButton *detail = new QPushButton("Detail",parent);
    detail->setText("Detail");
    (void) option;
    (void) index;
    return detail;
}

void ButtonColumnDelegate::setEditorData(QWidget *editor,
                                     const QModelIndex &index) const
{
    QPushButton *detail = qobject_cast<QPushButton *>(editor);
    detail->setProperty("Detail", "Detail");
    detail->setText("Detail");
    (void) index;
}

void ButtonColumnDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const
{
    QPushButton *detail = qobject_cast<QPushButton *>(editor);
    model->setData(index, detail->property("Detail"));

}

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

This is the dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include "buttoncolumndelegate.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    mybutton = new ButtonColumnDelegate(this);
    mModel = new QStandardItemModel(4,4,this);
    ui->tableView->setModel(mModel);
    ui->tableView->setItemDelegateForColumn(3, mybutton);

}

Dialog::~Dialog()
{
    delete ui;
}                     
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • "when I click on each cell of the 4 column". You mean each cell of the 4th column? I.e you clicking the buttons? – spinkus Feb 24 '18 at 03:47

1 Answers1

2

createEditor() is called only when you want to edit a widget, instead if you want a button to be displayed when you are not editing a value you must overwrite the paint() method

void ButtonColumnDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QPushButton button(index.data().toString());
    button.setGeometry(option.rect);
    painter->save();
    painter->translate(option.rect.topLeft());
    button.render(painter);
    painter->restore();
}

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks it works! :) Let me ask you another question, I would like to make the push button clickable. How can I do that? I would like to open another window where I have another table (this table needs to be created and it can be thought of something similar to your screen-shot). I am basically creating a nested table. –  Jan 27 '18 at 01:12
  • @Emanuele I do not understand, explain yourself better. Also, if my answer helps you, do not forget to mark it as correct, if you do not know how to do it, check the following link: [tour] – eyllanesc Jan 27 '18 at 01:15
  • Yes I marked the answer as correct because it was exactly what I was looking for, thanks Eyllanesc! :) –  Jan 27 '18 at 15:33
  • The additional question I have is: I am trying to click on the button "Detail" I just created because I need to open another Dialog window. How can I make the button clickable? –  Jan 27 '18 at 15:37