[Qt-interest] How to set a custom widget to fill a QListItem rect

yanqzhi at gmail.com yanqzhi at gmail.com
Thu Mar 18 02:56:18 CET 2010


I have a subclass of QItemDelegate. In the function paint() I want to fill option.rect with custom widget
but the posiont of the cutom widget is incorrect.

code as bellow:

main()
{
 QApplication app(argc, argv);
 QTableWidget *tableWidget = new QTableWidget(4, 1);
 tableWidget->setItemDelegate(new SpinBoxDelegate);
 for (int i=0; i<3; i++)
 {
  QTableWidgetItem *item = new QTableWidgetItem();
  CustomWidget *widget = new CustomWidget(/*tableWidget*/);
  item->setData(i, qVariantFromValue(widget) );
  tableWidget->setItem(i, 0, item);
 }
 tableWidget->show();
    return app.exec();
}

void CutomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
       const QModelIndex &index) const
{
 if (qVariantCanConvert<CustomWidget *>(index.data())) 
{
  CustomWidget * widget = qVariantValue<CustomWidget *>(index.data());
  widget->setGeometry(option.rect);
  qDebug()<<option.rect.left() << option.rect.top() <<option.rect.right() << option.rect.bottom();
 } else 
{
  QItemDelegate::paint(painter, option, index);
 }
}


In main() if I set tableWidget as customWidget's parentWidget ,the custom widget can be seen;
If no, the custom widget can't be seen;

How can I map the option.rect to tableWidget in the function paint()?

Thanks. 

yanqzhi at gmail.com 
2010-03-18 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100317/f6c5f5c9/attachment.html 


More information about the Qt-interest-old mailing list