How to load the Integer value from txt file



  • I have added in a new connect , which is when LOAD button is clicked, i can get my the value from txt file which is from loadValue() function, but I could not obtain it,
    I did not get debug message from this function too,
    please advise where i did wrong,

    By the way, i would like to get Integer value only, i don't want the words.

    @#include "dialog.h"
    #include "ui_dialog.h"

    #include <QDebug>
    #include <QFile>
    #include <QTextStream>

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    timer = new QTimer(this);
    
    cam = cvCaptureFromCAM(-1);
    
    if(cam==NULL)
        qDebug()<<"error";
    
    timer->start(30);
    connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
    
    connect(ui->hueSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->hueSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->satSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->satSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->lumSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->lumSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->load,SIGNAL(clicked()),this,SLOT(loadValue()));
    

    }

    void Dialog::getFrame()
    {
    frame = cvQueryFrame(cam);
    QImage image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
    ui->original->setPixmap(QPixmap::fromImage(image));
    }

    void Dialog::prcFrame()
    {
    imgHSV= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
    cvCvtColor(frame,imgHSV,CV_BGR2HSV);

    //threshed , get value from slider value
    

    // QImage filterImg = QImage ((const uchar*)imgHSV->imageData,imgHSV->width,imgHSV->height,QImage::Format_RGB888);
    // ui->filter->setPixmap(QPixmap::fromImage(filterImg));
    }

    void Dialog::saveValue()
    {
    int h_slide1 = ui->hueSlide1->value();
    int h_slide2 = ui->hueSlide2->value();
    int s_slide1 = ui->satSlide1->value();
    int s_slide2 = ui->satSlide2->value();
    int l_slide1 = ui->lumSlide1->value();
    int l_slide2 = ui->lumSlide2->value();
    QFile valueHSV("/home/pi/valueHSV/hsv.txt");
    if(!valueHSV.open(QIODevice::WriteOnly|QIODevice::Text))
    {
    qDebug() << "cannot open file for writing"<<endl;
    return;
    }
    QTextStream hsv(&valueHSV);
    hsv << "<HUE LOW>" <<h_slide1<<endl;
    hsv << "<HUE HIGH>" <<h_slide2<<endl;
    hsv << "<SAT LOW>" <<s_slide1<<endl;
    hsv << "<SAT HIGH>" <<s_slide2<<endl;
    hsv << "<LUM LOW>" <<l_slide1<<endl;
    hsv << "<LUM HIGH>" <<l_slide2<<endl;
    //qDebug() << "H_low" << h_slide1;
    //qDebug() << "H_high" << h_slide2;
    //valueHSV.close();

    }

    void Dialog::loadValue()
    {
    QStringList list;
    QFile valueHSV("/home/pi/valueHSV/hsv.txt");
    if(!valueHSV.open(QIODevice::ReadOnly|QIODevice::Text))
    {
    qDebug() << "cannot open file for reading"<<endl;
    return;
    }
    QTextStream hsv(&valueHSV);
    while (!hsv.atEnd())
    {
    QString load=hsv.readLine();
    if(load.isNull())
    break;
    }
    foreach(QString str, list)
    {
    int h1 = str.toInt();
    qDebug() << "value"<<h1;
    }
    return;
    }

    Dialog::~Dialog()
    {
    timer->stop();
    cvReleaseCapture(&cam);

    delete ui;
    

    }@



  • It might help to post a shorter, more focused example. I say this both for readers and for the poster.

    From what I see, the Dialog::loadValue() attempts to parse integer values from the elements of the QStringList list using a foreach loop at line 94. Unfortunately, perhaps due to a typo in the example, list is never initialized. My guess is that the QString load from line 90 is supposed to be tokenized based on some separator. This is probably supposed to happen based on the result of the readLine() at line 90.



  • @connect(ui->hueSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->hueSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->satSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->satSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->lumSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->lumSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    @
    You didn't pass an integer parameter to each slot.



  • [quote author="IamSumit" date="1404286234"]@connect(ui->hueSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->hueSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->satSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->satSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->lumSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    connect(ui->lumSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    @
    You didn't pass an integer parameter to each slot.[/quote]

    That's actually ok in this case, although possibly suboptimal. A signal can be connected to a slot with a shorter signature. The slot Dialog::saveValue saves values for each of the 6 sliders each time any one of them is modified by accessing the sliders directly.



  • Hi all,

    Thanks for you all reply, and i appreciate

    @void Dialog::loadValue()
    {
    QStringList list;
    QFile valueHSV("/home/pi/valueHSV/hsv.txt");
    if(!valueHSV.open(QIODevice::ReadOnly|QIODevice::Text))
    {
    qDebug() << "cannot open file for reading"<<endl;
    return;
    }
    QTextStream hsv(&valueHSV);
    while (!hsv.atEnd())
    {
    QString load=hsv.readLine();
    if(load.isNull())
    break;
    list << load;
    }
    /* foreach(QString str, list)
    {
    int h1 = str.toInt();
    qDebug() << "value"<<h1;
    }*/
    return;
    }@

    I have hide the foreach loop,

    and pass the value to list in line16 ,

    for this situation, i was able to read all the thing inside txt file,

    the output that i read is
    @HSV ("<HUE LOW>48")

    HSV ("<HUE LOW>48", "<HUE HIGH>124")

    HSV ("<HUE LOW>48", "<HUE HIGH>124", "<SAT LOW>79")

    HSV ("<HUE LOW>48", "<HUE HIGH>124", "<SAT LOW>79", "<SAT HIGH>165")

    HSV ("<HUE LOW>48", "<HUE HIGH>124", "<SAT LOW>79", "<SAT HIGH>165", "<LUM LOW>63")

    HSV ("<HUE LOW>48", "<HUE HIGH>124", "<SAT LOW>79", "<SAT HIGH>165", "<LUM LOW>63", "<LUM HIGH>205") @

    I do not want to get the <HUE LOW>
    but only read the integer, and get my slider to the value position,
    is this possible?



  • after i change to QXmlStreamWriter,
    I can get a xml output, now i would like to load the save value from the xml file,
    i meet some problem.
    i could get QString convert back to int , what i do is like below, please advise
    @QXmlStreamReader hsv(&valueHSV);
    hsv.readNext();
    while(!hsv.atEnd())
    {
    if(hsv.isStartElement())
    {
    if(hsv.name()=="HSV")
    {
    if(hsv.name()=="HUE LOW")

                    ui->hueSlide1->setValue(QString::toInt(hsv.readElementText()));
            }
        }
    }@

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.