add QLabel into QLineEdit



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


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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

    does look correct.


  • Moderators

    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.



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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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


  • Moderators

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


  • Qt Champions 2016

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


  • Moderators

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


  • Qt Champions 2016

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



  • @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 :)


  • Moderators

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


  • Qt Champions 2016

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



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



  • @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"));

  • Moderators

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


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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    @marlenet15
    No problem, I'm glad I could be of assistance.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.