Vertical QLabel, or the equivalent?

前端 未结 5 2197
执念已碎
执念已碎 2021-01-04 03:53

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

5条回答
  •  时光说笑
    2021-01-04 04:41

    try using this:

    #ifndef VERTICALLABEL_H
    #define VERTICALLABEL_H
    
    #include 
    
    class VerticalLabel : public QLabel
    {
        Q_OBJECT
    
    public:
        explicit VerticalLabel(QWidget *parent=0);
        explicit VerticalLabel(const QString &text, QWidget *parent=0);
    
    protected:
        void paintEvent(QPaintEvent*);
        QSize sizeHint() const ;
        QSize minimumSizeHint() const;
    };
    
    #endif // VERTICALLABEL_H
    

    ///cpp

    #include "verticallabel.h"
    
    #include 
    
    VerticalLabel::VerticalLabel(QWidget *parent)
        : QLabel(parent)
    {
    
    }
    
    VerticalLabel::VerticalLabel(const QString &text, QWidget *parent)
    : QLabel(text, parent)
    {
    }
    
    void VerticalLabel::paintEvent(QPaintEvent*)
    {
        QPainter painter(this);
        painter.setPen(Qt::black);
        painter.setBrush(Qt::Dense1Pattern);
    
        painter.rotate(90);
    
        painter.drawText(0,0, text());
    }
    
    QSize VerticalLabel::minimumSizeHint() const
    {
        QSize s = QLabel::minimumSizeHint();
        return QSize(s.height(), s.width());
    }
    
    QSize VerticalLabel::sizeHint() const
    {
        QSize s = QLabel::sizeHint();
        return QSize(s.height(), s.width());
    }
    

提交回复
热议问题