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

Dynamically rotate QTextEdit



  • I've got idea. When QTextEdit contains position of mouse i can rotate it by moving of mouse. I tried to rotate only text and resize QTextEdit but there are some problems. ```

    setFixedSize(1000,1000);
    QRect idealRect(0,0,document()->idealWidth(), fontMetrics().height()*toPlainText().split("\n").size());
    
    QTransform t;
    t.translate(idealRect.width()/2, idealRect.height()/2).rotate(-45).translate(-idealRect.width()/2, -idealRect.height()/2);
    
    idealRect = t.mapRect(idealRect);
    
    
    setFixedSize(idealRect.width(),idealRect.height());
    painter->begin(viewport());
    painter->setTransform(t);
    painter->drawText(0,idealRect.height()/2, toPlainText());
    painter->end();
    

    ``` ``

    Often shape larger then it should be or not all text inside QTextEdit. I wrote class TextEdit which inherits QTextEdit and overrided paintEvent. Maybe i must use another class to make rotation of widget&


  • Lifetime Qt Champion

    Hi and welcome to the forums.
    It would help if you first told us what the goal is.

    You want to draw some rotated text ?

    Or you want to make a widget that can rotate text with the mouse, like in a drawing program?

    And where will you use this text or widget ?



  • Thank you. I want to make opportunity to rotate text. If i could rotate widget i would like to rotate widget else rotate text. I do not want to rotate text by widget i want to rotate widget which contains string (QTextEdit). Can i do it?


  • Lifetime Qt Champion

    @Kaznachei
    you mean so its still possible to type text into it ?
    While rotated ?



  • Yes. I can print something if it 82 degrees for example


  • Lifetime Qt Champion

    @Kaznachei

    Hi
    Rotating the actual TextEdit is hard to do, but drawing the text rotated is possible.
    I found using a QPainterPath, gave the best results. (at least on windows)

     virtual void paintEvent(QPaintEvent *event) override
        {
            QPainter painter(this);
            painter.setRenderHint( QPainter::Antialiasing, true );
            painter.drawRect(rect());
            QFont font;
    
            font.setPointSize( 12 );
            font.setStyleStrategy( QFont::StyleStrategy::PreferAntialias );
            painter.setFont( font );
    
            QRect m_window = QRect(- width() / 2, - height() / 2, width(), height());
            painter.setWindow( m_window );
            QRect m_viewport = QRect(0, 0, width(), height());
            painter.setViewport( m_viewport );
            // rotate
            painter.rotate( 80 );
            //draw text
            QPainterPath glyphPath;
            glyphPath.addText( 0, 0, painter.font(), "TEXT" );
            painter.fillPath( glyphPath, painter.pen().color() );
    
        }
    

    alt text


Log in to reply