First Taste of Code

For your first bit of code on this blog, I’d like to simply give a quick tip on working with my favorite C++ library–Qt.

If you are familiar with Qt, you probably have a good understanding of the signals and slots mechanism. They are somewhat similar to C# and Java events, however they work in a different way. A signal is just a way to tell your program that something has happened (event). Then you have a slot that is sort of like a method. Your signal can pass parameters to this method as well.

After you write both of these, you connect them… literally. So for a little example, say we have a signal called  mySignal. Then we have a slot called mySlot. You can connect these by a simple method call:

// p_otherObject is the receiver, i.e. you are sending a message
// to the object at p_otherObject and handling it with mySlot()
connect(this, SIGNAL(mySignal()), p_otherObject, SLOT(mySlot()));

It’s also very easy to pass parameters through this mechanism:

// When you emit mySignal, you need to make sure to pass the values into the statement
connect(this, SIGNAL(mySignal(float, string)), p_otherObject, SLOT(mySlot(float, string)));

So lets whip up a really, really simple example. Say I have a Person class and an Employee class. Whenever I update the phone number in the Person class, I want it updated elsewhere in the Employee class. Lets start with the Person class:

class Person
{
	public:
		Person() {}

		Person(const int &phoneNumber)
		{
			m_phoneNumber = phoneNumber;
		}

		int phoneNumber() const
		{
			return m_phoneNumber;
		}

		void setPhoneNumber(const int &phoneNumber)
		{
			m_phoneNumber = phoneNumber;
		}

	private:
		int m_phoneNumber;
};

Just a plain old C++ class. Now lets add some things. Pay close attention to the QObject macro which is required for all classes that use Qt mechanisms such as signals/slots.

class Person : public QObject
{
        Q_OBJECT
	public:
		Person() {}

		Person(const int &phoneNumber)
		{
			m_phoneNumber = phoneNumber;
		}

		int phoneNumber() const
		{
			return m_phoneNumber;
		}

		void setPhoneNumber(const int &phoneNumber)
		{
			m_phoneNumber = phoneNumber;

			// Tell our program that this value has been
			// changed.
			emit phoneNumberChanged(phoneNumber);
		}

	signals:
		void phoneNumberChanged(int phoneNumber);

	private:
		int m_phoneNumber;
};

Now we have to handle that signal. The Employee class will do just that:

class Employee : public QObject
{
        Q_OBJECT
	public:
		Employee() {}

		Employee(const int &phoneNumber)
		{
			m_phoneNumber = phoneNumber;
		}

		int phoneNumber() const
		{
			return m_phoneNumber;
		}

		void setPhoneNumber(const int &phoneNumber)
		{
			m_phoneNumber = phoneNumber;
		}

	public slots:
		void updatePhoneNumber(int phoneNumber)
		{
			// This is what we do when the slot gets
			// called.
			setPhoneNumber(phoneNumber);
		}

	private:
		int m_phoneNumber;
};

Lastly, we need to actually use this. We’ll do this in a simple main method you can call in your application:

int main(int argc, char* argv[])
{
	// Initialize a Qt Application. This is important
	// as we need the event loop for it, as you can
	// see later.
	QCoreApplication app(argc, argv);

	// Don't try calling this number...
	Person person(5555555555);

	// There is much better ways to handle
	// the phone number, including passing
	// the actual object into the constructor.
	Employee employee(person.phoneNumber());

	std::cout << employee.phoneNumber(); // 555-555-5555

	// Change the phone number to a slightly less
	// meaningless phone number
	person.setPhoneNumber(5555555554);

	std::cout << employee.phoneNumber(); // 555-555-5554

	// This is that event loop
	return app.exec();
}

To compile this, copy and paste appropriately (I tried to keep it organized) and run it through qmake and then make (nmake or equivalent on Windows). Before compiling, be sure to include your necessary files as well. Please note that with all Qt examples, you’ll need a working Qt developer environment. Other than that, if you still have problems/questions, post them in the comments below and I’ll be glad to try and help. For further reading/explanations, please check out the Qt Documentation on Signals and Slots.

Advertisements

One thought on “First Taste of Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s