Results 1 to 20 of 21

Thread: protect some text in a QTextEdit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default protect some text in a QTextEdit

    Hi
    Is there a way of making some text within a QTextEdit non editable?
    I want to insert some text programatically but prevent the user from editing this text.

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: protect some text in a QTextEdit

    I think you'd need to implement your own QTextObject subclass.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: protect some text in a QTextEdit

    Thanks for the hint
    How would I make my QTextObject read only?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: protect some text in a QTextEdit

    You can start by reading the docs, especially the Text Object Example.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: protect some text in a QTextEdit

    Hi
    OK, I have read the relevant docs and started to implement my own text object for inserting a string.
    I can now get this displayed, but am struggling with how to correctly implement the 'intrinsicSize' method.

    inside this method I can obtain the string to be inserted and then i tried to calculate the size required to display it

    Qt Code:
    1. QString str = qVariantValue<QString>(format.property(Window::ProtectedTextData));
    2. QTextBlock block = doc->findBlock(posInDocument);
    3. QTextCharFormat fmt = block.charFormat();
    4.  
    5. qreal pointSize = fmt.fontPointSize();
    6. size.setHeight(pointSize);
    7. size.setWidth(pointSize*str.length());
    To copy to clipboard, switch view to plain text mode 

    but he size is always 0.
    I would appreciate any hint as to how to correctly implement this

    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: protect some text in a QTextEdit

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    GrahamLabdon (10th October 2011)

  8. #7
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: protect some text in a QTextEdit

    Hi
    Thanks for the help

    Inside the 'intrinsicSize' method I retrive the font by -
    Qt Code:
    1. QTextBlock block = doc->findBlock(posInDocument);
    2. QTextCharFormat fmt = block.charFormat();
    3. QFont font = fmt.font();
    4. QFontMetrics fm(font);
    5. QRect rect = fm.boundingRect(str);
    To copy to clipboard, switch view to plain text mode 
    But this always returns the same thing, I want the font being used in the document at the point that I insert the text.
    Why does my code not work?

    Thanks

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: protect some text in a QTextEdit

    This returns the text format of the whole block. If you want the format for your item, use QTextObject::format() and convert it to the proper type. You can also use QTextCursor::charFormat().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: protect some text in a QTextEdit

    Hi
    Within the intrinsicSize method I cannot get on object of type QTextObject or QTextCursor.

    Qt Code:
    1. QTextObject *textObject = doc->objectForFormat(format);
    To copy to clipboard, switch view to plain text mode 
    returns a null pointer.
    QTextCursor belongs to the QTextEdit class which does not seem to be available in this method

    Am I missing something?

    Graham

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: protect some text in a QTextEdit

    QTextCursor doesn't belong to QTextEdit class. You can have as many cursors as you want, have a look at available constructors. And I have no idea what your objectForFormat() call was supposed to do. You are implementing QTextObject subclass so just calling format() will return a QTextFormat which you can then convert to another type using the API available in QTextFormat.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: protect some text in a QTextEdit

    OK now I am confused
    You say I am implementing a QTextObject subclass, but I am just following the TextObject example that implements
    QTextObjectInterface.
    Where should I be creating a QTextObject ?

    Thanks

Similar Threads

  1. Protect PDF File
    By wirasto in forum Newbie
    Replies: 2
    Last Post: 25th August 2009, 10:19
  2. Replies: 1
    Last Post: 4th December 2008, 06:55
  3. Protect QSqlQueryModel in two threads
    By john_crichton in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2008, 17:00
  4. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  5. Problem on set QGraphicsTextItem write protect.
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 20:53

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
  •  
Qt is a trademark of The Qt Company.