12-23-2012 01:07 PM
Been looking for a tutorial on this in the examples and can't find anything. Maybe I missed it?
I'm looking for a simple data class example that has a public method to set an observer, similar to this:
class MyDataClass : QObject // I'm guessing I would subclass QObject
{
public:
int getSomeValue();
signals:
void setObserver(const QObject* receiver, ...?);
private:
int m_someValue;
};
// ...and cpp file with setting the connection
Is there sample code out there with this? If not, could someone post a sample class?
I've seen samples in QML, but I'm looking for C++.
Thanks!
Solved! Go to Solution.
12-23-2012 05:19 PM - edited 12-23-2012 05:19 PM
#include <QObject>
class Settings : public QObject
{
Q_OBJECT
public:
explicit Settings(QObject *parent = 0);
static Settings *sharedInstance();
QString getTargetIPAddress();
void setTargetIPAddress(const QString &ipAddress);
signals:
void targetIPAddressChanged();
};
---
#include <QSettings>
#include "Settings.h"
const char *targetIPAddressKey = "targetIPAddress";
Settings::Settings(QObject *parent) :
QObject(parent)
{
}
Settings *Settings::sharedInstance()
{
static Settings settings;
return &settings;
}
QString Settings::getTargetIPAddress()
{
QSettings settings;
QVariant v = settings.value(targetIPAddressKey);
if (v.isNull())
return "192.168.0.1";
return v.toString();
}
void Settings::setTargetIPAddress(const QString &ipAddress)
{
QSettings settings;
settings.setValue(targetIPAddressKey, QVariant(ipAddress));
settings.sync();
emit targetIPAddressChanged();
}
---
Some other class:
.h:
protected slots:
void targetAddressChanged();
.cpp:
[...constructor...]
Settings *settings = Settings::sharedInstance();
QObject::connect(settings, SIGNAL(targetIPAddressChanged()),
this, SLOT(targetAddressChanged()));
targetAddressChanged(); // for initial setup
[...]
void UDPClient::targetAddressChanged()
{
Settings *settings = Settings::sharedInstance();
// Do something with settings->getTargetIPAddress()
}I hope this will help. ![]()
12-23-2012 05:26 PM
Or if you want to pass a SLOT to the function:
void onSomething(const QObject* receiver, const char *method) {
this->connect(SIGNAL(touch(bb::cascades::TouchEven t*)), receiver, method);
}
12-23-2012 05:28 PM
This is very nice, thank you.