QTextEdit word blocks and text formatting - qt

I need to make a QLineEdit with non-editable word blocks.
For instance, imagine I drop a word (that actually represents a value) in a QLineEdit.
I would like to write normally on the QLineEdit, BUT if I try to move the cursor inside a word it would move over it, ie, place the cursor at the end/begining of the word.
Also, I would like the word to always show a certain highlight over it, with different colors (to emulate a box that would wrap it).
Any ideas on how to do this? Would a QTextArea be better for this? I was thinking of a QLineEdit because it would actually be inside a QTableWidget cell, so I think it may have more or less the same capabilities, am I wrong?
UPDATE I found these two functions that actually might be helpful for QLineEdit: cursorWordBackward() and cursorWordForward(), so this might help move over the words. Anyone has examples of this?
UPDATE 2 As QLineEdit does not support rich-text, the "highlight" feature I required can only be achieved with a QTextEdit. This has other issues: QTextEdit does not have the functions I mentioned in the first update above, and I don't know if it is possible to put a QTextEdit inside a QTableWidget cell.

QLineEdit can contain only plain text and certainly can't do this. I was thinking about QTextEdit, it can display HTML. But looking at the documentation, I realized that there is no way to insert non-editable block in QTextDocument (which is used by QTextEdit). I think there is no simple solution.
You can try to implement it manually. Catch textChanged() and cursorPositionChanged() signals of QTextEdit, analyze its content and cursor position and modify them if user moved the cursor into non-editable block or changed its content.

Maybe you could achieve this by using an inputMask...

Related

What is the best way to implement this using Qt

I'm totally new to Qt, so I'd be glad to have a wide answer.
Here I drew up some model:
We have a kind of table that contains:
an integer value with a spinbox.
a cell with three(not specifically) grouped radio buttons
Editbox
A button that interacts with this particular editbox.
Also we have 2 buttons to add and remove items from the table.
I did some google search and found out that it can be done via QTableView.
Is there any way to put such complex structures into a cell? Must it be a separate class inherited from QTableView?
If you're going to have up to a hundred or maybe a few hundreds of elements in the table, then use QTableWidget.
If you're going to have too many elements (about thousands), then go for QTableView, and learn model-view programming.
The reason why I recommend QTableWidget is because you're a beginner. All you have to do there is create a widget, and use setCellWidget() and you're done.
If you have thousands of rows, then you're gonna have to draw the widgets yourself using QStyledItemDelegate, which will paint the widgets inside your QTableView. This is a very painful thing to do, but there's no way around it. The reasons you can find here.
I see at least three options to implement that in Qt:
Use a QtableView or QTableWidget and insert some custom controls in it. See comments made be other persons to your post
Use a QGridLayout and fill it with your controls by line and column
Make your own QWidget to store and manage the line elements (the spinbox, edit field, radio button) using a QHBoxLayout. You can design this in QtCreator, it can have it's own .ui. This could make it easy to handle the interaction between each QWidget of a line (directly handled by your QWidget class). Later, you can put an instance of it for every line you need in a QVBoxLayout.
Personnaly, I would go with the last option, but it may not work smartly if the controls of each line have different content/size (see comments), then first options should be prefered.

How to setText for QPlainTextEdit?

Qt5's documentation doesn't mention that QPlainTextEdit has setText(QString) like QTextEdit does. But, I don't think it's impossible. The only way I found is to use QTextDocument which can has setPlainText(const QString& text). So I have to do this:
plain_text_edit->setDocument(text_document);
The problem is text_document should be a pointer. Not like QTextEdit's setText which can take a local variable as it's parameter. So, is there anyway to do setText like to QPlainTextEdit?
It's very simple, just get the current document and set its text:
plain_text_edit->document()->setPlainText(text);
Alternative way, just call this method:
plain_text_edit->setPlainText(text);
You could also use text cursor of the editor in many ways to achieve this, most simply by selecting entire existing text (assuming the editor is not empty), then doing plain_text_edit->TextCursor().insertText(text); (which replaces currently selected text with usual paste semantics), but for the simple case of replacing all text, that's overcomplicated.

QTextEdit: scroll beyond last line

I display some rich text contents using a subclass of QTextEdit.
Some editors - especially code editors & IDEs offer a possibility to scroll beyond the last line all the way until the last line gets to the top of the view.
I'd love to be able to do the same thing using QTextEdit. Is this somehow possible? I've been reading the doc but could not find anything.
Thank you.
I never had used QTextEdit that way, but maybe you can solve your problem with scrollToAnchor(QString arg) (which ensure the 'arg' presented in text will be visible), or ensureCursorVisible().

How can I access a modified QLabel?

I am instantiating an editable QLabel like so:
QLabel foo("some text");
foo.setTextInteractionFlags(Qt::TextEditorInteraction);
I can click the text and modify it, and the modified text must be in a buffer somewhere, but even after examining the data fields in Qt Creator I don't see where it is:
QString notmodified = foo.text(); // only returns the original text
is the modified text somewhere that I can access it?
EDIT: I think using something else is indeed an easier way, but I'm still interested in knowing the answer to my question.
EDIT: OK, it's been a week. "Answered".
I would say that even though you can set this flag on a QLabel (the Qt::TextInteractionFlag is used by other widgets than QLabel), it is not designed to be edited.
Why don't you use a QLineEdit ?
For editable text field you have a good choise, QLineEdit or QTextEdit. Use one of these widgets. QLabel is just for labeling.

How do I tell Qt to always show an editor in a QTableView?

I've got a QTableView for which I want to display the last column always in edit mode. (It's a QComboBox where the user should be able to always change the value.)
I think I've seen the solution in the Qt documentation, but I can't find it anymore. Is there a simple way of doing it?
I think I could archive this effect by using openPersistentEditor() for every cell, but I'm looking for a better way. (Like specifying it only one time for the whole column.)
One way to get the automatic editing behaviour is to call the view's setEditTriggers() function with the QAbstractItemView::AllEditTriggers value.
To display the contents of a given column in a certain way, take a look at QAbstractItemView::setItemDelegateForColumn(). This will let you specify a custom delegate just for those items that need it. However, it won't automatically create an editor widget for each of them (there could in principle be thousands of them), but you could use the delegate to render each item in a way that makes it look like an editor widget.
There are two possibilities:
Using setIndexWidget, but Trolltech writes:
This function should only be used to
display static content within the
visible area corresponding to an item
of data. If you want to display custom
dynamic content or implement a custom
editor widget, subclass QItemDelegate
instead.
(And it breaks the Model/View pattern…)
Or using a delegate's paint method. But here you have to implement everything like enabled/disabled elements yourself.
The QAbstractItemModel::flags virtual function is called to test if an item is editable (see Qt::ItemIsEditable). Take a look at Making the Model Editable in the Model/View Programming documentation.
I can't see an easy way to do this, but you might be able to manage by using a delegate. I honestly don't know exactly how it would work, but you should be able to get something working if you try hard enough. If you get a proper delegate, you should be able to set it on a whole view, one cell of a view, or just a column or row.

Resources