PDA

View Full Version : Vertical Orientation of Text.



ashukla
28th January 2008, 11:58
#include <QtGui>

class W : public QWidget {
public:
W() : QWidget(){}

protected:
void paintEvent(QPaintEvent *e){

QTextLayout textLayout( "Hello How are you!");
QFontMetrics fontMetrics(QApplication::font());
int leading = fontMetrics.leading();
int height = 0;
qreal widthUsed = 0;
textLayout.beginLayout();
while (1) {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;

line.setLineWidth(9);
//line.setNumColumns (1);
height += leading;
line.setPosition(QPoint(0, height));
height += line.height();
widthUsed = qMax(widthUsed, line.naturalTextWidth());
}
textLayout.endLayout();
QWidget::paintEvent(e);
QPainter p(window());
QPen pe = p.pen();
pe.setWidth(4);
pe.setColor(Qt::red);
p.setPen(pe);
textLayout.draw(&p, QPoint(0, 0));
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
W w;
w.show();
w.resize(300,200);
return app.exec();
}
I am getting the output as attachments;
but I wants as


H
e
l
l
o

H
o
w

a
r
e

y
o
u
!
What should I do in code for this;

wysota
28th January 2008, 12:07
The easiest way is to add a newline after every character.

ashukla
28th January 2008, 12:16
The easiest way is to add a newline after every character.
But it is not a way of vertical orientation of text.
I am facing the same problem in the below thread specially for white space character.
Space problem in QTextEdit (http://www.qtcentre.org/forum/f-qt-programming-2/t-space-problem-in-qtextedit-11204.html)

wysota
28th January 2008, 12:22
If you want to use the text layout object, then at least set the line width before creating lines. You can use QFontMetrics to check how wide the line should be to incorporate one character of the current font.

ashukla
28th January 2008, 12:48
If you want to use the text layout object, then at least set the line width before creating lines How a way I set the line before creating the text layout object.
Please suggest me a function for that or give me a example.

wysota
28th January 2008, 13:09
QTextLayout::setLineWidth()

ashukla
28th January 2008, 13:12
QTextLayout::setLineWidth()
Probably, this is function of QTextLine. How a way I use in my code?

wysota
28th January 2008, 13:17
Please take a look at the example in QTextLayout docs.

ashukla
28th January 2008, 13:26
Please take a look at the example in QTextLayout docs.
I have seen already and by the use of it I developed this above code. But QTextLayout::setLineWidth() I am not getting this function to set the text layout width previously before the line create.

wysota
28th January 2008, 13:40
Set it after you create the line but before you create another... The point is to force a single character on each line. Or consider not using the text layout at all and simply drawing each character yourself using QPainter::drawText.