在下面的代码中,paintSection
内部的qDebug()
被调用,但是,QPixmap
没有被绘制到标题列中。
可重现示例:
class HeaderView : public QHeaderView
{
Q_OBJECT
public:
HeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
: QHeaderView(orientation, parent)
{
}
QPixmap pixmap = QPixmap(":/files/icon.png");
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override
{
switch (logicalIndex)
{
case 0:
{
painter->drawPixmap(rect.x(), rect.y(), rect.width(), rect.height()
, pixmap);
//painter->fillRect(rect, QColor(255, 255, 255));
qDebug() << rect;
break;
}
default:
break;
}
QHeaderView::paintSection(painter, rect, logicalIndex);
}
};
class TreeView : public QTreeView
{
Q_OBJECT
public:
QStandardItemModel model;
HeaderView* headerView;
TreeView(QWidget* parent = 0) : QTreeView(parent)
{
setModel(&model);
setIndentation(0);
setUniformRowHeights(true);
setRootIsDecorated(false);
setSortingEnabled(true);
headerView = new HeaderView(Qt::Horizontal);
setHeader(headerView);
model.setHorizontalHeaderItem(0, new QStandardItem("column0"));
model.setHorizontalHeaderItem(1, new QStandardItem("column1"));
model.setHorizontalHeaderItem(2, new QStandardItem("column2"));
header()->setDefaultAlignment(Qt::AlignCenter);
header()->setMinimumSectionSize(100);
}
};
#include "treeview.h"
Application::Application(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
TreeView* treeView = new TreeView(this);
}
1条答案
按热度按时间d8tt03nd1#
在搜索时,我发现需要保存/恢复画家才能使其工作: