Hi all,
I would like to get my Qslider position value and store in a txt file. i did as following, my i could not obtain any value.
Code:
int h_slide1 = ui->hueSlide1->value(); return; hsv << h_slide1;
is this code correct?
Printable View
Hi all,
I would like to get my Qslider position value and store in a txt file. i did as following, my i could not obtain any value.
Code:
int h_slide1 = ui->hueSlide1->value(); return; hsv << h_slide1;
is this code correct?
That looks about right.
Did the QFile::open() work?
Cheers,
_
this can make file without error,
Will this store a value in a txt file that after i drag the slider? after i close the GUI,
i will can open the text file to see my slider value,
am i correct?
but the txt file is blank.
Regarding above title, i am doing a GUI to filter out the colour i do not want,
I would like to get value of H, S and V using slider and store these value inside a .txt file.
After that i would call these value into another function to processing and show a filter stream video,
I did use QFile and QTextStreamer, but could not get through.
Here is a photo of my GUI,
Attachment 10490
Please advise,
Value in file will be stored after the code execution. Question : how You activate this code ?
Hi,
I just simply build & Run it,
i create another thread here : http://www.qtcentre.org/threads/5964...-into-txt-file
Please do not create other threads, this only pollutes the forum. Focus on giving as much information as you can, so that we can help you effectively.
So far we have only seen a snippet of code, but we have no idea when it is executed. This may be dead code for all we know. Have you run a debugger and witnessed your snippet being executed? Another important question: what are the lifetimes of the QTextStream and QFile variables?
It would help if you posted the whole source of a minimal program reproducing the problem.
I am so sorry for that.
i am doing a GUI to filter out the colour i do not want,
I would like to get value of H, S and V using slider and store these value inside a .txt file.
After that i would call these value into another function to processing and show a filter stream video,
I did use QFile and QTextStreamer, but could not get through.
below is my code
Code:
//getCam.pro ###################################################################### # Automatically generated by qmake (2.01a) Tue Jun 24 17:53:36 2014 ###################################################################### CONFIG += console TEMPLATE = app TARGET = DEPENDPATH += . INCLUDEPATH += . # Input HEADERS += dialog.h FORMS += dialog.ui SOURCES += dialog.cpp main.cpp
Code:
//main.cpp #include <QApplication> #include "dialog.h" int main(int argc, char *argv[]) { Dialog w; w.show(); return a.exec(); }
Code:
//dialog.h #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include "opencv/cv.h" #include "opencv/highgui.h" #include <QTimer> #include <QPixmap> namespace Ui { class Dialog; } { Q_OBJECT public: ~Dialog(); private: Ui::Dialog *ui; CvCapture *cam; IplImage *frame,*imgHSV; QTimer *timer; private slots: void getFrame(); void prcFrame(); void getValue(); }; #endif // DIALOG_H
Code:
//dialog.cpp #include "dialog.h" #include "ui_dialog.h" #include <QDebug> #include <QFile> #include <QTextStream> ui(new Ui::Dialog) { ui->setupUi(this); cam = cvCaptureFromCAM(-1); if(cam==NULL) qDebug()<<"error"; timer->start(30); connect(timer,SIGNAL(timeout()),this,SLOT(getFrame())); } void Dialog::getFrame() { frame = cvQueryFrame(cam); } 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)); } [COLOR="#FFFF00"]void Dialog::getValue() { int h_slide1 = ui->hueSlide1->value(); { qDebug() << "cannot open file for writing"<<endl; return; } hsv <<h_slide1; qDebug() << "Value=" << h_slide1; valueHSV.close(); }[/COLOR] Dialog::~Dialog() { timer->stop(); cvReleaseCapture(&cam); delete ui; }
Code:
//compile output 04:45:50: Running steps for project getCam... 04:45:50: Configuration unchanged, skipping qmake step. 04:45:50: Starting: "/usr/bin/make" -w make: Entering directory `/home/pi/qt/getCam' /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ -o Makefile getCam.pro make: Leaving directory `/home/pi/qt/getCam' make: Entering directory `/home/pi/qt/getCam' g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv -o dialog.o dialog.cpp g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv -o main.o main.cpp /usr/bin/moc-qt4 -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv dialog.h -o moc_dialog.cpp g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv -o moc_dialog.o moc_dialog.cpp g++ -Wl,-O1 -o getCam dialog.o main.o moc_dialog.o -L/usr/lib/arm-linux-gnueabihf /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_imgproc.so -lQtGui -lQtCore -lpthread make: Leaving directory `/home/pi/qt/getCam' 04:47:35: The process "/usr/bin/make" exited normally.
and this is the pic of GUI design
Attachment 10491
Please advise
regards
YDYD
Could you point out where you call getValue()? I can't seem to find it.
Cheers,
_
Hi,
I didn't call getValue()...
By right, there should be a connect() in dialog.cpp, but i deleted,
and getValue() is to act as a SLOT?
I not really know how to use it....
Do i need another .cpp and .h file for "on_horizontalSlider_valueChanged()"?
Added after 45 minutes:
i just test my code using timer as my signal,
connect(timer,SIGNAL(timeout()),this,SLOT(getValue ()));
I was able to get Value and in text file too!
but, i would like to change only when slider is move, what should i put as SIGNAL?
Regards
YDYD
The slider has a valueChanged signal (declared in QAbstractSlider).
Code:
connect(ui->hueSlide1, SIGNAL(valueChanged(int)), this, SLOT(getValue()));
Cheers,
_
There is a problem at line 55 in dialog.cpp:
This closes the file even though the QTextStream hsv may still have bytes in its internal buffer that have not been written to the file. You can fix the issue by removing this line. When the function exits, the variables on the stack are destroyed in reverse order of construction. That means that hsv is destroyed before valueHSV. When hsv is destroyed, it flushes its buffer (see QTextStream::~QTextStream()), writing any unwritten bytes to the (still open) file. Then valueHSV is destroyed, and is closed and flushed to disk automatically in the process (see QFile::~QFile()).Code:
valueHSV.close();
Thank to anda_skoa and yeye_olive remind,
in my text file i get the value once i move the slider!
Code:
qDebug() << "Value=" << h_slide1;
in my text file only have value, what if i need "Value = XX" inside of my text file?
Is this achievable ?
Added after 12 minutes:
I have solved this myself, thank you all,
another question is, can i output as XML format?
my dialog.cpp end up like this..
Code:
#include "dialog.h" #include "ui_dialog.h" #include <QDebug> #include <QFile> #include <QTextStream> ui(new Ui::Dialog) { ui->setupUi(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())); } void Dialog::getFrame() { frame = cvQueryFrame(cam); } 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(); { qDebug() << "cannot open file for writing"<<endl; return; } 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(); } Dialog::~Dialog() { timer->stop(); cvReleaseCapture(&cam); delete ui; }
if i would like to read the value from the txt file and send back to the slider, is it possible?
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.
Code:
#include "dialog.h" #include "ui_dialog.h" #include <QDebug> #include <QFile> #include <QTextStream> ui(new Ui::Dialog) { ui->setupUi(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); } 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(); { qDebug() << "cannot open file for writing"<<endl; return; } 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; { qDebug() << "cannot open file for reading"<<endl; return; } while (!hsv.atEnd()) { if(load.isNull()) break; } { int h1 = str.toInt(); qDebug() << "value"<<h1; } return; } Dialog::~Dialog() { timer->stop(); cvReleaseCapture(&cam); delete ui; }
"list" is always empty, you never add anything to it. I guess you want to do that in the while loop:Code:
QStringList list; ... TextStream hsv(&valueHSV); while (!hsv.atEnd()) { if(load.isNull()) break; } { ....
Code:
TextStream hsv(&valueHSV); while (!hsv.atEnd()) { if(load.isNull()) break; list << load; }
Code:
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(); { qDebug() << "cannot open file for writing"<<endl; return; } QXmlStreamWriter hsv(&valueHSV); hsv.setAutoFormatting(true); hsv.writeStartDocument(); hsv.writeStartElement("HSV"); hsv.writeAttribute("value","hsv"); hsv.writeTextElement("HUE LOW",h_slide1); hsv.writeTextElement("HUE HIGH",h_slide2); hsv.writeTextElement("SAT LOW",s_slide1); hsv.writeTextElement("SAT HIGH",s_slide2); hsv.writeTextElement("LUM LOW",l_slide1); hsv.writeTextElement("LUM HIGH",l_slide2); hsv.writeEndElement(); hsv.writeEndDocument(); valueHSV.close(); /*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(); }
After i change to QXmlStreamWriter
I get error
Code:
ERROR MSG: invalid conversion from 'int' to 'const char*' [-fpermissive] error: initializing argument 1 of 'QString::QString(const char*)'
Please advise.
Thanks in advance
second argument to "writeTextElement" should be a string, not an integer:Code:
hsv.writeTextElement("HUE LOW",h_slide1);