add custom widget to QTableWidget cell
Asked Answered
O

2

5

I have custom widget made with qt designer and i want to add it to QTableWidget cell. But it doesn't work.

Here is the code :

int nRows =10;
for(int row = 0; row < nRows;row++;)

{
    QTableWidgetItem* item = new QTableWidgetItem();
    CustomWdg* wdg=new CustomWdg( );

    mTableWdg->insertRow( row );
    mTableWdg->setItem(row, 0, item);
    mTableWdg->setCellWidget( row, 0, wdg );

}  
Otilia answered 25/4, 2012 at 14:16 Comment(4)
The given code looks correct, I would suspect something is wrong with the custom widget itself.Miffy
The custom widget is standard widget made in qt designer, it has two buttons and two labels. I inherits from QWidget. I don't know what could be wrong with that.Otilia
If you use the widget in another context, is it visible and usable?Miffy
What does "it doesn't work" mean ? It doesn't compile ? It crashes ?Pusillanimity
B
9

If you want to add custom widget into table cell you can use QItemDelegate.

Create your own Delegate class and inherit it from QItemDelegate.

class MyDelegate : public QItemDelegate
{
    public:
    CChoicePathDelegate (QObject *parent = 0);
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; //delegate editor (your custom widget)
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const; //transfer editor data to model
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
    const QModelIndex &index) const;
};

And then set delegate for Table with this methods on your own.

setItemDelegate(QAbstractItemDelegate *)
setItemDelegateForColumn(int, QAbstractItemDelegate *)
setItemDelegateForRow(int, QAbstractItemDelegate *)

I have tried this code:

#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QHBoxLayout *l = new QHBoxLayout();
    l->addWidget((new QPushButton("I`m in cell")));
    l->addWidget((new QLabel("Test label")));

    QWidget *w = new QWidget();

    w->setLayout(l);

    ui->tableWidget->setCellWidget(1,1, w);
}

Widget::~Widget()
{
    delete ui;
}

and result is:

Result

Bondswoman answered 15/2, 2016 at 20:9 Comment(0)
M
8

Your code is correct, so the only thing that comes to my mind is that you didn't setColumnCount(1) before for loop. If that's not the case, you could try to set row and column count before that for loop instead inserting row by row in loop:

int nRows =10;
mTableWdg->setRowCount(nRows);
mTableWdg->setColumnCount(1);
for(int row = 0; row < nRows;row++;)

{
    //QTableWidgetItem* item = new QTableWidgetItem();// line one
    CustomWdg* wdg=new CustomWdg( );
    //mTableWdg->setItem(row, 0, item);// line three
    mTableWdg->setCellWidget( row, 0, wdg );

}  

If you really need item ("line one" and "line three") you should set it like this: QTableWidgetItem* item = new QTableWidgetItem("");, otherwise you don't need those lines, your CustomWdg is properly set with setCellWidget

Merriemerrielle answered 13/9, 2013 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.