Results 1 to 5 of 5

Thread: QTextEdit with document template

  1. #1
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QTextEdit with document template

    Hello all! I need to create QTextEdit with document template like this:



    Qt Code:
    1. *** HEADER LINE ***
    2. text block 1
    3. *** HEADER LINE 2 ***
    4. text block 2
    To copy to clipboard, switch view to plain text mode 

    Header lines must be readonly, not selectable, not deletable and must be created by default on widget creation. contents of text blocks between header lines must be available outside of widget for each block.

    Is it possible to create this?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTextEdit with document template

    I don't think this is very easy to do. QTextEdit allows you to set the entire document as read-only, but not individual parts of it.

    If you have only a small number of these blocks, can you make a GUI with several QTextEdit instances, each one with a QLabel at the top (*** HEADER LINE ***) followed by a QTextEdit for the corresponding text block?

    How about using a QTableWidget or QListWidget instead of a QTextEdit? You can set the read-only and selectability on a cell-by-cell basis, and can hide the grid lines. This will make it look like one continuous edit window, but your users will only be allowed to change text in the cells where you have allowed it. The text in each cell will be available as individual blocks.

    Alternatively, you can store your text blocks in a QAbstractItemModel and use QTableView or QListView instead. If you prefer to use QTextDocument you could wrap a model around this where each row represents a QTextBlock and you use the model's Qt::ItemFlags to control editability on a row-by-row basis.
    Last edited by d_stranz; 5th September 2014 at 15:17.

  3. #3
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit with document template

    Thank you. I will try to implement using QListView. QTextDocument not preferred, so I can use string list model

  4. #4
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit with document template

    I've created QListView with simple QStringListModel and it works properly, but my edit controls not resigzing properly. How can i make this editors resizable to auto fit it heights to display all editing text? Is it possible to use QSyntaxHighlighter to paint cell by QStyledItemDelegate?

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTextEdit with document template

    Instead of using QStringListModel directly, derive a class from it and reimplement the QStringListModel::data() method. In particular, implement new code to handle the Qt::SizeHintRole to return the appropriate height for the text. All of the other Qt::ItemDataRole handling can be passed up to the QStringListModel::data() base class implementation.

    QSyntaxHighlighter works only on QTextDocument. You could possibly implement a syntax highlighter on QTextDocuments that each contain one list view row's text, then retrieve the text using QTextDocument::toHtml(), but I do not know if the QListView supports display of HTML text in cells. This seems like the hard way to do it, since you have to do all the formatting in the QSyntaxHighlighter class anyway.

    If QListView supports HTML formatting in cell contents, then you can simply use the model's dataChanged() signal to monitor changes in the text, then reformat it as HTML for display. This may interfere with editing, though.

Similar Threads

  1. Replies: 9
    Last Post: 17th June 2014, 08:59
  2. Replies: 4
    Last Post: 25th June 2010, 09:21
  3. How to handle Large Immutable Document in QTextEdit
    By rcjohns in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2010, 20:35
  4. QTextEdit setCurrentFont() for entire document
    By sunil.thaha in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2006, 07: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.