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?
QtWS25 Last Chance

QTextEdit start text one tab in?

Scheduled Pinned Locked Moved Solved General and Desktop
qtextedit
16 Posts 5 Posters 3.1k Views
  • 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 5 Feb 2019, 18:57 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Feb 2019, 20:46 last edited by
      #2

      Hi,

      One quick way would be to react to the textChanged signal and if the text edit is empty, add the tab.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • L Offline
        L Offline
        legitnameyo
        wrote on 6 Feb 2019, 07:18 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?

        J 1 Reply Last reply 6 Feb 2019, 10:18
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 6 Feb 2019, 08:23 last edited by mrjj 2 Jun 2019, 08:36
          #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 6 Feb 2019, 09:48 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
              6 Feb 2019, 07:18
               // 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?

              J Offline
              J Offline
              JonB
              wrote on 6 Feb 2019, 10:18 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 6 Feb 2019, 10:47 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
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 6 Feb 2019, 11:30 last edited by mrjj 2 Jun 2019, 11:34
                  #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 6 Feb 2019, 11:40
                  0
                  • M mrjj
                    6 Feb 2019, 11:30
                    • 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 6 Feb 2019, 11:40 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?

                    J 1 Reply Last reply 6 Feb 2019, 11:45
                    0
                    • L legitnameyo
                      6 Feb 2019, 11:40

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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 6 Feb 2019, 11:45 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 6 Feb 2019, 12:06 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
                        
                        M 1 Reply Last reply 6 Feb 2019, 12:07
                        0
                        • L legitnameyo
                          6 Feb 2019, 12:06

                          @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
                          
                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 6 Feb 2019, 12:07 last edited by mrjj 2 Jun 2019, 12:10
                          #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 6 Feb 2019, 12:16 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 6 Feb 2019, 12:26 last edited by
                              #14

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

                              M 1 Reply Last reply 6 Feb 2019, 12:28
                              0
                              • L legitnameyo
                                6 Feb 2019, 12:26

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

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 6 Feb 2019, 12:28 last edited by
                                #15

                                @legitnameyo
                                So finally it runs ?

                                1 Reply Last reply
                                0
                                • L Offline
                                  L Offline
                                  legitnameyo
                                  wrote on 6 Feb 2019, 14:33 last edited by
                                  #16

                                  Yes, and works perfectly

                                  1 Reply Last reply
                                  1

                                  5/16

                                  6 Feb 2019, 09:48

                                  11 unread
                                  • Login

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