[solved]: Global parameter
-
Hi..
I want to ask about global parameter in Qt.
I have a class that have a method with 'count' variable. For the time being this 'count' I placed on private.In the mainwindow.cpp, I create an object of that class every 200ms.
I cannot detect if the count reaches 3 (count==3) because I thought that it become 0 when the object created every 200ms.
Please help where should I place this global parameter so that I can detect when the count reaches 3.
Thanks -
Hello.
One of the possible ways of achieving what you want is to make count a static private field, increment it in constructor and decrement it in destructor:
@
class Your_Class {
private:
static unsigned int count = 0;
public:
Your_Class () {
count++;
}
~Your_Class () {
count--;
}
}
@
But I think it's not a good solution:It's not thread safe.
You should watch out for temp objects.
-
Hi...
yes may be there is something wrong in my code, here is the code, please give suggestion:mylaserclick.h:
@
#ifndef MYLASERCLICK_H
#define MYLASERCLICK_H#include <QObject>
#include <opencv2/core/core.hpp>using namespace std;
using namespace cv;class MyLaserClick : public QObject
{
Q_OBJECT
public:
explicit MyLaserClick(bool inspace, bool laser_on, QObject *parent = 0);signals:
public slots:
bool cek_click();private:
bool inspace;
bool on;static unsigned int ready_klik; static unsigned int count;
};
#endif // MYLASERCLICK_H
@mylaserclick.cpp:
@
#include "mylaserclick.h"MyLaserClick::MyLaserClick(bool inspace, bool laser_on, QObject *parent) :
QObject(parent)
{
this->inspace = inspace;
this->on = laser_on;}
bool MyLaserClick::cek_click()
{
if (inspace==true && on==true && ready_klik != 2)
ready_klik=1;if (on==false && ready_klik==1 && inspace==true){ ready_klik=2; count= count+1; } if (count==1 && inspace==false){ ready_klik=0; count=0; } if (on==true && ready_klik==2 && inspace==true){ count = count+1; } if (count==2){ return true; count=0; ready_klik=0; }
else
return false;
}EDIT1: :) I modify by change the 'return true;' to the last line
if (count==2){
count=0;
ready_klik=0;
return true;
}@
mainwindow.cpp:
@
....
MyLaserClick *laserklik;
laserklik = new MyLaserClick(inspace,on);
laser_click_inspace = laserklik->cek_click();
....
@main.cpp:
@
#include <QtGui/QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);unsigned int MyLaserClick::ready_klik=0; unsigned int MyLaserClick::count=0; MainWindow *dialog = new MainWindow; dialog->show(); return a.exec();
}
@the error message are:
'MyLaserClick' is not a class or namespace name main.cpp line 9'MyLaserClick' is not a class or namespace name main.cpp line 10