Results 1 to 5 of 5

Thread: Text Alignment in a textedit in qt creator

  1. #1
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Text Alignment in a textedit in qt creator

    I wish to introduce text alignment in a textedit such that whenever I press ENTER key, It automatically takes 4 spaces in the next line just like the code editor in qt.... Is there any way to implement this? Can anyone help me out?

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Text Alignment in a textedit in qt creator

    use eventFilter to filter the enterKey press
    Qt Code:
    1. eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj == textEdit) {
    4. if (event->type() == QEvent::KeyPress) {
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    6. qDebug() << "Ate key press" << keyEvent->key();
    7. return true;
    8. } else {
    9. return false;
    10. }
    11. } else {
    12. // pass the event on to the parent class
    13. return QMainWindow::eventFilter(obj, event);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    and use settext ()
    Qt Code:
    1. textEdit->setText("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; text-indent:20px;\">the showing text</p>");
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Text Alignment in a textedit in qt creator

    Some additional notes:

    Instead of setting an event filter you can subclass QTextEdit (or QPlainTextEdit which QtC is using) and use keyPressEvent() directly. Since setText() replaces the entire previous text, better use QTextCursor. It can be received by textCursor().

  4. The following user says thank you to Lykurg for this useful post:

    aaditya190 (11th December 2013)

  5. #4
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Text Alignment in a textedit in qt creator

    I am using this code as an event of MainWindow in mainwindow.cpp as-:

    Qt Code:
    1. MainWindow::eventFilter(QObject *obj, QEvent *event)
    2. {
    3.  
    4. if (obj == ui->textEdit)
    5. {
    6. if (event->type() == QEvent::KeyPress)
    7. {
    8. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    9. qDebug() << "Ate key press" << keyEvent->key();
    10. return true;
    11. } else {
    12. return false;
    13. }
    14. } else {
    15. // pass the event on to the parent class
    16. return QMainWindow::eventFilter(obj, event);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    It is giving an error that ISO C++ forbids declaration of 'eventFilter' with no type..

    Can you please tell me how to declare this event in mainwindow.h ? and where to define the settext() property of textedit? Please I am very new to Qt. Kindly help me.

  6. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Text Alignment in a textedit in qt creator

    follow as lykurg said .. first create an object as a subclass of QTextEdit , reimplement keyPressEvent of that class . http://www.codeprogress.com/cpp/libr...p#.Uqg29vWEq1s
    and use the QTextCursor to set the text with the above html code i gave last time .
    "Behind every great fortune lies a crime" - Balzac

  7. The following user says thank you to wagmare for this useful post:

    aaditya190 (11th December 2013)

Similar Threads

  1. QComboBox text alignment
    By mentalmushroom in forum Qt Programming
    Replies: 1
    Last Post: 31st January 2012, 21:33
  2. Help text color in TextEdit
    By tho97 in forum Qt Programming
    Replies: 5
    Last Post: 26th March 2008, 08:47
  3. QToolButton text alignment
    By Vladimir in forum Qt Programming
    Replies: 6
    Last Post: 2nd March 2007, 07:42
  4. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02
  5. Icon Text Alignment
    By nupul in forum Newbie
    Replies: 3
    Last Post: 1st May 2006, 04:47

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.