1 Attachment(s)
Need help with QAbstractItemView::setIndexWidget()
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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QStandardItemModel>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
for (int i = 0; i <= 41; i++)
{
ui
->tableView
->setIndexWidget
(model
->index
(i,
2,
QModelIndex()), btn
);
}
ui->tableView->setModel(model);
}
Attachment 12230
Re: Need help with QAbstractItemView::setIndexWidget()
Have you tried creating the buttons without parent?
Cheers,
_
Re: Need help with QAbstractItemView::setIndexWidget()
Quote:
Originally Posted by
anda_skoa
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.
Re: Need help with QAbstractItemView::setIndexWidget()
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.