แปลงข้อมูล ใน Qt
//แปลงเป็นฐานอะไรก็ได้
int x = 15;
QString xAsHex = QString("%1").arg(x, 0, 16);
qDebug() << "x = " << x << " = " << xAsHex;
// QString to int
QString num="123";
int n = num.toInt();
//Int to QString
int num2 = 123;
QString str= QString::number(num2);
>> http://www.qtforum.org/article/33749/convert-string-to-int-and-int-to-string.html
----------------------------------------------------------------------------------------
//qtstring to int
QString Abcd = "45";
int b = Abcd.split(" ")[0].toInt();//convert the first part to Int
Abcd.split(" ")[0].toDouble();//convert the first part to double
Abcd.split(" ")[0].toFloat();//convert the first part to double
qDebug() << b+50;
>> : http://stackoverflow.com/questions/16633555/how-to-convert-qtstring-to-int
---------------------------------------------------------------------------------------
//string to const char * (serial.write)
QString callA = ui->lineEdit_call_number->text();
QString callB = "ATD"+callA+";";
const char* callC = callB.toLatin1().data(); serial->write(callC);
QString callA = ui->lineEdit_call_number->text();
QString callB = "ATD"+callA+";";
const char* callC = callB.toLatin1().data(); serial->write(callC);
-------------------------------------------------------------------------------------------
//การใช้ Qbyte Array
QByteArray A;
A[0] = 0x1A;
serial->write(A);
------------------------------------------------------------------------------------------
//-------- int to binary
int data = ui->lineEdit_InttoBinary->text().toInt();
QString myStringOfBits( QString::number( data, 2 ) );
ui->lineEdit_InttoBinary2->setText(myStringOfBits);
-------------------------------------------------------------------------------------
//--- int to hex 2 byteint data = ui->lineEdit_Inttohex1->text().toInt();QString data2 = QString::number(data, 16);if(data2.size()<4){data2 = QString(4-data2.size(), '0')+data2;}ui->lineEdit_Inttohex2->setText(data2);------------------------------------------------------------------------------------//---- ASCII hex to int
QString data = ui->lineEdit_hextoint1->text();
bool bStatus = false;
uint nHex = data.toUInt(&bStatus,16);
QString data2 = QString::number(nHex);
ui->lineEdit_hextoint2->setText(data2);-----------------------------------------------------------------------------------// String to hexstatic inline QString embracedString( const QString & s ){return s.section( '(', 1 ).section( ')', 0, 0 );}// String to ByteArray เพื่อจะแยกข้อมูลทีละตัวQString data2 ="01f4";QByteArray a2 = data2.toUtf8();qDebug() << "a2[0]" << a2[0];qDebug() << "a2[1]" << a2[1];qDebug() << "a2[2]" << a2[2];qDebug() << "a2[3]" << a2[3];//------ Byte Array to Int
QByteArray x;x.resize(2);x[0] = 0x01;x[1] = 0x2A;int num = 0;num |= x[0];num << 8;num |= x[1];num << 8;qDebug() << "num = " << num;
// Random
#include <QGlobal.h>
#include <QTime>
int QMyClass::randInt(int low, int high)
{
// Random number between low and high
return qrand() % ((high + 1) - low) + low;
}
// Create seed for the random
// That is needed only once on application startup
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
// Get random value between 0-100
int randomValue = randInt(0,100);
>> http://eastfist.com/qt_tutorials/how-to-generate-random-number-in-qt
//---- Name User Test ------------------------------------------
ui->combobox_NameUserTest->addItem("สิทธิกร สุดหล่อ");
ui->combobox_NameUserTest->addItem("มานี ใจดี");
ui->combobox_NameUserTest->currentIndex();
//---- Date ---------------------------------------------------
QDate date = QDate::currentDate();
QString date2 = date.toString("dd/MM/yyyy");
ui->lineEdit_DateJob->setText(date2);
//---- Time ---------------------------------------------------
QTime time = QTime::currentTime();
QString time2 = time.toString("hh:mm:ss");
ui->lineEdit_TimeJob->setText(time2);
//--------- hex to int
bool ok;
QString data = ui->lineEdit_test2->text();
int data2 = data.toUInt(&ok,16);
qDebug()<< "data2 = " << data2;
//------------- int to string และ ส่งข้อมูลผ่าน serial เป็น cont char
void Dialog::on_blue_slider_valueChanged(int value)
{
ui->blue_value_label->setText(QString("<span style=\" font-size:18pt; font-weight:600;\">%1</span>").arg(value));
Dialog::updateRGB(QString("b%1").arg(value));
qDebug() << value;
}
void Dialog::updateRGB(QString command)
{
if (arduino->isWritable())
{
arduino->write(command.toStdString().c_str());
}
else
{
qDebug() << "Couldn't write to serial!";
}
}
------------------------------
สรุปสั้นๆ string to cons char* ส่งผ่าน serial
QString datas = "[{\"id\":1,\"status\":1,\"r\":255,\"g\":0,\"b\":0}]";
arduino->write(datas.toStdString().c_str());
-----------------------------------
QString แสดงตัวเลขฐานสิบหก เอามาแยกทีละตัว แล้วแปลงเป็น Int
void MainWindow::on_pushButton_clicked()
{
ui->logstext->insertPlainText("0103BE000100020003000400050006000700080009000a000b000c0001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100000001000100010001000100010001000159E3");
QString data = ui->logstext->toPlainText(); qDebug() << "data = " << data;
QString str[2400] ;
for(int i=0; i<data.length();i++)
{
str[i] = data.at(i);
qDebug() << "str " << i << " = " << str[i];
}
QString str2[2400];
for(int i=8; i<60 ;i+=4)
{
str2[i] = str[i]+str[i+1];
qDebug() << "str2 " << i << " = " << str2[i];
}
int str3[400];
for(int i=8; i<60 ;i+=4)
{
str3[i] = strHexotInt(str2[i]);
qDebug() << "str3 " << i << " = " << str3[i];
}
}
int MainWindow::strHexotInt(QString data)
{
bool ok;
int a = data.toInt(&ok,16); //qDebug() << "a =" <<a;
ui->logstext_2->clear();
ui->logstext_2->insertPlainText(QString::number(a));
return a;
}
-------------------------------------------------------------
String to ByeArray and ByteAraay to String
QString a = "test";
QByteArray a1 = a.toUtf8();
QString backToString = a1;
ถ้าเอาง่ายๆ อ่านข้อมูลมาจาก serial ที่เป็น ByteArray
โดยยกตัวอย่าง คือ ByteAraay data;
QString s_data = data;
qDebug() << " BiG = " << s_data;
หรือ ByteAraay data = "123456";
QString s_data = data;
คำตอบคือ 123456
-----------------------------
QString hex;
//Convert the to a hex string
int nFirst = List.at(4).toUInt();
int nSecond = List.at(5).toUInt();
hex.append(QString("%1").arg(QString().setNum(nFirst,16),2,'0'));
hex.append(QString("%1").arg(QString().setNum(nSecond,16),2,'0'));
//convert the hex string into the port
bool ok;
mPort = hex.toUInt(&ok,16);
------------------------String to float----------------
String line = 1.23456;
bool ok = true;
float line_float = line.toFloat(&ok);
------------------------float to string cat define decimal----------------
ui->label_light->setText(QString::number(line_float,'f',1));