Using \d \D \t \T in C++

  • I am very new to any sort of coding. Good at Filemaker and HTML but...

    I am referencing a program written in Basic I believe where a drop-down list of codes is used to overlay specific information on an image. Selecting a line in the drop-down appends to the end of a field the '\d' part only. As an example:

    \d (prints date as mmm dd yy hh:mm:ss )
    \D (prints date as dd/mm/yy hh:mm )
    \t (prints date as hh:mm:ss mmm-dd-yy )
    \T (prints date as mmm dd yy hh:mm:ss )

    There are more codes, some with multiple letters but you get the picture. The field can also have any text in it and the codes/text are separated by a space.

    1. Does this make sense to anybody? I can't find any reference to '\d' meaning a date so I'm guessing it must be setup as a variable \d=(Date + %mmmm %yy etc.)
    2. Any Ideas on how this might be implemented?
    3. I am using a comboBox to display the codes. How would I append a lineEdit field on an item selection to select only the code and not the description as well?

  • 1,2 take a look on QDateTime::toString()

  • Thanks andreyc but that is not what I am after. I need to explain more fully.

    The code list may contain all sorts of things beyond dates and times. For example:
    \C = Company
    \Do - domain
    \P = project
    \n = new line
    and so on.

    I am referencing other code that works this way so I don't know if it will work in Qt.

    The lineEdit field might look like "\C \P \d" where the end user has selected these 'symbols' from a drop down list.
    So I want the code to know that "\C \P \d" = Company field + Project field + DateAndTime and therefore print for example ABC Industries CBD Construction Jun 04 2014 15:16:30

    What I am most interested in is how the "" was implemented in the reference code because it is not the greatest character to prefix with.

    Does this make any sense?

    Maybe the attached sreenshot will help?
    ! overlays)!

  • Lifetime Qt Champion


    Backslashes in C/C++ represent escape sequence so you would need to represent them in your code using double backslashes e.g. \ represent one real \ in a string.

    Seems you are doing string template so you could be interested by the "qt-mustache": project

    Hope it helps

  • Qt Champions 2017

    1. Yes, list of tags that get replaced by text.
      in C++ one might have chosen something else than \ to mean
      "Text to replace" but I guess that basic is happy with it.

    2. Any Ideas on how this might be implemented?
      Do you mean how is made in the basic code or how one could make it in c++?
      this splits a string into tokens and reacts to
      "project" if seen. Just test code. Could be better.

    #include "sstream"
    #include "stdio.h"
    const std::string code_Project="\\p"; // NOTE! only 2 slashes. the forum adds extra ?
    void test() {

    std::istringstream overlaytext("\a text \p \cow ");
    std::string s;
    while ( getline( overlaytext, s, ' ' ) ) {

          if ( s == code_Project )
        printf( "`%s'\n", s.c_str() );


    1. Hmm. I guess I would just take the \x part from the selection.
      If you put all \x in a list and use that for the combo box. then you
      could use the index of the selected line, to just look up the \x token.
      If that makes sense to you.

  • Lifetime Qt Champion

    Since you are asking on a Qt forum, why not make use of QString and its replace method ?

  • Thanks everyone for the interest.

    I'm trying to ascertain best practice in something I'm very new to - C++

    So it has been done this way in my reference app but I thought I must be missing something because if \ represents escape it would only work with things like \n for new line.

    I understand using \ will work for \ in a string so it can be coded but that's still not the crux of it. I still feel I'm missing something important in a code procedure such as in my example.

    \P \C \d \t \n literally means
    escape+key+space+escape+key+space+escape+key+space and so on in a string where escape is being used to 'identify' a key. The reference app also allows for any text, anywhere in the string as long as this is separated by a space (see the screenshot). It seems that the escape+key part of the string identifies between a plain old text string and a param such as \P=Project.

    Is there a more appropriate way to do something like this in C++ or should I just keep going with identifying keys in the overall string using the \ character?

    I note that any other special characters (@#$%^* etc.) seem to have the same issues when trying to use them as an identifier for a key.

    Hope this makes sense.

  • Qt Champions 2017

    \n \r \p in an editbox is just text. its not escaped as such. just text.

    As I see it - here the \ is used to know the difference from plain text.
    Could as well have been # if not used for anything else in the input

    Maybe there is more to it that I understand from the screen shot
    but it seems to be:
    define the format string being #replace1 #replace2 plaintext
    tokens are separated by space
    split the format string into the tokens
    Generate a new string from the token list where each token is replaced by the actual text/info

    A more robust way would be as mr. SGaist suggest using a text template library.

    But if you only have like 12 \keys then it might be a bit involved compared to
    the "\1 \2" format. And if it worked for the basic application it will also work fine in c++ even if may not be the most clever object orientated way of doing it :)

  • Thank you all.

    I will continue on then. I understand it can be a \ or / or # that is used as the identifier. Anything that would not normally be in a plain text string.

    mrjj - you have understood the logic I have been trying to convey.

    SGaist - I will check out qt-mustache and try and understand a string template.

    Yes, only about 12 keys so, as long as I get it working, even if it is not clever.

  • Lifetime Qt Champion

    Then QString and replace might be enough for your needs

  • Qt Champions 2017

    Currently reading about core classes - so for fun, i tried to use QString as mr SGaist suggests and its very powerful.

    #include <QString>
    #include <QDateTime>
    #include <iostream>

    const QString tok_project="\\\\\\\\p"; <--change to 2 slashes. forum changes it..
    const QString tok_date1="\d";
    const QString tok_newline="\\\\\\\\n"; <--change to 2 slashes.

    QString str = "some \p started: \d \n mytext ";
    QDateTime date ( QDateTime::currentDateTime() );
    str.replace (tok_project, QString ( "someproject" ) );
    str.replace (tok_date1, date.toString ( "MMM dd hh:mm:ss" ) );
    str.replace (tok_newline , QString ( '\n' ) );
    std::cout << str.toStdString();

Log in to reply

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