Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

rotate a Qlabel



  • Hello guys i'm trying to rotate a text on my Qlabel and nothing it works all methods i found on the internet didn't works so i used QGraphicsScene . this is one of the methode i found on the interenet and that worked for someone but perhaps i'm using it in a wrong way, even if i don't know how am I using it wrongly

    QLabel* m_memoryCapacity = new QLabel("");
        QVBoxLayout* vboxAlignement  = new QVBoxLayout();
        vboxAlignement->addWidget(m_memoryCapacity);
        vboxAlignement->addStretch(1);
    .....
    
    m_memoryCapacity->setText(o_l_memoryValue); //o_l_memoryValue is my label
     
    QGraphicsScene scene;
    QGraphicsProxyWidget  * proxy = scene.addWidget(o_l_memoryValue);
    proxy->setRotation(-45);
    QGraphicsView view(&scene);
    view.show();  
    

    it gave me this error QGraphicsProxyWidget::setWidget: cannot embed widget 0x3ccf8f0 which is not a toplevel widget, and is not a child of an embedded widget


  • Qt Champions 2019

    @benamiar Did you set parent for o_l_memoryValue? If so try without parent.



  • @benamiar said in rotate a Qlabel:

    m_memoryCapacity

    i tried but still not working with that methode so i used the draw event methode so redraw my Label. so now it's working but not with QGraphicScene

    Q2VerticalLabel::Q2VerticalLabel(QString text)
        {
        setFixedSize(80,250);
        setText(text);
    
        }
    
    void Q2VerticalLabel::setVerticalLabelFont(QFont font)
        {
        setFont(font);
        }
    
    void Q2VerticalLabel::setVerticalLabelText(QString text)
        {
        setText(text);
        }
    
    void Q2VerticalLabel::paintEvent(QPaintEvent* event) {
    
        QStylePainter painter(this);
    
        painter.translate(rect().center());
        painter.rotate(90);
        painter.translate(-rect().center());
    
        painter.drawText(0, 0, text());
    }
    

  • Lifetime Qt Champion

    Hi,

    Why are you using a QLabel rather than a QGraphicsTextItem ?


Log in to reply