本文介绍了垂直QLabel或等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个绘图的轴标签,当然y轴标签应该垂直。我确定 QwtPlot 这样做,但我想保持轻松,所以我只是使用一个简单的 QWidget + QPainter 。我没有看到在文档中更改 QLabel 方向的任何方式。一些解决方案在中给出,但我想要东西似乎不像这样的黑客。我现在使用 Qt 4.8 ,除了 QPainter :: drawText()

I want axis labels for a plot I'm making, and naturally the y-axis label should be oriented vertically. I'm pretty sure QwtPlot does this, but I'm trying to keep things light so I'm just using a simple QWidget + QPainter for now. I didn't see any way to change QLabel orientation in the documentation. Some solutions are given in this 2002 thread but I'd like something that doesn't seem like such a hack. I'm using Qt 4.8 now, is there really no way to do this aside from QPainter::drawText()?

推荐答案

确实我放弃了找到任何简单的方法来实现这一点,并查看Uwe Rathmann的Qwt代码确实他在其 QwtPainter :: drawText 包装器函数中的QPainter :: drawText(),以及 QTransform 实现旋转,在 QwtScaleDraw :: labelTransformation 中。所以我只是把这些在一起如下:

So indeed I gave up on finding any simple way to achieve this, and looking over Uwe Rathmann's Qwt code indeed he uses QPainter::drawText() in his QwtPainter::drawText wrapper function, and a QTransform to achieve the rotation, in QwtScaleDraw::labelTransformation. So I've just put these together as follows:

void LabelWidget::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setPen(Qt::black);
    //... Need an appropriate call to painter.translate() for this to work properly
    painter.rotate(90);
    painter.drawText(QPoint(0,0), _text);
}

不需要QPixmap,事实证明。

Didn't need a QPixmap, it turns out.

这篇关于垂直QLabel或等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:15