[Qt-interest] Render into QGraphicsTextItem, vertically centered.

John Clayton johnclayton72 at gmail.com
Tue Dec 15 23:30:30 CET 2009


Hello,

Is anyone able to inform me as to how I render vertically centered  
text into a QGraphicsTextItem?

I have a properly rendered text document in the graphics view now -  
its just that I can't seem to get it vertically centered.  I thought  
this might work, but it doesn't seem to have any effect:

to center vertically, I do this:

void TextGenerator::textAlignVCenter() {
	QTextCursor currCursor = textCursor();
	QTextCharFormat format;
	format.setProperty(QTextFormat::TextVerticalAlignment,  
Qt::AlignVCenter);
	currCursor.mergeCharFormat(format);
	setTextCursor(currCursor);
}


to draw, I do this:
void TextGenerator::paintContent(QPainter *painter, const  
QStyleOptionGraphicsItem *option, QWidget *widget) {
	// force the widget to do its thing, bypassing immediate super-class
	painter->save();
	painter->setRenderHints(QPainter::TextAntialiasing, true);
	
	// unset selected state to remove the dotted rect
	QStyleOptionGraphicsItem styleOption(*option);
	styleOption.state = QStyle::State_None;
	supersuper::paint(painter, &styleOption, widget);
	
	painter->restore();
}

any tips appreciated.

Thanks.



More information about the Qt-interest-old mailing list