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. add QLabel into QLineEdit
QtWS25 Last Chance

add QLabel into QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 5 Posters 15.8k 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.
  • M Offline
    M Offline
    marlenet15
    wrote on 20 Jan 2016, 18:51 last edited by
    #1

    I would like to have an unedited timestamp into the QLineEdit. So it would look something like this:
    Jan 20 2016:
    When the user clicks on the QLineEdit, they can not edit or even place the cursor where the timestamp is located, only after the colon. Therefore, I decided to add a QLabel into the QlineEdit. I found this forum:

    http://stackoverflow.com/questions/27311577/how-to-put-a-static-text-postfix-prefix-in-qlineedit

    The user states that in order to add a QLabel into QlineEdit you do the following:

    NOTE: you won't be able to put QLabel on QLineEdit in QtDesigner if the QLineEdit is inside of a layout. In this case you can add QWidget instead of QLineEdit in the layout and put QLineEdit and QLabel within this widget which doesn't have a layout.

    So what I did in Qt Designer was to add The Qlabel 'Comments' and QWidget in a formLayout. and in that Qwidget I created a layout inside it.
    My code for creating the QlineEdit and Qlabel is the following (I know the code below places the text to the left instead of the right but that is the code that the forum showed and I just want to test if it works):

    QLabel _dateLabel =  new QLabel(this);
    QlineEdit _commentsLineEdit = new QLineEdit(this);
    
    _dateLabel->setText("testing");
    _dateLabel->move(_commentsLineEdit->rect().right() - _dateLabel->width()-3,
                _commentsLineEdit->rect().center().y() - _dateLabel->height()/2);
    
    ui->commentsWidget->layout()->addWidget(_commentsLineEdit);
    

    However, when I ran the code the text "testing" locates itself in another area of the Screen. Is there anything that I am doing wrong? Or is it preferable that I go in another route?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 20 Jan 2016, 19:02 last edited by
      #2

      Hi
      Why can you just add both the label and the edit in Designer?

      When you new _commentsLineEdit you give "this" as parent.
      Unless "this" is the Qwidget you talk about, LineEdit will be placed in top of main window.

      M 1 Reply Last reply 20 Jan 2016, 19:13
      0
      • M mrjj
        20 Jan 2016, 19:02

        Hi
        Why can you just add both the label and the edit in Designer?

        When you new _commentsLineEdit you give "this" as parent.
        Unless "this" is the Qwidget you talk about, LineEdit will be placed in top of main window.

        M Offline
        M Offline
        marlenet15
        wrote on 20 Jan 2016, 19:13 last edited by
        #3

        @mrjj I tried that right now but what it does is that the cursor overlaps the Qlabel. So when I type into the QLineEdit the text is right under the text of the QLabel. I want the text that I will be typing in to start where QLabel ends.

        M 1 Reply Last reply 20 Jan 2016, 19:18
        0
        • M marlenet15
          20 Jan 2016, 19:13

          @mrjj I tried that right now but what it does is that the cursor overlaps the Qlabel. So when I type into the QLineEdit the text is right under the text of the QLabel. I want the text that I will be typing in to start where QLabel ends.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 20 Jan 2016, 19:18 last edited by mrjj
          #4

          @marlenet15
          and the label should be sort of inside the lineEdit ?
          something like
          Jan 20 2016:____________
          where ________ is the linedit is not ok?

          M 1 Reply Last reply 20 Jan 2016, 19:21
          0
          • M mrjj
            20 Jan 2016, 19:18

            @marlenet15
            and the label should be sort of inside the lineEdit ?
            something like
            Jan 20 2016:____________
            where ________ is the linedit is not ok?

            M Offline
            M Offline
            marlenet15
            wrote on 20 Jan 2016, 19:21 last edited by
            #5

            @mrjj Yes. I would like for the QLabel to be inside the QLineEdit. So I would like it to look like this

            Comments(QLabel) (Jan 19 2016: ____________________________ ) <- inside paranthesis is the QLineEdit and the __ is the part where the user can type

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 20 Jan 2016, 19:29 last edited by
              #6

              Well , it will be messy as the cursor might go into/under label
              so not so easy to match up.
              The same you linked to is postfix so if we limit the length
              then it wont go into. but in ur case it will.

              M 1 Reply Last reply 20 Jan 2016, 19:31
              0
              • M mrjj
                20 Jan 2016, 19:29

                Well , it will be messy as the cursor might go into/under label
                so not so easy to match up.
                The same you linked to is postfix so if we limit the length
                then it wont go into. but in ur case it will.

                M Offline
                M Offline
                marlenet15
                wrote on 20 Jan 2016, 19:31 last edited by
                #7

                @mrjj is it even plausible to make the timestamp uneditable text and the rest can be editable. That way I don't add a QLabel inside the QLineEdit?

                M 1 Reply Last reply 20 Jan 2016, 19:33
                0
                • M marlenet15
                  20 Jan 2016, 19:31

                  @mrjj is it even plausible to make the timestamp uneditable text and the rest can be editable. That way I don't add a QLabel inside the QLineEdit?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 20 Jan 2016, 19:33 last edited by mrjj
                  #8

                  @marlenet15
                  well maybe u can make inputmask do something like that.

                  Best solution would be custom widget but that is far more work.
                  Not even sure how to make it overlap.

                  M 1 Reply Last reply 20 Jan 2016, 19:42
                  0
                  • M mrjj
                    20 Jan 2016, 19:33

                    @marlenet15
                    well maybe u can make inputmask do something like that.

                    Best solution would be custom widget but that is far more work.
                    Not even sure how to make it overlap.

                    M Offline
                    M Offline
                    marlenet15
                    wrote on 20 Jan 2016, 19:42 last edited by
                    #9

                    @mrjj Do you have an idea how to make it work with an input mask?

                    M 1 Reply Last reply 20 Jan 2016, 19:50
                    0
                    • M marlenet15
                      20 Jan 2016, 19:42

                      @mrjj Do you have an idea how to make it work with an input mask?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 20 Jan 2016, 19:50 last edited by mrjj
                      #10

                      @marlenet15
                      yes using escape
                      \D\A\T\E:AAAAA-AAAAA-AAAAA-AAAAA-AAAAA
                      it shows as "Date:" and u cant change it.
                      so should be able to show an actual date.

                      M 1 Reply Last reply 20 Jan 2016, 20:01
                      0
                      • M mrjj
                        20 Jan 2016, 19:50

                        @marlenet15
                        yes using escape
                        \D\A\T\E:AAAAA-AAAAA-AAAAA-AAAAA-AAAAA
                        it shows as "Date:" and u cant change it.
                        so should be able to show an actual date.

                        M Offline
                        M Offline
                        marlenet15
                        wrote on 20 Jan 2016, 20:01 last edited by
                        #11

                        @mrjj So the actual text "Date:" can be changed to "Jan 19 2016" or any format of date?

                        M 1 Reply Last reply 20 Jan 2016, 20:04
                        0
                        • M marlenet15
                          20 Jan 2016, 20:01

                          @mrjj So the actual text "Date:" can be changed to "Jan 19 2016" or any format of date?

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 20 Jan 2016, 20:04 last edited by
                          #12

                          @marlenet15
                          yes
                          its not pretty but
                          \J\a\n\ \1\9\ \2\0\1\6:AAAAAA

                          does look correct.

                          R M 2 Replies Last reply 20 Jan 2016, 20:14
                          0
                          • M mrjj
                            20 Jan 2016, 20:04

                            @marlenet15
                            yes
                            its not pretty but
                            \J\a\n\ \1\9\ \2\0\1\6:AAAAAA

                            does look correct.

                            R Offline
                            R Offline
                            raven-worx
                            Moderators
                            wrote on 20 Jan 2016, 20:14 last edited by raven-worx
                            #13

                            i also think the input mask is actually the best way, or maybe even better a custom QValidator.
                            But to just add something to the (rather ugly) QLabel idea: You could use QLineEdit::setTextMargins() to move the left-most position of the cursor.

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            M 1 Reply Last reply 20 Jan 2016, 20:17
                            1
                            • M mrjj
                              20 Jan 2016, 20:04

                              @marlenet15
                              yes
                              its not pretty but
                              \J\a\n\ \1\9\ \2\0\1\6:AAAAAA

                              does look correct.

                              M Offline
                              M Offline
                              marlenet15
                              wrote on 20 Jan 2016, 20:15 last edited by marlenet15
                              #14

                              @mrjj So right now in my code the date that is going into the QLineedit is the current date. So I would need to separate the string and make sure to add \ ? Also what do the A's mean? the ones you put in your example?

                              1 Reply Last reply
                              0
                              • R raven-worx
                                20 Jan 2016, 20:14

                                i also think the input mask is actually the best way, or maybe even better a custom QValidator.
                                But to just add something to the (rather ugly) QLabel idea: You could use QLineEdit::setTextMargins() to move the left-most position of the cursor.

                                M Offline
                                M Offline
                                marlenet15
                                wrote on 20 Jan 2016, 20:17 last edited by
                                #15

                                @raven-worx yes I agree the input mask Is the best way to go. However, it's a new concept for me since I never heard of it. That is why I am very confused to how it works.

                                M 1 Reply Last reply 20 Jan 2016, 20:21
                                0
                                • M marlenet15
                                  20 Jan 2016, 20:17

                                  @raven-worx yes I agree the input mask Is the best way to go. However, it's a new concept for me since I never heard of it. That is why I am very confused to how it works.

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 20 Jan 2016, 20:21 last edited by
                                  #16

                                  @raven-worx said:
                                  setTextMargins
                                  +1 That would work for custom control.

                                  yes, you would need to put \ into your date.
                                  the AAAA is input specifier .
                                  Go to a LineEdit and find inputmask property and press F1 to read about them.

                                  M 1 Reply Last reply 20 Jan 2016, 20:24
                                  0
                                  • M mrjj
                                    20 Jan 2016, 20:21

                                    @raven-worx said:
                                    setTextMargins
                                    +1 That would work for custom control.

                                    yes, you would need to put \ into your date.
                                    the AAAA is input specifier .
                                    Go to a LineEdit and find inputmask property and press F1 to read about them.

                                    M Offline
                                    M Offline
                                    marlenet15
                                    wrote on 20 Jan 2016, 20:24 last edited by marlenet15
                                    #17

                                    @mrjj So if the user is allowed 100 characters for the QLineEdit do I have to put 100 A's?

                                    M R 2 Replies Last reply 20 Jan 2016, 20:26
                                    0
                                    • M marlenet15
                                      20 Jan 2016, 20:24

                                      @mrjj So if the user is allowed 100 characters for the QLineEdit do I have to put 100 A's?

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 20 Jan 2016, 20:26 last edited by mrjj
                                      #18

                                      @marlenet15 erhm, yes I think. maybe there is way
                                      but inputmasks are normally used for short input like date
                                      or iP etc

                                      1 Reply Last reply
                                      0
                                      • M marlenet15
                                        20 Jan 2016, 20:24

                                        @mrjj So if the user is allowed 100 characters for the QLineEdit do I have to put 100 A's?

                                        R Offline
                                        R Offline
                                        raven-worx
                                        Moderators
                                        wrote on 20 Jan 2016, 20:28 last edited by
                                        #19

                                        @marlenet15
                                        if you are familiar with regular expressions you might want to use QRegExpValidator which will result in the same behavior
                                        And set it on the line edit.

                                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                        If you have a question please use the forum so others can benefit from the solution in the future

                                        M 1 Reply Last reply 20 Jan 2016, 20:30
                                        1
                                        • R raven-worx
                                          20 Jan 2016, 20:28

                                          @marlenet15
                                          if you are familiar with regular expressions you might want to use QRegExpValidator which will result in the same behavior
                                          And set it on the line edit.

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 20 Jan 2016, 20:30 last edited by
                                          #20

                                          @raven-worx
                                          oh. So QRegExpValidator can have static text?

                                          R 1 Reply Last reply 20 Jan 2016, 20:31
                                          0

                                          7/32

                                          20 Jan 2016, 19:31

                                          topic:navigator.unread, 25
                                          • Login

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