qt在tableview中绘制图片

void ItemModelDeletage::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                           const QModelIndex &index) const
{
    QStyleOptionViewItem viewOption(option);
    initStyleOption(&viewOption, index);
    if (option.state.testFlag(QStyle::State_HasFocus))
        viewOption.state = viewOption.state ^ QStyle::State_HasFocus;

ItemModelDeletage::paint(painter, viewOption, index);
    if(index.column() == AllowTradeTime::Operator)
    {
        QStyleOptionButton buttonStyle;
                buttonStyle.rect = option.rect.adjusted(0, 0, 0, 0);
                buttonStyle.text = "X";
                buttonStyle.state = QStyle::State_Enabled;

        QPushButton button;
        buttonStyle.initFrom(&button);
        button.setFlat(true);
        button.setStyleSheet("QPushButton{ border-image:url(res/skin/default/images/dockwidget/dock_close.png) no-repeat;}");
        //QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonStyle, painter);
        //painter->drawText(option.rect,Qt::AlignJustify | Qt::AlignVCenter | Qt::AlignHCenter,"X");
        QImage image;
        bool re= image.load("res\\skins\\default\\images\\dockwidget\\time_delete.png");
        int sx =-45;
        int sy = -10;
        int sw = 80;
        int sh = 35;
        painter->drawImage(option.rect.x(),option.rect.y(),image,sx, sy,sw,sh,Qt::AutoColor);
    }
}
posted @ 2018-07-03 15:53  beautifulday  阅读(1753)  评论(0编辑  收藏  举报