PDA

View Full Version : QTextEdit with document template



golubevsv
4th September 2014, 15:59
Hello all! I need to create QTextEdit with document template like this:





*** HEADER LINE ***
text block 1
*** HEADER LINE 2 ***
text block 2


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?

d_stranz
5th September 2014, 15:09
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.

golubevsv
6th September 2014, 10:41
Thank you. I will try to implement using QListView. QTextDocument not preferred, so I can use string list model

golubevsv
9th September 2014, 11:27
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?

d_stranz
9th September 2014, 21:39
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.