0

These days I encounter a problem about qt. There is a batch data which I want to show in my qt application with QTableview(not qml gridview, since my application use widgets). Each data item include a image and a image name. I need to let the image displayed on a qlabel and it's name displayed on another qlabel. These two widgets should be showed on my self-defined frame. And then I will let the user-defined frame displayed in each QTableView's cell.

From BigBourin(who also ask the similar question), I know i should implement a self-defined delegate, and reimplement the painter function, just like this:

void PackageListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{

// here we have active painter provided by caller

// by the way - we can't use painter->save() and painter->restore()
// methods cause we have to call painter->end() method before painting
// the QWidget, and painter->end() method deletes
// the saved parameters of painter

// we have to save paint device of the provided painter to restore the painter
// after drawing QWidget
QPaintDevice* original_pdev_ptr = painter->device();

// example of simple drawing (selection) before widget
if (option.state & QStyle::State_Selected)
    painter->fillRect(option.rect, option.palette.highlight());

// creating local QWidget (that's why i think it should be fasted, cause we 
// don't touch the heap and don't deal with a QWidget except painting)
PackageListItemWidget item_widget;

// Setting some parameters for widget for example
    // spec. params
item_widget.SetPackageName(index.data(Qt::DisplayRole).toString());     
    // geometry
item_widget.setGeometry(option.rect);

// here we have to finish the painting of provided painter, cause
//     1) QWidget::render(QPainter *,...) doesn't work with provided external painter 
//          and we have to use QWidget::render(QPaintDevice *,...)
//          which creates its own painter
//     2) two painters can't work with the same QPaintDevice at the same time
painter->end(); 

// rendering of QWidget itself
item_widget.render(painter->device(), QPoint(option.rect.x(), option.rect.y()), QRegion(0, 0, option.rect.width(), option.rect.height()), QWidget::RenderFlag::DrawChildren);   

// starting (in fact just continuing) painting with external painter, provided
// by caller
painter->begin(original_pdev_ptr);  

// example of simple painting after widget
painter->drawEllipse(0,0, 10,10);   
};

my question is item_widget.SetPackageName(index.data(Qt::DisplayRole).toString());

the data come from where? can the data be user-defined data class?

  • through QItemDelegate we can only display the widget during editing the cell.. Is there any way to display it permanently ? – user2919227 Mar 23 '16 at 13:53
  • Thanks for your advice, code and words is better than just words. – user2919227 Mar 24 '16 at 05:39
  • This does not look like a minimal example, please see: http://stackoverflow.com/help/mcve. Code and words is better than just words is better than wall of code that nobody reads... – Mailerdaimon Mar 24 '16 at 08:19
  • Ok, thank you very much. I will keep your advice in mind and try not to make such mistake later. – user2919227 Mar 24 '16 at 13:57

0 Answers0