Results 1 to 3 of 3

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

  1. #1
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question 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.


  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Customize the QLineEdit by paintEvent(), the cursor can't follow the text.

    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
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    zimang (25th November 2010)

  4. #3
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Customize the QLineEdit by paintEvent(), the cursor can't follow the text.

    yes, it's right, thanks a lot.

Similar Threads

  1. How to set the Text Cursor in a PlainTextEdit box?
    By Mia S. in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2010, 16:30
  2. "Follow symbol under cursor" does not work...?
    By liormessinger in forum Qt Tools
    Replies: 0
    Last Post: 16th September 2009, 17:22
  3. QLineEdit keep cursor active.
    By bunjee in forum Qt Programming
    Replies: 10
    Last Post: 2nd September 2009, 14:50
  4. Is there a way to get text from under the mouse cursor?
    By squirrel in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2009, 01:39
  5. How to synchronize text cursor and cursor in QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 09:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.