QTextEdit start text one tab in?
-
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