Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
javoid
Posts: 189
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA
Accepted Solution

Wanted: simple example of data class with a signal?

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!

 

Please use plain text.
Developer
Zmey
Posts: 1,505
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Wanted: simple example of data class with a signal?

[ Edited ]
#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. :smileyhappy:


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
Zmey
Posts: 1,505
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Wanted: simple example of data class with a signal?

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::TouchEvent*)), receiver, method);
}

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
javoid
Posts: 189
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA

Re: Wanted: simple example of data class with a signal?

This is very nice, thank you.

Please use plain text.