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 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
                    • M mrjj
                      20 Jan 2016, 20:30

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

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

                      @mrjj
                      Of course, a regular expression can have a static text included

                      --- 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 M 2 Replies Last reply 20 Jan 2016, 20:33
                      0
                      • R raven-worx
                        20 Jan 2016, 20:31

                        @mrjj
                        Of course, a regular expression can have a static text included

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

                        @raven-worx
                        yes, but i didn't know it would show in lineEdit as Validator sounds so none visual :)

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

                          @mrjj
                          Of course, a regular expression can have a static text included

                          M Offline
                          M Offline
                          marlenet15
                          wrote on 20 Jan 2016, 21:00 last edited by
                          #23

                          @raven-worx Can you please show me an example of that? I have never heard of that and I googled it and I didn't find anything about that :)

                          R 1 Reply Last reply 21 Jan 2016, 07:11
                          0
                          • M marlenet15
                            20 Jan 2016, 21:00

                            @raven-worx Can you please show me an example of that? I have never heard of that and I googled it and I didn't find anything about that :)

                            R Offline
                            R Offline
                            raven-worx
                            Moderators
                            wrote on 21 Jan 2016, 07:11 last edited by
                            #24

                            @marlenet15
                            you can see how to use QRegExpValidator in the link i've posted.
                            Use a RegExp similiar to this one. There are tons of material on the web about regexp to learn.

                            QRegExp("Comments\\: ([Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Nov|Dec] [0-3]?[0-9] [0-9]{4}\\: (.+))");
                            

                            This regexp is surely not perfect, but enough to get an idea and continue to learn the syntax i guess.

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

                            K M 2 Replies Last reply 21 Jan 2016, 07:20
                            0
                            • R raven-worx
                              21 Jan 2016, 07:11

                              @marlenet15
                              you can see how to use QRegExpValidator in the link i've posted.
                              Use a RegExp similiar to this one. There are tons of material on the web about regexp to learn.

                              QRegExp("Comments\\: ([Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Nov|Dec] [0-3]?[0-9] [0-9]{4}\\: (.+))");
                              

                              This regexp is surely not perfect, but enough to get an idea and continue to learn the syntax i guess.

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 21 Jan 2016, 07:20 last edited by
                              #25

                              @raven-worx
                              If I may interject, [Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Nov|Dec] is not okay for matching one should use a (non-capturing) group instead. i.e: (?:Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Nov|Dec).

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              0
                              • V Offline
                                V Offline
                                ValentinMichelet
                                wrote on 21 Jan 2016, 08:49 last edited by
                                #26

                                My two cents: if you have Qt5, consider using QRegularExpressionValidator and QRegularExpression instead, since the motor has been remarkably improved.

                                1 Reply Last reply
                                0
                                • R raven-worx
                                  21 Jan 2016, 07:11

                                  @marlenet15
                                  you can see how to use QRegExpValidator in the link i've posted.
                                  Use a RegExp similiar to this one. There are tons of material on the web about regexp to learn.

                                  QRegExp("Comments\\: ([Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Nov|Dec] [0-3]?[0-9] [0-9]{4}\\: (.+))");
                                  

                                  This regexp is surely not perfect, but enough to get an idea and continue to learn the syntax i guess.

                                  M Offline
                                  M Offline
                                  marlenet15
                                  wrote on 21 Jan 2016, 16:53 last edited by marlenet15
                                  #27

                                  @raven-worx I just tried it and the amazing thing is the cursor moves automatically moves to the right of the date. However, I can still edit the date and I don't want it to be edited whatsoever. When the mouse clicks the QLineEdit, I want it so that the cursor starts right after the date text. :( Is it possible for this? Thank you so much for you help!

                                  This is the code I used:

                                  QRegExp rx("Comments\\: ([Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Nov|Dec] [0-3]?[0-9] [0-9]{4}\\: (.+))");
                                  QValidator *validator = new QRegExpValidator(rx, this);
                                  ui->lineEdit->setValidator(validator);
                                  
                                  QDate date = QDate::currentDate();
                                  QTime time = QTime::currentTime();
                                  ui->lineEdit->setText(date.toString("MMM dd yyyy")+" "+time.toString("hh:mm:ss"));
                                  
                                  R 1 Reply Last reply 21 Jan 2016, 21:14
                                  0
                                  • M marlenet15
                                    21 Jan 2016, 16:53

                                    @raven-worx I just tried it and the amazing thing is the cursor moves automatically moves to the right of the date. However, I can still edit the date and I don't want it to be edited whatsoever. When the mouse clicks the QLineEdit, I want it so that the cursor starts right after the date text. :( Is it possible for this? Thank you so much for you help!

                                    This is the code I used:

                                    QRegExp rx("Comments\\: ([Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Nov|Dec] [0-3]?[0-9] [0-9]{4}\\: (.+))");
                                    QValidator *validator = new QRegExpValidator(rx, this);
                                    ui->lineEdit->setValidator(validator);
                                    
                                    QDate date = QDate::currentDate();
                                    QTime time = QTime::currentTime();
                                    ui->lineEdit->setText(date.toString("MMM dd yyyy")+" "+time.toString("hh:mm:ss"));
                                    
                                    R Offline
                                    R Offline
                                    raven-worx
                                    Moderators
                                    wrote on 21 Jan 2016, 21:14 last edited by raven-worx
                                    #28

                                    @marlenet15

                                    Try this:

                                    QString dateTimeStr = QDateTime::currentDateTime().toString("MMM dd yyyy hh:mm:ss");
                                    QString rxStr("Comments\\: %1\\: (.+)");
                                    QRegExp rx( rxStr.arg(dateTimeStr), Qt::CaseSensitive, QRegExp::RegExp2 );
                                    QValidator *validator = new QRegExpValidator(rx, this);
                                    ui->lineEdit->setValidator(validator);
                                    

                                    --- 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 21 Jan 2016, 21:42
                                    1
                                    • R raven-worx
                                      21 Jan 2016, 21:14

                                      @marlenet15

                                      Try this:

                                      QString dateTimeStr = QDateTime::currentDateTime().toString("MMM dd yyyy hh:mm:ss");
                                      QString rxStr("Comments\\: %1\\: (.+)");
                                      QRegExp rx( rxStr.arg(dateTimeStr), Qt::CaseSensitive, QRegExp::RegExp2 );
                                      QValidator *validator = new QRegExpValidator(rx, this);
                                      ui->lineEdit->setValidator(validator);
                                      
                                      M Offline
                                      M Offline
                                      marlenet15
                                      wrote on 21 Jan 2016, 21:42 last edited by
                                      #29

                                      @raven-worx I can still edit the date_time stamp :(

                                      K 1 Reply Last reply 21 Jan 2016, 22:42
                                      0
                                      • M marlenet15
                                        21 Jan 2016, 21:42

                                        @raven-worx I can still edit the date_time stamp :(

                                        K Offline
                                        K Offline
                                        kshegunov
                                        Moderators
                                        wrote on 21 Jan 2016, 22:42 last edited by kshegunov
                                        #30

                                        @marlenet15
                                        Hello,
                                        I believe you are going to need an input mask after all. I think you should try something like this:

                                        QString date = QDateTime::currentDateTime().toString("MMM dd yyyy hh:mm:ss");
                                        QString heading = QStringLiteral("Comments %1: %2").arg(date); //< Construct the heading
                                        QRegularExpression escaperx(QStringLiteral("([AaNnXx09Dd#HhBb><!\\\\]{1})")); //< Escape regex for input mask special characters
                                        heading.replace(escaperx, QStringLiteral("\\\\1")); //< Escape the input mask
                                        
                                        QString mask = heading.arg("", 255, QLatin1Char('x')); //< Add 255 optional characters to the mask for the user to fill.
                                        _dateLabel->setInputMask(mask);
                                        

                                        Then if you wish you could add a validator to ensure the user has entered correct data.

                                        Kind regards.

                                        EDIT:
                                        I had forgotten to add the actual characters that the user can fill to the mask, but I edited the example and it should be correct now.

                                        Read and abide by the Qt Code of Conduct

                                        M 1 Reply Last reply 21 Jan 2016, 23:32
                                        2
                                        • K kshegunov
                                          21 Jan 2016, 22:42

                                          @marlenet15
                                          Hello,
                                          I believe you are going to need an input mask after all. I think you should try something like this:

                                          QString date = QDateTime::currentDateTime().toString("MMM dd yyyy hh:mm:ss");
                                          QString heading = QStringLiteral("Comments %1: %2").arg(date); //< Construct the heading
                                          QRegularExpression escaperx(QStringLiteral("([AaNnXx09Dd#HhBb><!\\\\]{1})")); //< Escape regex for input mask special characters
                                          heading.replace(escaperx, QStringLiteral("\\\\1")); //< Escape the input mask
                                          
                                          QString mask = heading.arg("", 255, QLatin1Char('x')); //< Add 255 optional characters to the mask for the user to fill.
                                          _dateLabel->setInputMask(mask);
                                          

                                          Then if you wish you could add a validator to ensure the user has entered correct data.

                                          Kind regards.

                                          EDIT:
                                          I had forgotten to add the actual characters that the user can fill to the mask, but I edited the example and it should be correct now.

                                          M Offline
                                          M Offline
                                          marlenet15
                                          wrote on 21 Jan 2016, 23:32 last edited by
                                          #31

                                          @kshegunov it works thank you very much and to everyone!

                                          K 1 Reply Last reply 22 Jan 2016, 10:06
                                          0

                                          21/32

                                          20 Jan 2016, 20:31

                                          • Login

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