Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit start text one tab in?
Forum Updated to NodeBB v4.3 + New Features

QTextEdit start text one tab in?

Scheduled Pinned Locked Moved Solved General and Desktop
qtextedit
16 Posts 5 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    legitnameyo
    wrote on last edited by
    #3
     // 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?

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #4

      Hi
      An alternative is also

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

      alt text

      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

      1 Reply Last reply
      3
      • L Offline
        L Offline
        legitnameyo
        wrote on last edited by
        #5

        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?

        1 Reply Last reply
        0
        • L legitnameyo
           // 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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #6

          @legitnameyo

          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.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            legitnameyo
            wrote on last edited by
            #7

            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.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #8
              • 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.

              L 1 Reply Last reply
              0
              • mrjjM mrjj
                • 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.

                L Offline
                L Offline
                legitnameyo
                wrote on last edited by
                #9

                @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?

                jsulmJ 1 Reply Last reply
                0
                • L legitnameyo

                  @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?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    legitnameyo
                    wrote on last edited by
                    #11

                    @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
                    
                    mrjjM 1 Reply Last reply
                    0
                    • L legitnameyo

                      @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
                      
                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #12

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

                      1 Reply Last reply
                      3
                      • L Offline
                        L Offline
                        legitnameyo
                        wrote on last edited by
                        #13

                        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
                        
                        
                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          legitnameyo
                          wrote on last edited by
                          #14

                          Solved it by removing the function in the class of customQTextEdit and adding "public" right after the first {

                          mrjjM 1 Reply Last reply
                          0
                          • L legitnameyo

                            Solved it by removing the function in the class of customQTextEdit and adding "public" right after the first {

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @legitnameyo
                            So finally it runs ?

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              legitnameyo
                              wrote on last edited by
                              #16

                              Yes, and works perfectly

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved