Qt ComboBox->addItem() Integer to Qstring conversion error? - qt

I am really new to Qt and I have a little question for you. I am trying to work on ComboBox and when I add items to a combobox an integer like;
combobox->addItem(class.value); // class.value is an integer
It just adds a symbol to the combobox (*, / or ? )
How can I solve this little problem ?

Try combobox->addItem(QString::number(class.value));

Use QVariant . Advantage of using QVariant over QString::number() is you can convert data of any type to any other type.
int to string
QVariant(32).toString(); //assuming calss.value to be int
in your case it will be
combobox->addItem(QVariant(class.value).toString());
float to a string
QVariant(3.2).toString();
string to a float:
QVariant("5.2").toFloat();
it is that easy.

Related

How to read numbers; How to store those numbers in variable; and how to display those numbers in a label; from line edit in Qt?

I have to read numbers from line edit in Qt Creator, and then divide them by 100 and display them in a label.
The only method I know is:
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
ui->label_OOP_marks->setText(QString(OOP_marks));
But the above method cannot read numbers; it reads string. I have tried a lot but can't figure out the code for this part of the program.
You can use QString::toDouble for converting string to number. If you are planning to accept integers use QString::toInt instead. To convert number to string again QString::number is way to go.
const auto& OOP_marks = ui->lineEdit_OOP_marks_input->text();
bool isNumber; // optional
const double number = OOP_marks.toDouble(&isNumber);
const auto& result = isNumber ? QString::number(number / 100) : QString("Please enter valid number.");
ui->label_OOP_marks->setText(result);
But I suggest you to use QSpinBox or QDoubleSpinBox, instead of QLineEdit.
How about QString toInt function, and then static QString::number function?
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
int OOP_marks_val = OOP_marks.toInt();
ui->label_OOP_marks->setText(QString::number(OOP_marks_val/100));

Problems in converting to UTF-8 in Qt

I try to show a persian string in Qt:
QMessageBox msg;
QString str = "یا حسین";
msg.setText(QString::fromUtf8(str));
msg.exec();
but it shows the following error :
/home/msi/Desktop/VoMail
Project/Project/VoMail-build-desktop-Qt_4_8_1_in_PATH__System__Release/../VoMail/mainwindow.cpp:40:
error: no matching function for call to 'QString::fromUtf8(QString&)'
I want to use a string variable, and not a string directly.
How can I convert a QString variable to Utf8?
As seen here, QString::fromUtf8() does not accept an argument of type QString. You must give it a const char *, so you could rewrite it like this:
QMessageBox msg;
QString str = QString::fromUtf8("یا حسین");
msg.setText(str);
msg.exec();
its not good idea write like that
using this must be better
QString str(tr("ya hossein");
and use linguist and add persian translation file to your project http://qt-project.org/doc/qt-4.8/linguist-translators.html
and if you dont want use this, you must be sure your IDE or code editor (like qtcreator) use utf8 for saving files and just use
QString str("یا حسین");
it must be ok, i tested that so many times

Convert TCHAR* to QString

How to convert easiest way in Qt?
int recordSize = 1000;
TCHAR* qRecord = new TCHAR[recordSize];
//here I get data form other function
//here I try to display
qString() << QString::fromWCharArray(qRecord,recordSize);//gives many ????
printf("%s",qRecord); // this work perfectly
I tried with wcstombs, formStdWString nad other but nothing seems to work.
Thanks for any help
QString s= (LPSTR)qRecord;
worked.
thanks
#kajojeq
no, your second answer is not correct. because when the encoding is set to utf16(or even utf8 sometimes) the s variable only save one character.
correct conversion is:
QString str = QString::fromWCharArray(qrecord)

Convert string to int in xcode

I have two text fields.I need to convert text values into int. So i can add two values and display in result text field.Somebody please help me to convert string to integer.
You don't specify whether you're using iOS or OSX, which doesn't help answer the question.
Regardless, if you're using OSX, then NSTextField derives from NSControl which provides an intValue method:
int number = [myTextField intValue];
(reference).
And if you're using iOS then UITextField provides its value via the text property (NSString) which provides an intValue method:
int number = [[myTextField text] intValue];
(reference).
There are variations of these methods which also provide their value as float, double, etc.
int *vla = (int)[string number here];

How to convert TBuf8 to QString

I've tried to convert using the following code:
template< unsigned int size >
static QString
TBuf82QString( const TBuf8< size > &buf )
{
return QString::fromUtf16(
reinterpret_cast<unsigned short*>(
const_cast<TUint8*>(
buf.Ptr() ) ), buf.Length() );
}
But It always returns something like ?????b.
EDIT: Changed code example
Using a template probably isn't a good solution, since it will result in a new instantiation of this block of code within your application binary, for every size of input string which is converted. Since the output type (QString) contains no compile-time constant, this means you end up with code bloat, for no gain.
A better approach would be to leverage the fact that TBuf8<N> inherits from TDesC8:
QString TBuf2QString(const TDesC8 &buf)
{
return QString::fromLocal8Bit(reinterpret_cast<const char *>(buf.Ptr()),
buf.Length());
}
TBuf<16> foo(_L("sometext"));
QString bar = TBuf2QString(foo);
TBuf8 is used for binary data or non-Unicode strings. TBuf16 is used for Unicode strings. TBuf is conditionally compiled and will always be TBuf16 as Symbian OS is natively Unicode.
Try using QString::fromLocal8Bit() with TBuf8::Ptr()

Resources