1. 程式人生 > >qt在tableview中繪制圖片

qt在tableview中繪制圖片

tag images cat void ons def model widget png

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);
    }
}

qt在tableview中繪制圖片