[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.
-
Thanks for your reply,
I tried but there is a message:
' ..only static const integral data members can be initialized within a class'I change the parameter to
static unsigned int count;
the message become: ' unresolved external symbol...'
-
Sorry, my fault. Change to @static unsigned int count@ and in you .cpp file write @unsigned int Your_Class::count = 0.@
-
Thanks for reply...
I already try it, I tried adding unsigned int MyClass::count=0, in mainwindow.cpp, it did not work. And neither in main.cpp.
Is there any other solution for Global parameter?
thanks in advance -
Hello again.
I'm sorry, but it seems like you're doing something wrong. Because I'm looking at my own code with the same structure. It works. Could you please post code, that doesn't work and compiler output with error? -
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
-
Yep, wrong code.
The right one:
mylaserclick.cpp:
@
#include "mylaserclick.h"unsigned int MyLaserClick::ready_klik=0;
unsigned int MyLaserClick::count=0;/* ... */
@And delete 9 and 10 lines from main.cpp.
-
It works, no error message again. And the count run well. Many thanks...
Thank you very very much...:) -
Do not mention it