PDA

View Full Version : Need help with QAbstractItemView::setIndexWidget()



ayanda83
23rd November 2016, 08:27
I want to filling the second column of my TableView with pushbuttons so I wrote the code below but the output is not what I want. Please see the attached snapshot of the output. As can been seen in the output window, the buttons are stacked at the top left-hand corner instead of being listed on the second column
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QStandardItemModel>

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

QStandardItemModel *model = new QStandardItemModel(41,2,this);


for (int i = 0; i <= 41; i++)
{
QPushButton *btn = new QPushButton("E-Mail", this);
ui->tableView->setIndexWidget(model->index(i, 2, QModelIndex()), btn);
}
ui->tableView->setModel(model);
}12230

anda_skoa
23rd November 2016, 09:41
Have you tried creating the buttons without parent?

Cheers,
_

ayanda83
23rd November 2016, 09:52
Have you tried creating the buttons without parent?
_
Yes I have, when I remove the parent on the buttons, the buttons disappear all together. At one point I did that and buttons appeared outside the window output. I also noticed the error in my code above, that I am pulling the model index from row i and column 3 when the model only has 2 columns. I changed that, as can be seen below, but the outcome is still the same.
QModelIndex index = model->index(i, 1, QModelIndex());

ayanda83
23rd November 2016, 16:23
Is there a problem with using QAbstractItemView::setIndexWidget() because every thread I've come across people recommend using a QItemDelegate instead even when what you really need is a QAbstractItemView::setIndexWidget(). Am I missing something in my code above, why doesn't it work?

Added after 14 minutes:

Never mind, I figured it out. You just have to set the model to the view before the loop.