Qt Plain text edit, printing from right - qt

I am making a calculator in qt.
I have a plain text edit box.
when I input numbers in it, I want it to print from the right(like any other calculator).
also, I want to disable input of anything except numbers.
(little new in qt)

You just need to use setAlignment(Qt::AlignRight) of your QLineEdit and
QLineEdit::setValidator(), for example:
myLineEdit->setValidator( new QIntValidator(0, 100, this) );
Take alook at this one as well: Set QLineEdit to accept only numbers

Related

How did I retrieve changes in the content of all text edit boxs in a widget

Now, I have a widget that used for conguring some parameters, There were some QlineEdit with default value and a save button on this widget. People may change the content of QlineEdit. And click the save button, so that the modified parameters can take effect. Here is my question:
How do retrieve changes in the content of all text line edit in this QWidget?
Once I know which edit content has changed, I can judge whether the modified values is legal, and then let the change take effect.
Can anyone give me some ideas?
OS: Windows10
QT: qt 5.9.0
For this situation, It's better to do one more step before manually validating the user's input. The step is to limit the user to enter invalid settings. If your setting value is a number, use QSpinBox or QDoubleSpinBox for floating-point values. If you want to let the user select from multiple predefined values, like gender(Male, Female), use QComboBox or QRadioButton and so forth. Here is the list of Qt's widgets. So bear in mind, using QLineEdit for all of the inputs is not a good idea.
If your input is something more complex, you can use validators. For getting the idea see this question.
At last, you connect the save button's clicked signal to the slot defined in your widget class using Qt's signals and slots mechanism and get values from all of your inputs and check them, and if everything is OK, apply them to your system.

How to show line number on side right QPlainTextEdit in Qt

I am just learning Qt. I want to show line number of QPlainTextEdit. I found this link http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
and it worked. But now I want QPlainTextEdit to show line number with other controls on the form as in the image below.
Thanks!
The example from doc.qt.io that you've cited does exactly what you are asking. They subclassed QPlainTextEdit and extended it accordingly. If you just need line numbers, simply delete extra functionality from that example.

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 word blocks and text formatting

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...

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.

Resources