PDA

View Full Version : Rotate QTextEdit Widget



lxman
6th February 2011, 03:41
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?

high_flyer
7th February 2011, 08:43
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 (http://dev.libqxt.org/libqxt/wiki/Home)

lxman
10th February 2011, 11:50
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

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "myqpushbutton.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
MyQPushButton *pb;
};

#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pb = new MyQPushButton(ui->centralWidget);
pb->show();
}

MainWindow::~MainWindow()
{
delete pb;
delete ui;
}


myqpushbutton.h

#ifndef MYQPUSHBUTTON_H
#define MYQPUSHBUTTON_H

#include <QPushButton>

class MyQPushButton : public QPushButton
{
Q_OBJECT
public:
explicit MyQPushButton(QWidget *parent = 0);

private:
void paintEvent(QPaintEvent *e);

signals:

public slots:

};

#endif // MYQPUSHBUTTON_H


myqpushbutton.cpp

#include "myqpushbutton.h"
#include <QPaintEvent>
#include <QPainter>

MyQPushButton::MyQPushButton(QWidget *parent) :
QPushButton(parent)
{
}

void MyQPushButton::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.rotate(45);
QPushButton::paintEvent(e);
}


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 QPushButton::paintEvent(e); 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.

high_flyer
10th February 2011, 13:18
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.