我想改变表格小部件垂直标题的文本方向。我打算将它从默认的水平更改为垂直。从下到上。
发布于 2018-07-16 16:40:55
你可以写一个委托类垂直呈现文本的。你需要覆盖paint
和sizeHint
并在那里提供你的代码。注意,在应用所需的转换(即不需要重写绘图逻辑)之后,可以重用相同的基本实现。下面是我如何实现这样一个类:
//a delegate that renders text vertically
class VerticalTextDelegate : public QStyledItemDelegate {
public:
explicit VerticalTextDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent){}
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override {
QStyleOptionViewItem optionCopy = option;
QPointF rectCenter = QRectF(option.rect).center();
painter->save();
//rotate around rectCenter
painter->translate(rectCenter.x(), rectCenter.y());
painter->rotate(-90.0);
painter->translate(-rectCenter.x(), -rectCenter.y());
//apply the same transformation on the rect
optionCopy.rect = painter->worldTransform().mapRect(option.rect);
//use base paint method after applying required transformations
QStyledItemDelegate::paint(painter, optionCopy, index);
painter->restore();
}
virtual QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override {
//get sizeHint from base sizeHint method
QSize val = QStyledItemDelegate::sizeHint(option, index);
//swap height and width
return QSize(val.height(), val.width());
}
};
下面是一个使用上述类的完整示例:
#include <QtWidgets>
//a delegate that renders text vertically
class VerticalTextDelegate : public QStyledItemDelegate {
public:
explicit VerticalTextDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent){}
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override {
QStyleOptionViewItem optionCopy = option;
QPointF rectCenter = QRectF(option.rect).center();
painter->save();
//rotate around rectCenter
painter->translate(rectCenter.x(), rectCenter.y());
painter->rotate(-90.0);
painter->translate(-rectCenter.x(), -rectCenter.y());
//apply the same transformation on the rect
optionCopy.rect = painter->worldTransform().mapRect(option.rect);
//use base paint method after applying required transformations
QStyledItemDelegate::paint(painter, optionCopy, index);
painter->restore();
}
virtual QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override {
//get sizeHint from base sizeHint method
QSize val = QStyledItemDelegate::sizeHint(option, index);
//swap height and width
return QSize(val.height(), val.width());
}
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
//setup model with dummy data
QStandardItemModel model(0, 2);
for(int i=0; i<5; i++) {
model.appendRow({new QStandardItem(QDate::currentDate().toString("dd-MM-yyyy")),
new QStandardItem(QStringLiteral("Lorem ipsum dolor sit "
"amet, consectetur adipiscing elit, sed do eiusmod tempor "
"incididunt ut labore et dolore magna aliqua. Ut enim ad "
"minim veniam, quis nostrud exercitation ullamco laboris "
"nisi ut aliquip ex ea commodo consequat."))});
model.setData(model.index(i, 0), Qt::AlignCenter, Qt::TextAlignmentRole);
}
//setup view and delegate
QTableView view;
VerticalTextDelegate delegate;
view.verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
view.setItemDelegateForColumn(0, &delegate);
view.setModel(&model);
view.show();
return a.exec();
}
https://stackoverflow.com/questions/-100005583
复制相似问题