QTextEdit start text one tab in?
-
I'd like my text to start a tab in, no matter what the user does. I can make the user start the text one tab in but if they press backspace they go back to the regular setup, which is not what I want. Is there any way to style how far the text will go and where the text starts? Thanks!
-
Hi,
One quick way would be to react to the textChanged signal and if the text edit is empty, add the tab.
-
// starts text with indent (tab) QTextBlockFormat bf; bf.setIndent(1); QTextCursor tc; tc.setBlockFormat(bf); ui->note->textCursor().setBlockFormat(bf);
This is the code I use to create a tab when the program starts. This works fine until my user presses backspace on the actual tab, then the text gets aligned along the left side as usual again. I don't see how reacting to empty text would solve my problem, could you please elaborate @SGaist?
-
Hi
An alternative is alsoclass TextEdit: public QTextEdit { Q_OBJECT public: TextEdit(QWidget *p = 0): QTextEdit(p) { setViewportMargins(30, 30, 30, 30); // add 30 pixels area around text setStyleSheet("TextEdit { background: white; }"); // make the new "border" white } };
Thats give a hard limit border around text area.
however, its not based on Text content (such as tab) but directly on the viewport. so
say if you print it, it will not have such margin on paper.to very easily use such sub class, you can use Creators promotion feature.
http://doc.qt.io/qt-5/designer-using-custom-widgets.html -
Where do I append this "class" and "public" code? I've tried accessing setViewportMargin directly but the class is private. I can't access through QTextEdit nor through QAbstractScrollArea. Again, where do I add it?
-
This works fine until my user presses backspace on the actual tab, then the text gets aligned along the left side as usual again. I don't see how reacting to empty text would solve my problem, could you please elaborate @SGaist?
Going down @SGaist's simple route, clearly just reacting to "empty" text is not good enough. But the principle still holds: in your slot for
textChanged
you actually should test whether the first character of the text is a tab, and insert one if it is not. Simplez!Alternatively, you could place a validator on the text edit which returns anything without a leading tab as "positively invalid". the way Qt text edits validation work, this should prevent the user from deleting the tab at the beginning at all. I don't know whether you prefer that to
textChanged
signal or not.Having said the above, just why do you want the text to start with a tab? If you genuinely want a tab in the data returned then go down the above route(s). However, if you want it simply for some kind of visual/layout reason only, I would not force insertion of a character into the widget, rather I would use @mrjj's suggestion of inserting some margin/padding into the widget.
Think about the logic of your data, and do whatever is appropriate tab-wise to whatever you want the data returned to actually be.
-
The text needs to be more to the left since I'll actually add a button ON the QTextEdit and it will be disastrous to have the button overlap the text, for obvious reasons. I am not yet sure about the whole "if you were to print then the viewport wont add those indent changes" thingy, but I would like to have the option to be able to print and have the format intact!
tl;dr having a viewport margin would in my case be better than actually adding a tab into the document.
-
- Where do I append this "class" and "public" code? I've tried accessing setViewportMargin directly but the class is private. I can't -access through QTextEdit nor through QAbstractScrollArea. Again, where do I add it?
Its a new class. a so called subclass. setViewportMargins can only be called by
inheriting from QTextEdit.
To use it:
Add it to its own .h file. also add some #includes
And that .h file to the project.
then read
http://doc.qt.io/qt-5/designer-using-custom-widgets.html
To have be your text edit when run. ( if you use Designer)
else you just have to new it your self.The promotion feature is very easy to use.
if your new h file is called MyTextEdit.h
then you just give TextEdit as classname and
MyTextEdit.h for the file
then when run, its your class instead.
so its nto worse then right clicking your existing TextEdit,
and select promote. then fill out info, press Add button, then promote button. -
@mrjj I have not idea what you're talking about. I've tried adding a new .c and .h with a custom name and then "promoting" my QTextEdit to that same name, but that results in about 50 different errors regarding ui_myProject.h and other files. You continually wrote "Add it to its own .h file ..." what is "it"? Why do I need to add a .h file to the project, can't I just create one directly?
-
@legitnameyo said in QTextEdit start text one tab in?:
Why do I need to add a .h file to the project, can't I just create one directly?
It's the same.
If you have errors then show the errors and your code. -
@mrjj said in QTextEdit start text one tab in?:
Q_OBJECT
#ifndef CUSTOMQTEXTEDIT_H #define CUSTOMQTEXTEDIT_H #include <QWidget> #include <QTextEdit> class customQTextEdit { public: customQTextEdit(); customQTextEdit(QWidget *p = 0): customQTextEdit(p) { setViewportMargins(30, 30, 30, 30); // add 30 pixels area around text setStyleSheet("TextEdit { background: white; }"); // make the new "border" white } }; #endif // CUSTOMQTEXTEDIT_H
Gives me
error: use of undeclared identifier 'setViewportMargins' error: use of undeclared identifier 'setStyleSheet' error: constructor for 'customQTextEdit' creates a delegation cycle
-
@legitnameyo
you dont inherit QTextEdit.
So that is why :)class customQTextEdit : public QTextEdit
Also your constructor seems odd.
make SURE you init base class.
TextEdit(QWidget *p = 0): QTextEdit(p) ... -
Now I get
/error: out-of-line definition of 'customQTextEdit' does not match any declaration in 'customQTextEdit'
for the code
#ifndef CUSTOMQTEXTEDIT_H #define CUSTOMQTEXTEDIT_H #include <QWidget> #include <QTextEdit> class customQTextEdit: public QTextEdit { customQTextEdit(QWidget *p = 0): QTextEdit(p) { setViewportMargins(30, 30, 30, 30); // add 30 pixels area around text setStyleSheet("TextEdit { background: white; }"); // make the new "border" white } }; #endif // CUSTOMQTEXTEDIT_H
-
Solved it by removing the function in the class of customQTextEdit and adding "public" right after the first {
-
@legitnameyo
So finally it runs ? -
Yes, and works perfectly