Results 1 to 4 of 4

Thread: Rotate QTextEdit Widget

  1. #1
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Rotate QTextEdit Widget

    Hello all,

    I am currently subclassing QTextEdit and dynamically creating what are (in effect) fancy labels that can be created and moved around the window, edited, etc. So far everything is working out beautifully.

    I would now like to add the ability to rotate these labels. Here is what I have tried:

    Each MyQTextEdit is created with the QMainWindow's centralwidget as parent. I have a QList of pointers to each of the MyQTextEdit objects. In the MyQTextEdit subclass, I have overridden the paintEvent, pass the event first to QTextEdit::paintEvent and then create a QPainter with MyQTextEdit::viewport() as its parent, call its rotate() and then continue on.

    This runs without error, but does not have the desired effect. I have also tried transform() without success. The MyQTextEdit objects are painted normally, no rotation occurs.

    To be clear, I wish to rotate the entire widget, not just the text in the widget.

    I feel like perhaps I am reaching for the wrong viewport() to rotate here. Yet QMainWindow does not have a viewport().

    Any ideas on how to accomplish this?
    Last edited by lxman; 6th February 2011 at 03:42. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Rotate QTextEdit Widget

    If you have a pointer to your labels, and you have overriden their paintEvent() that should be enough, if you use trasformation (totate) on in your paintEvent() of your labels.
    This runs without error, but does not have the desired effect.
    What is the effect you did get - and please post code for it.

    Another thing your might want to consider, is using a ready made such QLabel that allow rotation.
    You can find it here:
    libQxt
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Rotate QTextEdit Widget

    Thank you for taking the time to reply.

    I have been working out a trivial example to demonstrate what I am trying to accomplish. Here is what I have:

    The project contains a standard QMainWindow with only a centralWidget.

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "myqpushbutton.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. MyQPushButton *pb;
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. pb = new MyQPushButton(ui->centralWidget);
    10. pb->show();
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete pb;
    16. delete ui;
    17. }
    To copy to clipboard, switch view to plain text mode 

    myqpushbutton.h
    Qt Code:
    1. #ifndef MYQPUSHBUTTON_H
    2. #define MYQPUSHBUTTON_H
    3.  
    4. #include <QPushButton>
    5.  
    6. class MyQPushButton : public QPushButton
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MyQPushButton(QWidget *parent = 0);
    11.  
    12. private:
    13. void paintEvent(QPaintEvent *e);
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. };
    20.  
    21. #endif // MYQPUSHBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    myqpushbutton.cpp
    Qt Code:
    1. #include "myqpushbutton.h"
    2. #include <QPaintEvent>
    3. #include <QPainter>
    4.  
    5. MyQPushButton::MyQPushButton(QWidget *parent) :
    6. QPushButton(parent)
    7. {
    8. }
    9.  
    10. void MyQPushButton::paintEvent(QPaintEvent *e)
    11. {
    12. QPainter painter(this);
    13. painter.rotate(45);
    14. QPushButton::paintEvent(e);
    15. }
    To copy to clipboard, switch view to plain text mode 

    With this configuration, the button is painted on the form, but it is painted in a normal horizontal position. I presume this is due to the fact that I am calling the paintEvent of the inherited class QPushButton. If I remove the line
    bold Code:
    1. QPushButton::paintEvent(e);
    To copy to clipboard, switch view to plain text mode 
    however, nothing is drawn. I can call painter.drawText() and the text will be printed at the desired angle.

    I have looked through libQxt, and while an excellent project, they only provide rotation at 90, 180 and 270 degrees. I am attempting to attain an arbitrary angle.

    Once again, thank you for your time, and I would appreciate any further input.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Rotate QTextEdit Widget

    I have looked through libQxt, and while an excellent project, they only provide rotation at 90, 180 and 270 degrees.
    If all you want is to rotate text in a QLabel, all you have to do is resize the label to the bounding rect of the result string, rotate the painter, and do drawText() as you did.
    But if you want to have a complex widget such as a QPushButton drawn as it is usually drawn just rotated, this makes it more complicated.
    You will have to calculate the widgets bounding rect for the given angel, resize is, and then run the drawing code (which you basically have to copy from the original widget).

    Another approach might be to use QGraphicsViewer and proxy widgets - then rotating is trivial.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 2
    Last Post: 17th June 2010, 20:24
  2. How to rotate the control?
    By tszzp in forum Qt Programming
    Replies: 9
    Last Post: 25th November 2009, 09:05
  3. To rotate widget
    By ajit in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2007, 10:03
  4. Scrolling in QTextEdit Widget
    By ajb_advance in forum Newbie
    Replies: 2
    Last Post: 25th September 2007, 11:34
  5. QPolygon rotate
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2007, 11:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.