PDA

View Full Version : Making Table cell as a combobox?



kaushal_gaurav
1st August 2008, 07:37
Hi,

I was facing an issue in designing one screen where in I need to display a table and each table cell is a combo and the values in them need to be populated from a Modal view.
Approach that I had followed is … Creating a QTableView widget and displaying values from a Modal.
But I am struck – ‘how to make each cell of table as a comb box and the populate each combo with a values from Modal’?

help me out.

Ginsengelf
1st August 2008, 09:13
Hi,
you need to use a custom delegate. In its createEditor()-method, return a new QCombobox.
Possibly you will need persistend editors in your table, otherwise the combobox will only show up if the users edits the cell.

Ginsengelf

kaushal_gaurav
1st August 2008, 09:18
i have written my custom delegate but it just works wwith QTableWidget and not with
QTableView. I need to use QTableView because i want to show data from my Modal.

Ginsengelf
1st August 2008, 09:51
That's weird since QTableWidget inherits QTableView...
Could you show some code?

kaushal_gaurav
1st August 2008, 10:44
That is ok. Got the combobox in table cell using delegate.

But there is a problem when i use my custom model. My custom model is derived from
QAbstract item Model...
When i use this model then only the data is displayed but no combo box is there...
but when i use QStandardItemModel there is combobox as well as data.

help!

jpn
1st August 2008, 10:48
How did you implement flags() and data()? Did you return Qt::ItemIsEditable together with other flags in former and did you handle Qt::EditRole in latter?

kaushal_gaurav
1st August 2008, 11:21
I have implemented data as


QVariant className::data(const QModelIndex & index, int role) const
{
if( index.isValid() == false )
return QVariant();

if( role == Qt::EditRole )
{
if( index.internalPointer() )
{
int parentRow = ipToRow(index.internalPointer());
return QString("Cell %3.%1.%2").arg(index.row()).arg(index.column())
.arg(parentRow);
}

return QString("Cell %1.%2").arg(index.row()).arg(index.column());
}


and Flags() as


Qt::ItemFlags className::flags( const QModelIndex& idx ) const
{

return Qt::ItemIsEditable;
}

but it did not work.

did i do something wrong...?

jpn
1st August 2008, 12:29
Qt::ItemFlags className::flags( const QModelIndex& idx ) const
{

return Qt::ItemIsEditable;
}


This would mean that all items are disabled, thus non-editable. Try something like:


Qt::ItemFlags className::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;

return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsE ditable;
}

kaushal_gaurav
1st August 2008, 12:32
yipeee....it works...
thanks buddy..

but how can make it persistent....i have to click the table cell to make it show.

Ginsengelf
1st August 2008, 13:04
Hi,
use QAbstractItemView::openPersistentEditor.

Ginsengelf