首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Qt设置小部件的文本方向

Qt设置小部件的文本方向
EN

Stack Overflow用户
提问于 2018-07-16 06:57:56
回答 1查看 0关注 0票数 0

我想改变表格小部件垂直标题的文本方向。我打算将它从默认的水平更改为垂直。从下到上。

EN

回答 1

Stack Overflow用户

发布于 2018-07-16 16:40:55

你可以写一个委托类垂直呈现文本的。你需要覆盖paintsizeHint并在那里提供你的代码。注意,在应用所需的转换(即不需要重写绘图逻辑)之后,可以重用相同的基本实现。下面是我如何实现这样一个类:

代码语言:txt
复制
//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());
    }
};

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

代码语言:txt
复制
#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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档