วันอาทิตย์ที่ 5 กุมภาพันธ์ พ.ศ. 2560

QFile and Cutter String ( , )


#include <QString>
#include <QStringRef>
#include <QFile>
#include <QStringList>
#include <QRegExp>

QString vendor = "/home/pi/hino/hino/vendor.txt";
void MainWindow::on_btn_write_vendor_file_clicked()
{
       QFile vendorFile(vendor);
       if(!vendorFile.open(QFile::WriteOnly | QFile::Text))
       {
          qDebug() << "could not open file vendor for write";
           return;
       }

       QTextStream data(&vendorFile);

         QString data1 =  ui->lineEdit_vendor_file->text();
         QString data2 =   "," ;
         QString data3 =   ui->lineEdit_product_file->text();
         QString datas = +"," +data1+data2+data3;
       data << datas;
       vendorFile.flush();
       vendorFile.close();

}


void MainWindow::on_btn_read_vendor_file_clicked()
{
    QFile vendorFile(vendor);
    if(!vendorFile.open(QFile::ReadOnly | QFile::Text))
    {
        qDebug() << "could not open file vendor for Read";
        return;
    }

    QTextStream data(&vendorFile);

    QString datas = data.readAll(); // 1234 , 5678

    QRegExp tagExp(",");
    QStringList firstList = datas.split(tagExp);

    QString requiredPart1 = firstList.value(firstList.length()-2);
    QString requiredPart2 = firstList.value(firstList.length()-1);
     qDebug() <<  "  requiredPart1 = " << requiredPart1;
     qDebug() <<  "  requiredPart2 = " << requiredPart2;

     bool ok1,ok2;
     serial_uno_vendor_id  = requiredPart1.toInt(&ok1,10);
     serial_uno_product_id = requiredPart2.toInt(&ok2,10);

 ui->lineEdit_vendor_file->setText( requiredPart1);
 ui->lineEdit_product_file->setText(requiredPart2);

    vendorFile.flush();

    vendorFile.close();

}



 void rl_write_file::WriteFile(QString data)
{
    qDebug() << "WriteFile";

    QFile gpsLog(pathFile);
    //if(!gpsLog.open(QFile::WriteOnly | QFile::Text))
    if (!gpsLog.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        qDebug() << "could not open file vendor for write";
        return;
    }
    QTextStream save(&gpsLog);

    save << data;
    qDebug() << data;
    gpsLog.flush();
    gpsLog.close();


}








Dialog
==============================


void checkconcurrent::on_btn_export_clicked()
{
    QString textData;
    int rows = model->rowCount();
    int columns = model->columnCount();

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {

                textData += model->data(model->index(i,j)).toString();
                textData += ", " ;     // for .csv file format
        }
        textData += "\n";             // (optional: for new line segmentation)
    }

    // [Save to file] (header file <QFile> needed)
    // .csv
     QString fileName = QFileDialog::getSaveFileName(this, tr("Save Address Book"), "",  tr("Address Book (*.csv);;All Files (*)"));
 
     QFile csvFile(fileName);
    if(csvFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {

        QTextStream out(&csvFile);
        out << textData; 
        csvFile.close();
    } 
}








0 ความคิดเห็น:

แสดงความคิดเห็น

สมัครสมาชิก ส่งความคิดเห็น [Atom]

<< หน้าแรก