#include class MyTextEdit : public QTextEdit { Q_OBJECT public: MyTextEdit() : QTextEdit() { QTextCursor tc= textCursor(); QTextCharFormat f = tc.charFormat(); f.setVerticalAlignment(QTextCharFormat::AlignMiddle); tc.setCharFormat(f); tc.insertText("hoge"); setTextCursor(tc); qDebug() << "textcarformat"; } protected: void resizeEvent(QResizeEvent * event) { valignMiddle(); QTextEdit::resizeEvent(event); } void showEvent(QShowEvent * event) { valignMiddle(); QTextEdit::showEvent(event); } private slots: void valignMiddle() { int nDocHeight = (int)document()->size().height(); if(nDocHeight == 0) return; int nCtrlHeight= height(); QTextFrame * pFrame = document()->rootFrame(); QTextFrameFormat frameFmt = pFrame->frameFormat(); int nTopMargin = (int)frameFmt.topMargin(); int nBBDocH = nDocHeight - nTopMargin; // compute and set appropriate frame top margin if(nCtrlHeight <= nBBDocH) nTopMargin = 2; else nTopMargin = (nCtrlHeight - nBBDocH)/2 - 2; frameFmt.setTopMargin(nTopMargin); // setTopMargin() to valign. frameFmt.setBorder(1); frameFmt.setBorderBrush(QColor(0xFFFFFF)); pFrame->setFrameFormat(frameFmt); //qDebug() << toHtml(); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyTextEdit edit; edit.show(); return app.exec(); } #include "main.moc"