在QTableView单元格的中心绘制QPixmap

时间:2015-08-16 06:13:31

标签: qt qtableview qstyleditemdelegate

我有一个非常好的QTableView,第一列包含一些缩略图,在此列的每个单元格中,缩略图垂直居中,但不是水平居中。

我真的需要使用委托吗? 如果是,如何使用QStyledItemDelegate水平居中?

3 个答案:

答案 0 :(得分:2)

自己绘图是没有必要的,但是自定义委托 - 是。样式化的项目委托使用样式的控制元素绘图代码来绘制CE_ItemViewItem - 请参阅the source code for Qt 5.5.0。绘图代码考虑了样式选项的decorationAlignment成员。不幸的是,没有数据角色可以将这种对齐方式传递给样式的实现。相反,您必须覆盖委托中的对齐方式:

class DecorationAligningDelegate : public QStyledItemDelegate {
  Q_OBJECT
  Qt::Alignment const m_alignment;
public:
  explicit DecorationAligningDelegate(Qt::Alignment alignment, QObject * parent = 0) :
    QStyledItemDelegate(parent), m_alignment(alignment) {}
  Qt::Alignment alignment() const { return m_alignment; }
  void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
    auto opt = option;
    opt.decorationAlignment = m_alignment;
    QStyledItemDelegate::paint(painter, opt, index);
  }
};

然后,缩略图居中:

view.setItemDelegateForColumn(0, 
  new DecorationAligningDelegate(Qt::AlignHCenter, &view));
//or
view->setItemDelegateForColumn(0, 
  new DecorationAligningDelegate(Qt::AlignHCenter, view));

如果您真的希望自己绘制它,即使它不必要,要绘制的项目的矩形也会在样式选项(option.rect)中给出。要在项目的矩形中心绘制像素图,您可以执行以下操作:

QStyleOption option;
QPixmap pix;
QPainter painter;
...
painter.save();
auto loc = option.rect.center() - pix.rect().center()
painter.drawPixmap(loc, pix);
painter.restore();

答案 1 :(得分:2)

构建您自己的委托并继承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);

}

答案 2 :(得分:1)

我将保留我的观点,即文学是两个答案的结合。

class DecorationAligningDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    explicit DecorationAligningDelegate(Qt::Alignment alignment, QObject *parent = nullptr)
    : QStyledItemDelegate(parent), m_alignment(alignment) {}

    Qt::Alignment alignment() const { return m_alignment; }

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QIcon icon = QIcon(qvariant_cast<QIcon>(index.data(Qt::DecorationRole)));

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

        icon.paint(painter, option.rect, m_alignment);
    }

private:
    Q_DISABLE_COPY(VDecorationAligningDelegate)
    Qt::Alignment const m_alignment;
};

我假设您是这样定义项目的:

auto *item = new QTableWidgetItem();
item->setIcon(QIcon("Your pixmap file path"));

不要忘记设置代表。

相关问题