How to concatenate strings+ int in messageBox qt c++?
-
wrote on 29 Sept 2022, 09:03 last edited by
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? -
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?wrote on 29 Sept 2022, 09:22 last edited by JonB@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()
.) -
Lifetime Qt Championwrote on 29 Sept 2022, 09:23 last edited by Christian Ehrlicher
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 :)
-
wrote on 29 Sept 2022, 10:04 last edited by
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? -
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? -
wrote on 29 Sept 2022, 11:27 last edited by
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; }
-
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; }
wrote on 29 Sept 2022, 11:33 last edited by@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.
-
wrote on 29 Sept 2022, 11:36 last edited by
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? -
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?wrote on 29 Sept 2022, 11:37 last edited by@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.
-
wrote on 29 Sept 2022, 11:40 last edited by
@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?
-
@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?
wrote on 29 Sept 2022, 12:00 last edited by JonB@RuWex
Instead of your currentQString 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 matchingarg()
s, create a different string with arguments according to yourif
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
-
wrote on 29 Sept 2022, 12:12 last edited by
ou!
Thanks, this is exactly what I wanted!! -
@RuWex
Instead of your currentQString 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 matchingarg()
s, create a different string with arguments according to yourif
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
@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);
;)
-
@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);
;)
wrote on 29 Sept 2022, 12:28 last edited by JonB@Christian-Ehrlicher
I don't follow?! I don't think you're pointing out thetr()
, I don't know what your%n
is about, and OP wants theEstimated time ....
at the start unconditionally....
I think you're trying a piece of humor that is too droll/subtle for me to grasp? -
@Christian-Ehrlicher
I don't follow?! I don't think you're pointing out thetr()
, I don't know what your%n
is about, and OP wants theEstimated time ....
at the start unconditionally....
I think you're trying a piece of humor that is too droll/subtle for me to grasp?@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 -
@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-pluralswrote on 29 Sept 2022, 12:35 last edited by JonB@Christian-Ehrlicher
Ohhhhhhhh.... Well I'm certainly not writing this for Polish/Russian!
My look up ofQString::arg()
did not show any%n
?
And if we are going to be that fussy, will%n day(s)
come out as1 day
[sic.] ifn == 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-Ehrlicher
Ohhhhhhhh.... Well I'm certainly not writing this for Polish/Russian!
My look up ofQString::arg()
did not show any%n
?
And if we are going to be that fussy, will%n day(s)
come out as1 day
[sic.] ifn == 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
? ;-)
@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.
1/17