[英]Read model data with custom QHeaderView
我想設置一個自定義QHeaderView來旋轉水平標題的文本。 我正在使用QStandarItemModel
目前我有這個
class QHeaderViewR : public QHeaderView
{
public:
QHeaderViewR():QHeaderView(Qt::Horizontal)
{}
void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
QPen pen(Qt::black);
painter->setPen(pen);
painter->translate(rect.width() * logicalIndex, (logicalIndex * -18) -18);
painter->rotate(90);
painter->drawText(rect,"header");
}
};
我不太了解翻譯的內容。 我只是反復嘗試,直到它與各列相符為止。 仍然它不能做到完美,並且沒有明顯的理由就剪切了文本。 我該怎么做才能使文本與列匹配而不被截斷?
另一件事是我不想只在每列上寫“ header”。 要查看的模型已為每一列分配了HorizontalHeaderItem,我想改為顯示這些標題
在此先感謝您的幫助
我解決了 剛剛添加了QStringList作為構造函數的參數,並使用邏輯索引對其進行了迭代。 這是最終結果
class QHeaderViewR : public QHeaderView
{
QStringList heads;
public:
QHeaderViewR(QStringList _heads):QHeaderView(Qt::Horizontal)
{
heads = _heads;
}
void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
QPen pen(Qt::black);
painter->setPen(pen);
painter->rotate(90);
painter->translate(0,-rect.width()+1);
QRect copy = rect;
copy.setWidth(rect.height());
copy.setHeight(rect.width());
copy.moveTo(0,-this->sectionPosition(logicalIndex));
if (logicalIndex == 0)
{
copy.setHeight(rect.width()-1);
}
painter->drawText(copy, " " + heads.at(logicalIndex));
painter->drawRect(copy);
}
};
由於QHeaderView只是一個視圖,因此您應該從模型中獲取要顯示的數據。
因此,類似於QHeaderView中的基本實現 :
QString text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();
要在模型上設置標題,請使用例如
QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.