Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to concatenate strings+ int in messageBox qt c++?
Forum Updated to NodeBB v4.3 + New Features

How to concatenate strings+ int in messageBox qt c++?

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
17 Posts 3 Posters 1.9k 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.
  • R RuWex

    Hi:)
    I want to concatenate for Example : "Hello"+ 2 + "World"
    I tried to do it:
    int x=2;
    msgBox.setText("Hello" + x + "World"); // its just for the example
    but its invalid.
    how can I do it?

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #2

    @RuWex
    There is no such thing as "qt c++". C++ is a language, your error in your attempt is because C++ dows not allow strings and numbers to be added together with +. Qt is just a framework/library written in C++.

    Have a look at Qt QString methods, such as
    QString QString::number(int n, int base = 10)
    QString QString::arg(const QString &a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
    (For your case above I would use the second one, QString::arg().)

    1 Reply Last reply
    5
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #3

      See the QString documentation - either QString::number() or QString::arg(). And if you want to make your application translateable than take a look at QObject::tr()

      /JonB was faster :)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • R Offline
        R Offline
        RuWex
        wrote on last edited by
        #4

        thank you:)
        but now I have a problem,
        because,
        I wanted that not all of the numbers appear, for example just numbers more than 1000 and the other more than 2000...
        so I did many many if and paste every time to the debbuger, but now I want it to appear in messageBox once...
        do you have any idea how to solve it?

        JonBJ 1 Reply Last reply
        0
        • R RuWex

          thank you:)
          but now I have a problem,
          because,
          I wanted that not all of the numbers appear, for example just numbers more than 1000 and the other more than 2000...
          so I did many many if and paste every time to the debbuger, but now I want it to appear in messageBox once...
          do you have any idea how to solve it?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #5

          @RuWex
          I don't think anyone can understand what you are asking. If you need an if then write one. If you want a function to not print certain numbers so that you can re-use it, write one.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RuWex
            wrote on last edited by
            #6

            I did this function : (you see all the if?) I did what you said its work well! but |I want to paste like I paste in the debugger

            
            bool PrintTimeRemainig(int second)
            {
                int days, hours, minutes;
                minutes=60;
                hours=minutes*60;
                days= hours*24;
                int remainingDays=0, remainingHours=0, remainingMinute=0;
                if(second>days)
                {
                    remainingDays= second/days;
                    second= second%days;
                    qDebug()<<"Remaing days:"<< remainingDays<<endl;
            
                }
                if (second>hours)
                {
                    remainingHours= second/hours;
                    second= second%hours;
                    qDebug()<<"Remaing hours:"<< remainingHours<<endl;
            
                }
                if(second>minutes)
                {
                    remainingMinute= second/minutes;
                    second= second%minutes;
                    qDebug()<<"Remaing minutes:"<< remainingMinute<<endl;
            
                }
                qDebug()<<"Remaing seconds:"<< second<<endl;
               
            
                QString text= QString("Estimated time to complete the test %1 days, %2 hours, %3 minutes, %4 seconds").arg(remainingDays).arg(remainingHours).arg(remainingMinute).arg(second);
            
                QMessageBox msgBox;
                msgBox.setText(text);
                msgBox.setStandardButtons(msgBox.Ok| msgBox.Abort);
               int ret=msgBox.exec();
               switch (ret) {
                    case msgBox.Ok:
                   {
                       return true;
                   }
                    case msgBox.Abort:
                   {
                       return false;
                   }
            
            
               }
                return false;
            }
            
            JonBJ 1 Reply Last reply
            0
            • R RuWex

              I did this function : (you see all the if?) I did what you said its work well! but |I want to paste like I paste in the debugger

              
              bool PrintTimeRemainig(int second)
              {
                  int days, hours, minutes;
                  minutes=60;
                  hours=minutes*60;
                  days= hours*24;
                  int remainingDays=0, remainingHours=0, remainingMinute=0;
                  if(second>days)
                  {
                      remainingDays= second/days;
                      second= second%days;
                      qDebug()<<"Remaing days:"<< remainingDays<<endl;
              
                  }
                  if (second>hours)
                  {
                      remainingHours= second/hours;
                      second= second%hours;
                      qDebug()<<"Remaing hours:"<< remainingHours<<endl;
              
                  }
                  if(second>minutes)
                  {
                      remainingMinute= second/minutes;
                      second= second%minutes;
                      qDebug()<<"Remaing minutes:"<< remainingMinute<<endl;
              
                  }
                  qDebug()<<"Remaing seconds:"<< second<<endl;
                 
              
                  QString text= QString("Estimated time to complete the test %1 days, %2 hours, %3 minutes, %4 seconds").arg(remainingDays).arg(remainingHours).arg(remainingMinute).arg(second);
              
                  QMessageBox msgBox;
                  msgBox.setText(text);
                  msgBox.setStandardButtons(msgBox.Ok| msgBox.Abort);
                 int ret=msgBox.exec();
                 switch (ret) {
                      case msgBox.Ok:
                     {
                         return true;
                     }
                      case msgBox.Abort:
                     {
                         return false;
                     }
              
              
                 }
                  return false;
              }
              
              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #7

              @RuWex said in How to concatenate strings+ int in messageBox qt c++?:

              but |I want to paste like I paste in the debugger

              Sorry, no idea what this means. You do not "paste in the debugger", the debugger is for debugging not for editing. And copy-paste works anywhere.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RuWex
                wrote on last edited by
                #8

                yes, but i just wanted to see if uts work well...
                anyway,
                do you have any idea what can i do??
                if I dont want to paste on the messageBox just what that standing on some condition?

                JonBJ 1 Reply Last reply
                0
                • R RuWex

                  yes, but i just wanted to see if uts work well...
                  anyway,
                  do you have any idea what can i do??
                  if I dont want to paste on the messageBox just what that standing on some condition?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #9

                  @RuWex said in How to concatenate strings+ int in messageBox qt c++?:

                  do you have any idea what can i do??

                  No because as I said I have no idea what you are asking.

                  if I dont want to paste on the messageBox just what that standing on some condition?

                  Nor do I know what this means.

                  I will leave you to see if anyone else answers.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    RuWex
                    wrote on last edited by
                    #10

                    @JonB said in How to concatenate strings+ int in messageBox qt c++?:

                    Nor do I know what this means.
                    I will leave you to see if anyone else answers.

                    Sorry, I'll try to explain myself more clearly, I want to send a message to the user in the messageBox, but I don't want all the %X to be displayed, but only those that meet a certain condition. Does anyone have an idea how to do this?

                    JonBJ 1 Reply Last reply
                    0
                    • R RuWex

                      @JonB said in How to concatenate strings+ int in messageBox qt c++?:

                      Nor do I know what this means.
                      I will leave you to see if anyone else answers.

                      Sorry, I'll try to explain myself more clearly, I want to send a message to the user in the messageBox, but I don't want all the %X to be displayed, but only those that meet a certain condition. Does anyone have an idea how to do this?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #11

                      @RuWex
                      Instead of your current

                      QString text= QString("Estimated time to complete the test %1 days, %2 hours, %3 minutes, %4 seconds").arg(remainingDays).arg(remainingHours).arg(remainingMinute).arg(second);

                      Instead of this fixed string with its %...s and matching arg()s, create a different string with arguments according to your if condition. Or build it up one bit at a time.

                      Maybe you have in mind something like this:

                      QString text= QString("Estimated time to complete the test: ");
                      if (remainingDays > 0)
                        text.append(QString("%1 days, ").arg(remainingDays));
                      if (remainingHours > 0)
                        text.append(QString("%1 hours, ").arg(remainingHours));
                      // and so on
                      
                      Christian EhrlicherC 1 Reply Last reply
                      4
                      • R Offline
                        R Offline
                        RuWex
                        wrote on last edited by
                        #12

                        ou!
                        Thanks, this is exactly what I wanted!!

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @RuWex
                          Instead of your current

                          QString text= QString("Estimated time to complete the test %1 days, %2 hours, %3 minutes, %4 seconds").arg(remainingDays).arg(remainingHours).arg(remainingMinute).arg(second);

                          Instead of this fixed string with its %...s and matching arg()s, create a different string with arguments according to your if condition. Or build it up one bit at a time.

                          Maybe you have in mind something like this:

                          QString text= QString("Estimated time to complete the test: ");
                          if (remainingDays > 0)
                            text.append(QString("%1 days, ").arg(remainingDays));
                          if (remainingHours > 0)
                            text.append(QString("%1 hours, ").arg(remainingHours));
                          // and so on
                          
                          Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @JonB said in How to concatenate strings+ int in messageBox qt c++?:

                          QString text= QString("Estimated time to complete the test: ");

                          QString text= tr("Estimated time to complete the test: %n day(s)").arg(remainingDays);

                          ;)

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          JonBJ 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            @JonB said in How to concatenate strings+ int in messageBox qt c++?:

                            QString text= QString("Estimated time to complete the test: ");

                            QString text= tr("Estimated time to complete the test: %n day(s)").arg(remainingDays);

                            ;)

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #14

                            @Christian-Ehrlicher
                            I don't follow?! I don't think you're pointing out the tr(), I don't know what your %n is about, and OP wants the Estimated time .... at the start unconditionally....
                            I think you're trying a piece of humor that is too droll/subtle for me to grasp?

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @Christian-Ehrlicher
                              I don't follow?! I don't think you're pointing out the tr(), I don't know what your %n is about, and OP wants the Estimated time .... at the start unconditionally....
                              I think you're trying a piece of humor that is too droll/subtle for me to grasp?

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @JonB said in How to concatenate strings+ int in messageBox qt c++?:

                              I think you're trying a piece of humor that is too droll/subtle for me to grasp?

                              No, it's the correct way to translate a string for singular/plural - we discuessed this some days ago already.
                              See https://doc.qt.io/qt-6/i18n-source-translation.html#handling-plurals

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              JonBJ 1 Reply Last reply
                              3
                              • Christian EhrlicherC Christian Ehrlicher

                                @JonB said in How to concatenate strings+ int in messageBox qt c++?:

                                I think you're trying a piece of humor that is too droll/subtle for me to grasp?

                                No, it's the correct way to translate a string for singular/plural - we discuessed this some days ago already.
                                See https://doc.qt.io/qt-6/i18n-source-translation.html#handling-plurals

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #16

                                @Christian-Ehrlicher
                                Ohhhhhhhh.... Well I'm certainly not writing this for Polish/Russian!
                                My look up of QString::arg() did not show any %n?
                                And if we are going to be that fussy, will %n day(s) come out as 1 day [sic.] if n == 1??

                                Oh I see, it's in that link.

                                OK, @Christian-Ehrlicher, how about this then: I want the count in the message to be on sheep. yes, that's right, how many sheep something takes. How do I do that cross-language then?

                                %n sheep(s) --- wrong for English in plural
                                %n sheep --- wrong for French etc. in plural
                                

                                ? ;-)

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @Christian-Ehrlicher
                                  Ohhhhhhhh.... Well I'm certainly not writing this for Polish/Russian!
                                  My look up of QString::arg() did not show any %n?
                                  And if we are going to be that fussy, will %n day(s) come out as 1 day [sic.] if n == 1??

                                  Oh I see, it's in that link.

                                  OK, @Christian-Ehrlicher, how about this then: I want the count in the message to be on sheep. yes, that's right, how many sheep something takes. How do I do that cross-language then?

                                  %n sheep(s) --- wrong for English in plural
                                  %n sheep --- wrong for French etc. in plural
                                  

                                  ? ;-)

                                  Christian EhrlicherC Online
                                  Christian EhrlicherC Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  @JonB said in How to concatenate strings+ int in messageBox qt c++?:

                                  %n sheep(s) --- wrong for English in plural
                                  %n sheep --- wrong for French etc. in plural

                                  ? ;-)

                                  You have to enter two translations in linugist then (or more for other languages). But there is only one tr() call in your code.

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  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