PDA

View Full Version : QItemDelegate use in QTableWidget



arpspatel
26th October 2009, 23:45
Hi,

delegate class


QCLDelegate::QCLDelegate(QObject *parent)
: QItemDelegate(parent)
{
}

QWidget *QCLDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & /* option */,
const QModelIndex &index) const
{
QComboBox *comboBox = new QComboBox(parent);
if (index.column() == 2) {
for(int i = 1970; i < 2014 ; i++){
QVariant v(i);
comboBox->addItem(v.toString());
}
} else if (index.column() == 3) {
comboBox->addItem(tr("BRRip"));
comboBox->addItem(tr("DVDRip"));
comboBox->addItem(tr("Blu-Ray"));
comboBox->addItem(tr("Other"));
} else if (index.column() == 4) {
comboBox->addItem(tr("Yes"));
comboBox->addItem(tr("No"));
}
connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData()));

return comboBox;
}


and here is how i add to the table widget



bool QCLMovies::parseElements(QDomNode &c, int row)
{
while(!c.isNull()){
QDomElement k = c.toElement();
if(k.text().isEmpty())
return false;
if(labels.indexOf(k.tagName()) == -1)
return false;
//Adding to table widget
setItem(row, labels.indexOf(k.tagName()), new QTableWidgetItem(k.text()));
c = c.nextSibling();
}

return true;
}


the class declaration is QCLMovies: public QTableWidget

I am trying to read an XML file into the QTableWidget, with columns 2,3 and 4 as combo boxed and columns 0 and 1 as Qtablewidgetitem (QString), but when i start the application it shows all combo boxes..

Please help me out

Thanks

spirit
27th October 2009, 08:22
in QCLDelegate::createEditor you should write additional condition


QWidget *QCLDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & /* option */,
const QModelIndex &index) const
{
if (index.column() == 2 || index.column() == 3 || index.column() == 4) {
//creating and customization of comboboxes
return comboBox;
}

return new QLineEdit(parent);
}

you also should be careful in other Delegate's method where an editor is used as method's parameter, you should cast each editor to concert type, because now you have two different editor's type -- QComboBox and QLineEdit.

arpspatel
27th October 2009, 23:18
Thanks it worked perfectly....