Results 1 to 6 of 6

Thread: How to make a specific string read-only in a textedit in Qt?

  1. #1
    Join Date
    Jul 2013
    Posts
    3

    Default How to make a specific string read-only in a textedit in Qt?

    Hi,

    I am designing a command prompt using Qt textedit with the prompt as shown below...

    comman-prompt>

    The string is coming every time an enter is pressed as shown below...

    comman-prompt> ls (with enter pressed)
    comman-prompt> ps (with enter pressed)
    comman-prompt>

    but I do not have any idea how to disable the back space from removing this prompt. I mean the prompt should always be read-only with the cursor and back space should not enter in to it.

    Any suggestion is highly appreciated.

    Thanks in Advance,
    Prabhu

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to make a specific string read-only in a textedit in Qt?

    This example should give you some idea. This example is based on Qt5, but should be trival to run using Qt4
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include <QtWidgets>
    4.  
    5. class TextEdit : public QTextEdit
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit TextEdit(const QString & prompt, QWidget * parent = 0)
    10. : QTextEdit(parent)
    11. , mPrompt(prompt)
    12. , mCount(0)
    13. {
    14. append(mPrompt);
    15. }
    16.  
    17. protected:
    18. void keyPressEvent(QKeyEvent * event)
    19. {
    20. if((event->key() == Qt::Key_Up) or
    21. (event->key() == Qt::Key_Down) or
    22. (event->key() == Qt::Key_Left) or
    23. (event->key() == Qt::Key_Right))
    24. {
    25. return;
    26.  
    27. }
    28. else if(event->key() == Qt::Key_Return)
    29. {
    30. append(mPrompt);
    31. mCount = 0;
    32. return;
    33. }
    34. else if(event->key() == Qt::Key_Backspace)
    35. {
    36. if(mCount > 0)
    37. mCount--;
    38. else
    39. return;
    40. }
    41. else if((event->key() & Qt::Key_Escape) == Qt::Key_Escape)
    42. {
    43. return;
    44. }
    45. else
    46. {
    47. mCount++;
    48. }
    49.  
    50. return QTextEdit::keyPressEvent(event);
    51. }
    52.  
    53. private:
    54. QString mPrompt;
    55. unsigned int mCount;
    56. };
    57.  
    58. int main(int argc, char ** argv)
    59. {
    60. QApplication app(argc, argv);
    61.  
    62. TextEdit widget("comman-prompt>");
    63. widget.show();
    64.  
    65. return app.exec();
    66. }
    67.  
    68. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2013
    Posts
    3

    Default Re: How to make a specific string read-only in a textedit in Qt?

    Thanks A lot...I modified my code according to your suggestion and its working fine. But I am getting another issue. When I am typing some thing, it is recognizing the input as a string but coping some thing and pasting in the command prompt is not recognized as keyboard event and not generating the corresponding keys. Below is the example.

    command_prompt>ls -l (pasted not typed)

    here the characters "ls -l" since copied and pasted, the respective key events are not getting generated and not recognized as a command.

    Need your valuable suggestion for this to be solved.

    Thanks,
    prabhu

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to make a specific string read-only in a textedit in Qt?

    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include <QtWidgets>
    4.  
    5. class TextEdit : public QTextEdit
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit TextEdit(const QString & prompt, QWidget * parent = 0)
    10. : QTextEdit(parent)
    11. , mPrompt(prompt)
    12. , mCount(0)
    13. {
    14. append(mPrompt);
    15. }
    16.  
    17. protected:
    18. void keyPressEvent(QKeyEvent * event)
    19. {
    20. qDebug() << mCount;
    21.  
    22. if((event->key() == Qt::Key_Up) or
    23. (event->key() == Qt::Key_Down) or
    24. (event->key() == Qt::Key_Left) or
    25. (event->key() == Qt::Key_Right))
    26. {
    27. return;
    28.  
    29. }
    30. else if(event->key() == Qt::Key_Return)
    31. {
    32. append(mPrompt);
    33. mCount = 0;
    34. return;
    35. }
    36. else if(event->key() == Qt::Key_Backspace)
    37. {
    38. if(mCount > 0)
    39. mCount--;
    40. else
    41. return;
    42. }
    43. else if(event->key() == Qt::Key_V)
    44. {
    45. if(event->modifiers() & Qt::ControlModifier)
    46. mCount += QApplication::clipboard()->text().length();
    47. }
    48. else if(event->key() == Qt::Key_Insert)
    49. {
    50. if(event->modifiers() & Qt::ShiftModifier)
    51. mCount += QApplication::clipboard()->text().length();
    52. }
    53. else if((event->key() & Qt::Key_Escape) == Qt::Key_Escape)
    54. {
    55. return;
    56. }
    57. else
    58. {
    59. mCount++;
    60. }
    61.  
    62. return QTextEdit::keyPressEvent(event);
    63. }
    64.  
    65. private:
    66. QString mPrompt;
    67. unsigned int mCount;
    68. };
    69.  
    70. int main(int argc, char ** argv)
    71. {
    72. QApplication app(argc, argv);
    73.  
    74. TextEdit widget("comman-prompt>");
    75. widget.show();
    76.  
    77. return app.exec();
    78. }
    79.  
    80. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jul 2013
    Posts
    3

    Default Re: How to make a specific string read-only in a textedit in Qt?

    Hi,
    First of all I thank you for your reply that helped me a lot for solving the issue I faced. The previous copy-paste option is working now very fine based upon the suggestion u gave. but the mouse clik option is not working. I mean when I am pasting using mouse the respected key events for the word I pasted using mouse are not getting generated and the command is not working. Need your valuable suggestion to get out of this issue

    command_prompt>ls -l (pasted using mouse)

    The key events for command "ls -l" is not getting generated and the command is not getting recognised.


    Thanks,
    prabhu

  6. #6
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make a specific string read-only in a textedit in Qt?

    Hi Santhosh,
    I am sorry for replying to little old thread , but i am struggling for few days to solve this problem and i am asking this because you mentioned Qt5 in the post, thought you have Qt5 installed on your system and may be you will try running my program and provide some suggestion.


    http://www.qtcentre.org/threads/5492...dwidgets/page2

Similar Threads

  1. How do I search for files which contain a specific text string?
    By rolandburt in forum General Programming
    Replies: 1
    Last Post: 20th August 2012, 21:32
  2. Read a specific line from a file
    By rleojoseph in forum Qt Programming
    Replies: 11
    Last Post: 21st March 2011, 11:58
  3. Replies: 1
    Last Post: 22nd July 2010, 17:12
  4. How to make text in label or TextEDit to flash?
    By qtlinuxnewbie in forum Newbie
    Replies: 2
    Last Post: 6th April 2010, 07:53
  5. Make the cursor visible in textEdit (Qt3.3.5)
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 24th January 2007, 22:50

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.