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. Using \d \D \t \T in C++
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 8.0k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    gergnnud
    wrote on last edited by
    #1

    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 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gergnnud
        wrote on last edited by
        #3

        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?
        !http://www.eon-fx.com/downloads/code_overlays.png(code overlays)!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          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":https://github.com/robertknight/qt-mustache project

          Hope it helps

          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
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5
            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("projectname\n");
            
                printf( "`%s'\n", s.c_str() );
              }
            

            }
            @

            1. Hmm. I guess I would just take the \x part from the selection.
              BUT
              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.
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              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
              0
              • G Offline
                G Offline
                gergnnud
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  \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:
                  1:
                  define the format string being #replace1 #replace2 plaintext
                  tokens are separated by space
                  2:
                  split the format string into the tokens
                  3:
                  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 :)

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gergnnud
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Then QString and replace might be enough for your needs

                      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
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

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