Home

OFF-SOFT.net

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.

Update Signal and Command Signal
Here, let's create a signal in the slot and click on the menu and menu updates.

For convenience, the command signal is called signal-click menu. Update Signal the signal is called when a menu update.

Qt is a GUI library, so, let's create a sample in the next screen.

Like the three sample preparation.

  • sample.cpp
    - Main is routine.
  • mainwindow.h
    - QWidget class is inherited header.
  • mainwindow.cpp
    - QWidget inherited class source.

Similarly, QWidget (QObject) class inheritance is sure, you have to separate the header and the source. moc does not recognize correctly.

Let's look at the source code.

[mainwindow.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QtGui>
 
class MainWindow : public QWidget
{
	Q_OBJECT
 
public:
	MainWindow();
 
private slots:
	void updateMenus();
 
private:
	QMenuBar *m_menu;
	QMenu *m_fileMenu;
	QAction *m_exit_act;
 
	bool m_active;
};
#endif
This time, it creates a menu, QMenuBar, QMenu, QAction is added.
m_active is the state flag to enable the disabled menu item.


[mainwindow.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
26
27
#include "mainwindow.h"
 
MainWindow::MainWindow()
{
	m_menu = new QMenuBar( this );
 
	m_fileMenu = m_menu->addMenu(tr("&File"));
 
	m_exit_act = m_fileMenu->addAction("E&xit" );
	m_exit_act->setShortcut(tr("Ctrl+Q"));
	m_exit_act->setStatusTip(tr("Exit the application"));
 
	connect(m_exit_act, SIGNAL(triggered()), 
	         qApp, SLOT(closeAllWindows()));
	connect(m_fileMenu, SIGNAL(aboutToShow()),
	         this, SLOT(updateMenus()));
 
    setWindowTitle(tr("SampleWindow"));
 
	m_active=true;
}
 
void MainWindow::updateMenus()
{
	m_exit_act->setEnabled(m_active);
	m_active=!m_active;
}
In line 5, to create a menu bar. Widget is to manage the entire menu.
In line 7, the menu bar "File" and add a pop-up menu.
9 from the line, "File" pop-up menu called "Exit" menu item to add it.

The past, MFC in the same way as the ID was to manage and, now, is to manage action items. But it is no longer ID, this is to manage action items along with the flow.

From MFC, VCL or feel close to you. VCL using C + + but I did like this.

Line 13, the menu "Exit" when I click on, qApp of "closeAllWindows" I have to start the method declaration.
In other words, defines a Command Signal and its corresponding slot.
Line 15, the pop-up menu "File" when you see the object (this) of "updateMenus" I have to start the method declaration.
In other words, defines the slot and its corresponding Update Signal.

The qApp, MFC within-AfxGetApp () is similar.
MFC is not the only CWinApp CWinApp or after generation inherited objects, objects取RI出SEMASEN correctly.
Qt is the same. It corresponds to the QApplication. Without this reality, qApp is empty.
Follows in the main, the first of which creates a QApplication is also for that.

23 from the line, pop-up menu "File" menu item is displayed each time the "Exit" to disable and enable the toggle switch.

[mainwindow.cpp]
1
2
3
4
5
6
7
8
9
10
11
#include <QApplication>
 
#include "mainwindow.h"
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

Here, I think the explanation is not required. Create a simple application, just make the screen display.

Now, let's run.

The following menu, "Exit" I think every time you see the switch disabled.



Definition of each signal, so all are included in the reference class is good in there.
Most of the necessary signals, and we are aligned. Can easily be created as described above if the individual signals.

Qt class reference: http://doc.trolltech.com/4.5/classes.html


Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ