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.
Related
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
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...
I've to deal with a scientific application and thus have to use RTF at many place (think about displaying units with exponents mainly).
I've implemented a delegate to deal with tables and drop box and it works mostly fine. (implementation : http://pastebin.com/FuCbGqkY , header : http://pastebin.com/D6hxeWdF ).
However, I've been into a major issue : it looks like the "button" part of the QComboBox is not rendered with the delegate (it only applies to the drop down box). Is there any way to have the text in the combobox when it's not dropped displayed correctly ? If not, what do I have to do ? Subclass and overwrite paint method ? Looks like a lot of pain and basically it makes the delegate useless.
Any clue ?
Consider QComboBox::lineEdit() method. You can try to apply your changes to the QLineEdit directly if it's possible. Otherwise you can subclass QLineEdit and change its behaviour to what you want, and then insert it using QComboBox::setLineEdit. Maybe you will need to make the combo box editable in order to use this.
I want to build a structured document editor using Qt. The base concept for v1 is nested sections, each section having a title and one or more paragraphs. Sections and paragraphs are distinct visual units (probably via background shading). I also need to be able to store character-level semantics (ie: this run of text is associated with reference X). If I wanted to build a read-only view of this it would be doable with QFrame for the sections and a QLabel for each title and each paragraph. To make this editable I'm pretty sure I can capture all keyboard events to the window and implement a cursored text-entry-and-editing feel that way.
What I'm having trouble with is how to handle copy/paste.
I want the clipboard interactions to feel native: that is, ctrl+c/v on window, command+c/v on OSX, ctrl+c/v for clipboard on X, select to copy for PRIMARY on X, middle click to paste for PRIMARY on X, etc.
The standard text editing controls in Qt handle all of this just fine. I'm wondering if there is some sort of "paste event" or similar that I can grab to implement the same thing in my custom widget? Is there another way?
For native keyboard shortcuts, you can add them to menu items:
ui->actionCut->setShortcut(QKeySequence::Cut);
ui->actionCopy->setShortcut(QKeySequence::Copy);
ui->actionInsert_empty_row->setShortcut(Qt::Key_Insert);
ui->actionPaste->setShortcut(QKeySequence::Paste);
ui->actionRemove->setShortcut(QKeySequence::Delete);
See QKeySequence docs
There's no paste signal/event as far as i know to listen to, though there's nothing stopping you from taking a sneak look at how the paste() slot is implemented in widgets like QLineEdit and implement your own if possible. The afferent signal is not that important, since it's just a signal and you can trigger that whenever you desire(eg. Ctrl+v, context menu or program menu).
LE: If i think better, you might be thinking this the wrong way, you don't need a signal, you just need the slot that you can call whenever the action is called by any means you wish(eg. ctrl+v). Once you have the slot(QClipboard), it's just a matter of properly connecting it to the desired triggering actions/signals.
I'm not completely sure why you would want to do it with a QLabel and then capture the keys when there are already classes that handle text edition for you (and you can even override the key pressed funcionality)
If you want to have editable text, you could use a QTextEdit or a QPlainTextEdit and those classes already handle the copy-paste functionality (even with right click menu and everything).
If you want to add special a special behavior to your copy and paste, you can override the Mime functions:
//in your header file, add
void insertFromMimeData(const QMimeData *source) override; // override for paste
QMimeData * createMimeDataFromSelection() const override; // override for copy
// in the cpp:
//it would be something like this:
void YourTextField::insertFromMimeData(const QMimeData *source) {
// Do something special on the paste event, maybe even create your own "source"
//call the base class insert
QPlainTextEdit::insertFromMimeData(source);
}
Note: I'm not 100% with the copy, since I only overrode paste, but I'm almost certain that that's the right function.
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.