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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 194
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!

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

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. Smiley Happy


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

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
Developer
Posts: 194
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.