PDA

View Full Version : QTextEdit align right.



bunjee
27th October 2007, 17:48
Hello,

I'm trying to achieve this in a QTextEdit.

http://bunjeee.free.fr/QTextEdit.png

Any idea ?

momesana
27th October 2007, 20:52
http://doc.trolltech.com/4.3/qtextedit.html#setAlignment

Compile and test this as an example:


#include <QApplication>
#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTextEdit edit;
edit.setText("blablabla");
edit.setAlignment(Qt::AlignRight);
edit.show();
return app.exec();
}

jpn
28th October 2007, 12:48
Actually, there's a slight difference:


______________
| saddsadsa|
| asdds|
| dfds|
|_____________|

vs.


______________
| saddsadsa|
| asdds |
| dfds |
|_____________|

You can do the latter with the help of tables:


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextEdit edit;
QTextTableFormat format;
format.setAlignment(Qt::AlignRight);
QTextFrame* frame = edit.textCursor().insertTable(1, 1, format);
QTextCursor cursor(frame);
cursor.insertText("saddsadsa\nasdds\ndfds");
edit.show();
return a.exec();
}

wysota
28th October 2007, 14:08
Or use a horizontal layout with a spacer that will push your text edit widget to the right.

bunjee
29th October 2007, 17:35
Man, that was tough.

I found absolutely no way to do it with a layout:

I needed a stretchable QParagraph with height for width justified on the right.
I had to reimplement my own widget to place it manually.

The layout wouldn't let me put a stretchable area that let the paragraph take as much space as it can.

Here is the result:

http://bunjeee.free.fr/alignedParagraph.png

Ask me if you need some code.

Regards.

wysota
29th October 2007, 19:48
But what exactly is your goal?

ambang_10111
7th November 2009, 21:43
Hey Bunjee, i will need the code for QTextEdit align right:)