Customize the QLineEdit by paintEvent(), the cursor can't follow the text.
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?ui...2204&goto=down
Re: Customize the QLineEdit by paintEvent(), the cursor can't follow the text.
Quote:
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
Re: Customize the QLineEdit by paintEvent(), the cursor can't follow the text.
yes, it's right, thanks a lot.