gpt4 book ai didi

c++ - Qt 设置小部件的文本方向

转载 作者:行者123 更新时间:2023-11-30 03:22:37 26 4
gpt4 key购买 nike

enter image description here

我想更改 tablewidget 垂直标题的文本方向。我打算将其从默认的 horizo​​ntal 更改为 vertical 。从下到上。

最佳答案

在表格标题中显示垂直文本

不幸的是,QHeaderView does not support custom delegates .但是,我们可以子类化 QHeaderView 并覆盖 paintSectionsectionSizeFromContents并在那里垂直绘制文本。请注意,我们可以重用在渲染时考虑各种因素的相同基础实现(例如,如果鼠标指针位于标题上方,如果用户正在单击标题,......)并免费获得所有这些注意事项。下面是我将如何实现这样一个类:

//a header view that renders text vertically
class VerticalHeaderView : public QHeaderView {
public:
explicit VerticalHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
: QHeaderView(orientation, parent) {}

void paintSection(QPainter *painter, const QRect &rect,
int logicalIndex) const override {
QPointF rectCenter = QRectF(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
QRect rectCopy = painter->worldTransform().mapRect(rect);
//use base paintSection method after applying required transformations
QHeaderView::paintSection(painter, rectCopy, logicalIndex);
painter->restore();
}

QSize sectionSizeFromContents(int logicalIndex) const override {
//get sizeHint from base sizeHint method
QSize val = QHeaderView::sectionSizeFromContents(logicalIndex);
//swap height and width
return QSize(val.height(), val.width());
}
};

要使用上面的类,我们需要使用 setVerticalHeader() 在我们的 QTableView/QTableWidget 上设置它(它也可以用作表格的水平标题,但在大多数情况下这没有意义)。这是一个完整的示例,显示了上述类的实际操作:

screenshot

#include <QtWidgets>

//a header view that renders text vertically
class VerticalHeaderView : public QHeaderView {
public:
explicit VerticalHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
: QHeaderView(orientation, parent) {}

void paintSection(QPainter *painter, const QRect &rect,
int logicalIndex) const override {
QPointF rectCenter = QRectF(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
QRect rectCopy = painter->worldTransform().mapRect(rect);
//use base paintSection method after applying required transformations
QHeaderView::paintSection(painter, rectCopy, logicalIndex);
painter->restore();
}

QSize sectionSizeFromContents(int logicalIndex) const override {
//get sizeHint from base sizeHint method
QSize val = QHeaderView::sectionSizeFromContents(logicalIndex);
//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, 1);
for(int i=0; i<5; i++) {
model.appendRow(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.setHeaderData(i, Qt::Vertical,
QDate::currentDate().toString("dd-MM-yyyy"),
Qt::DisplayRole);
model.setHeaderData(i, Qt::Vertical, Qt::AlignCenter, Qt::TextAlignmentRole);
}

//setup view
QTableView view;
VerticalHeaderView headerView(Qt::Vertical);
view.setVerticalHeader(&headerView); //set our custom headerview
view.verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
view.setModel(&model);
view.show();

return a.exec();
}

在表格单元格中显示垂直文本*

你可以写一个delegate class垂直呈现文本。您需要覆盖 paintsizeHint并在那里提供您的代码。也可以在应用所需的转换后重用相同的基础实现。下面是我将如何实现这样一个类:

//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());
}
};

这是一个使用上述类的完整示例:

screenshot

#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();
}

显然,我在第一次回答问题时误解了这个问题,但我将其作为一个单独的部分保留下来,因为它可能对 future 的读者有用。

关于c++ - Qt 设置小部件的文本方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50994577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com