Concatenate string to QString

  • Hi all,

    I need to concatenate the start date, start time with the end date, end time.

    For example: 2011/05/18 10:53:55.371;2012/09/22 10:57:32.168

    Can you help me?

  • Moderators


    Search "concat" in the "QString documentation":

  • Hi,

    I did this:

    // Retrieves, appends the Start Date and Start Time and converts it to standard
    CSPTDateTime objDateInitial;
    dateInitial += " " + hourInitial;
    objDateInitial.SetYMD(dateInitial, 2);

    //Retrieves, appends the End Date and End Time and converts it to standard
    CSPTDateTime objDateFinal;
    dateFinal += " " + hourFinal;
    objDateFinal.SetYMD(dateFinal, 2);

    I need to concatenate the variable QString m_LinhaComando at: dateInitial string, string hourInitial and dateEnd, HourEnd

  • Moderators

    Sorry, I don't understand your question.

    You already know how to concatenate dateInitial with hourInitial (and you concatenated dateFinal with hourfFinal). What's the problem?

    EDIT: Do you want to concatenate a std::string with a QString? Convert your std::string to a QString first. See the QString documentation for details.

  • Hi,

    Yes, I am concatenating the start date and start time as follows:

    dateInitial += " " + hourInitial;
    dateFinal += " " + hourFinal;

    But, I need to concatenate in one variable so that it looks like for example:

    m_LinhaComando = dateInitial += " " + hourInitial;dateFinal += " " + hourFinal;

    m_LinhaComando = 2011/05/18 10:53:55.371;2012/09/22 10:57:32.168

  • Moderators

    Use + instead of +=.

    += is the combination of concatenation and assignment.

    The 2 pieces of code below both do the same thing. (This is basic C++)
    // Technique #1:
    dateInitial += " " + hourInitial;
    // Technique #2:
    dateInitial = dateInitial + " " + hourInitial;

  • But this error occurs in this line:

    QString m_LinhaComando;

    Line: m_LinhaComando = dateInitial + " " + hourInitial;
    Error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

  • Moderators

    Like I said before, you need to convert dateInitial and hourInitial to QString first. See the "QString documentation": on how to convert std::string to QString.

  • First make sure all parts are QString:

    for numbers this is
    QString nr = QString::number(15);

    for other types see the many constructors / static methods in QString.

    Than use the "append" function of QString.

    But I know if your used to java or c# this is a pain in the ...

Log in to reply