PDA

View Full Version : Customize the QLineEdit by paintEvent(), the cursor can't follow the text.



zimang
25th November 2010, 06:29
I paint widget QLineEdit through paintEvent() by myself, but the problem is the cursor can't follow the text.

I set the text AlignCenter, and drawText also AlignCenter, but the cursor still think my word is AlignLeft.


painter.drawText(0, 0, button_rect.width(), button_rect.height(), Qt::AlignCenter, text);

QLine cursor(this->cursorRect().x(), this->cursorRect().y(), this->cursorRect().x() , this->cursorRect().y()+ this->cursorRect().height() -1);

painter.setPen(QPen(QBrush(QColor(0,0,0,255)), 3.0));
painter.drawLine(cursor);


here the picture, and the black vertical line is cursor.

http://my.chinaunix.net/space.php?uid=22954826&do=album&picid=32204&goto=down

high_flyer
25th November 2010, 09:36
QLine cursor(this->cursorRect().x(), this->cursorRect().y(), this->cursorRect().x() , this->cursorRect().y()+ this->cursorRect().height() -1);
Of course the cursor is in the same place, since you are giving it the current cursor position.
You need to give your cursor the position you want it to be in!
this->cursorRect().x() + ofsset

zimang
25th November 2010, 09:53
yes, it's right, thanks a lot.