วันอาทิตย์ที่ 8 พฤศจิกายน พ.ศ. 2558

qt thread

https://youtu.be/JaGqGhRW5Ks
https://www.youtube.com/watch?v=yazMHbIew0Q
https://www.youtube.com/watch?v=yazMHbIew0Q&t=444s

วันจันทร์ที่ 19 ตุลาคม พ.ศ. 2558

วันศุกร์ที่ 2 ตุลาคม พ.ศ. 2558

ต่อข้อมูล string (qt append string)

การเชื่อมต่อ string เป็นเรื่องยากสำหรับผมมาก เพราะว่าหาตัวอย่างยังไงก็หาไม่เจอ
จริงๆแล้วมันไม่ได้ยากหรอก แต่เราใช้คำในการค้นหา ไม่ตรงกับความต้องการ(อีกแล้ว)

การเพิ่มข้อมูล string หรือการต่อข้อมูล string (Append string) สามารถทำได้ดังนี้

#include <QDebug>
#include <QString>
.
.
.
QString your_name = "Niran";
QString message = "Your name is " + your_name;
qDebug() << message;

หรือ
QString message = "Your name is ";
message += your_name;
qDebug() << message;

หรือ
QString message = "Your name is ";
message.append(your_name);
qDebug() << message;

ที่มา
http://www.qtcentre.org/threads/33603-how-to-concatenate-a-variable-with-a-string

convert int to string

วันนี้เขียนโปรแกรม ที่จำเป็นต้อง convert จาก int เป็น string
ตอนแรกก็งงอยู่พักใหญ่ ทำยังไงหว่า เลยถาม google เหมือนเดิม
แต่ถามครั้งแรกมันแสดงผลไรมาไม่รู้เยอะแยะ ไม่ตรงกับที่เราต้องการ
ก็ต้องเปลี่ยนคำถามใหม่ (สอนให้รู้ว่า ต้องตั้งคำถามให้ตรงคำตอบ) ก็จะได้คำตอบที่ต้องการ 555

วิธีเปลี่ยนจาก int เป็น string คือ
int i=42;
QString s = QString::number(i);

ที่มา
http://stackoverflow.com/questions/3211771/how-to-convert-int-to-qstring

วันพฤหัสบดีที่ 17 กันยายน พ.ศ. 2558

qt database ตัวอย่าง 2

ก่อนเขียน code ต้องแก้ไข .pro ด้วยนะ
QT += sql




#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql>
#include <QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("/home/kong/test-console-application/test");
    if (!db.open()){
        qDebug() << db.lastError() << endl;
    }
    else{
        qDebug()<<"open database" << endl;
    }

    qDebug() << db.tables();
    QSqlQuery query;
    query.exec("select * from hose");

    while(query.next()){
        int hose_id = query.value(0).toInt();
        qDebug() << "hose_id : " << hose_id << endl;
        int pump_id = query.value(1).toInt();
        qDebug() << "pump_id : " << pump_id << endl;
        int hose_number = query.value(2).toInt();
        qDebug() << "hose_number : " << hose_number << endl;
    }

    return a.exec();
}

qt sqlite ตัวอย่าง 1

ก่อนเขียน code ต้องแก้ไข .pro ด้วยนะ
QT += sql


#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    serial = new QSerialPort();
    serial->setPortName("ttyS0");

    if (!QSqlDatabase::drivers().contains("QSQLITE")){
        qDebug()<< "not support database" << endl;
    }
    else{
        qDebug() << "support QSQLITE"<< endl;
    }

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test");
    if (!db.open()){
        qDebug()<<"can't open database"<< endl;
    }
    else{
        qDebug()<<"open database" << endl;
    }

    return a.exec();
}

ลง sqlite3

sudo apt-get install sqlite3 libsqlite3.dev
หลังจากลงเสร็จแล้ว ให้พิมพ์ sqlite3 จะเห็นเวอร์ชัน 3.8.7.4
ถ้าต้องการออก ให้พิมพ์ .quit

ต่อมาเราต้องการ ui เพื่อให้ใช้งาน database ได้ง่ายๆ ก็ต้องลง sqlite man จาก software center