OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?

Qt (2) Examine the signals and slots

Published on| May 21st, 2009 | No Comment.
Description:
Qt is most likely features of Qt, moc and uic compiler that is to have one.
In this article, is one of the features described moc.
Qt, and in class (or the object-oriented, objects) used the concept of signals and slots to achieve the exchange of information.

Windows SDK, the equivalent processing in the messages. On Windows, HWND a unique ID as a key, the sender is, SendMessage, PostMessage and sending information. The receiver, GetMessage, PeekMessage and you will receive.
That is, MFC in the same, CWnd even even if you inherit, SendMessage is used, the method wrap (SendMessage) also.

In Qt, QObject inherited class if, for anyone to handle signals and slots.
Also, QObject inherited if between classes, it is like to handle, even if the thread.
Functional (not technical) is, Windows speaking, signals, SendMessage equivalent.
Slot, Windows in, GetMessage to call the callback handler, like the handler.

moc is an auxiliary compiler written by programmers to make it easier for the signal and slot.
In fact, the compiler, moc_xxxxx.cpp (xxxxx: QObject inherited class name) of the output file, the file C / C + + compiler will be passed to the pre-treatment.

So, immediately, let's try using the sample source.
Program used here is a sample program that helps判RI are listed below the site, some are changed.
MOC: Meta-Object Compiler
UIC: User Interface Compiler



Related Sites: http://doc.trolltech.com/4.5/signalsandslots.html

Sample source code:

Try: console application
Further explanation, related sites, are also described in other sites, here and advance the talks, focusing on a sample program.

Prepare three sample files.

  • sample.cpp
    - Main is routine.
  • counter.h
    - QObject class is inherited header.
  • counter.cpp
    - QObject inherited class source.

Here, QObject inherited class, always have to separate the header and the source. moc does not recognize correctly.

Let's look at the source code.

[counter.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "QObject"
#include "QTextStream"
 
class Counter : public QObject
{
	Q_OBJECT
 
public:
	Counter(const QString sname);
 
	int value() const;
 
public slots:
	void setValue(int value) ;
 
signals:
	void valueChanged(int newValue);
 
private:
	int m_value;
	QString m_name;
 
};
I get the argument to the constructor of the object name.
The information in the output below, you will not easily moved Rika Waka?

Slot definition, "public slot:" The description of the above.
Definition of the signal, "signals:" The description of the above.


[counter.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "counter.h"
 
Counter::Counter(const QString sname) 
{
	m_value = 0;
	m_name = sname;
}
 
int Counter::value() const
{
	return m_value;
}
 
void Counter::setValue(int value) 
{
	QTextStream out(stdout);
	if (value != m_value) {
		m_value = value;
		out << "send:[" << m_name << "] " << value << endl;
		emit valueChanged(value);
		out << "completed:[" << m_name << "] " << value << endl;
	} else {
		out << "Ignore:[" << m_name << "] " << value << endl;
	}
}
When you call the setValue method, if only to set the number of attribute values and internal argument, is to issue a signal. "emit" a signal is issued.
The information in the output below, you will not easily moved Rika Waka?

If the numbers match the numbers with the internal attributes of the argument is ignored.

Output arguments are given the name of each object in the root.

It is defined in the signal valueChanged Please note that there is no reality.
moc is automatically created.

"emit" is not anything in the reserved words. Qt is defined by a unique, original, is not good, but it is meant to describe it as a signal that the issue?


[sample.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "QObject"
#include "counter.h"
 
 
int main(int argc, char *argv[])
{
	Counter a("a");
	Counter b("b");
 
	QObject::connect(&a, SIGNAL(valueChanged(int)),
	              &b, SLOT(setValue(int)));
	QObject::connect(&b, SIGNAL(valueChanged(int)),
	              &a, SLOT(setValue(int)));
 
	a.setValue(12);
	b.setValue(48);
}

QObject:: connect is defined to connect signals and slots.
Each argument is as follows. The fifth argument to have, almost by default, so good, here is a spare.
If you want to know more, Qt, please refer to the documentation.

The first argument: The object address to issue a signal to two arguments: The definition of a signal three arguments: The object address is received in slot 4 arguments: Definition of slot

Signal, SIGNAL (x) using a macro, remember, x method is specified in the specified argument.
Slot, SLOT (y) using a macro, be sure, y specify the method that is specified by the argument.

Here, if you forgot to specify the argument will output the following error at runtime.
1
Object::connect: No such signal Counter::valueChanged() in .\sample.cpp:10
The wrong number of arguments, the behavior is different, do not change will not work correctly.
If there are many parameters of the signal is issued, for now, to try to operate slots. Naturally, I would not work properly, if good parameter can be ignored, it will appear to work properly.

The reverse does not reach the slot. Ignored the signal issue.


Now, the source code line 10-13 in QObject:: connect you to write, please look.

a. valueChanged -> b. setValue flow to line 10.
b. valueChanged -> a. setValue flow to line 12.

a, b. each together to issue a signal to receive. Definition of cross-coupled. However,, [counter.cpp] Slot (the receiver) setValue, signals, and then issue.

In other words, a. ValueChanged -> b. SetValue -> b. ValueChanged -> a. SetValue next, and finally, we will issue a signal from the beginning, the process is infinitely more so.

However, [counter.cpp] Slot (the receiver) setValue, if the numbers match the numbers with the internal attributes of the argument is ignored.
Therefore, the infinite loop陥RIMASEN.

The process is easy to process led to a deadlock. Process is called to meet each other.
 Object a   -->   Object b  
           <--      
Well, this is what results. What will be processed as expected.
As with previous Qt Create console application to create a console application from here, let's run.

1
C:\temp> qmake "CONFIG+=console"
The following are the results.

1
2
3
4
5
6
7
8
9
10
send:[a] 12
send:[b] 12
Ignore:[a] 12
completed:[b] 12
completed:[a] 12
send:[b] 48
send:[a] 48
Ignore:[b] 48
completed:[a] 48
completed:[b] 48

動KIMASHITA clean. Even as a thread in this process is as fine as usual.
(I tried this, single tasking, single-CPU environment because, really, have to try a lot more)

Typically, "qmake" after, "nmake" We are running a build of all. If you run a build, moc, uic work in front of the cl. moc, uic you can manually run separately.
The following is an example of the command.

moc-o debug \ moc_counter.cpp counter.h
uic-o ui_sqllogform.h sqllogform.ui

The following format. -o is for output parameters.
-o <output filename> <input filename>
For more details, please refer to the Reference Manual.

moc Reference Manual: "http://doc.trolltech.com/4.5/moc.html
uic Reference Manual: "http://doc.trolltech.com/4.5/uic.html

Up until now, Qt has been explained with a simple tutorial example of the world.

Next, let's do the same thing with the screen.
This is a menu of the menu update and windows that let in a handler.



Add to your favorites(bookmarks): はてなブックマークへ追加するdel.icio.usLivedoor ClipYahoo!FC2Nifty ClipPOOKMARK. AirlinesBuzzurl(バザール)Choixnewsing

Trackback URL

After Admin approves this comment, it will be shown.


Comments

Leave a Reply





*




Contents

Recent Posts


Tag Cloud

Links

Site Description

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?

Add to your favorites(bookmarks)

はてなブックマークへ追加するdel.icio.usLivedoor ClipYahoo!FC2Nifty ClipPOOKMARK. AirlinesBuzzurl(バザール)Choixnewsing