2

私は非常にうまく機能する QTableView を持っています。最初の列にはいくつかのサムネイルが保持されています。この列の各セルでは、サムネイルは垂直方向の中央にありますが、水平方向の中央にはありません。

本当にデリゲートを使用する必要がありますか? はいの場合、QStyledItemDelegate を使用してそれらを水平方向に中央揃えにする方法は?

4

3 に答える 3

4

独自のデリゲートを構築し、QStyledItemDelegate を継承します。ペイント メソッドをオーバーライドします。

次に、次のようにします。

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

     QPixmap pixmap;
     pixmap.load("Your pixmap file path");
     pixmap = pixmap.scaled(option.rect.width(), option.rect.height(), Qt::KeepAspectRatio);

    // Position our pixmap
    const int x = option.rect.center().x() - pixmap.rect().width() / 2;
    const int y = option.rect.center().y() - pixmap.rect().height() / 2;

    if (option.state & QStyle::State_Selected) {
        painter->fillRect(option.rect, option.palette.highlight());         
    }

    painter->drawPixmap(QRect(x, y, pixmap.rect().width(), pixmap.rect().height()), pixmap);

}
于 2015-08-17T09:56:27.033 に答える