Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533217
  • 博文数量: 174
  • 博客积分: 4177
  • 博客等级: 上校
  • 技术积分: 1827
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-15 14:12
文章分类

全部博文(174)

文章存档

2018年(1)

2017年(1)

2013年(3)

2012年(9)

2010年(12)

2009年(5)

2008年(106)

2007年(37)

我的朋友

分类: C/C++

2007-11-29 17:17:04

   今天下午可以说是极其郁闷的一下午,折腾了一下午,到最后才发现问题出在自己多写的几个变量名上,看来有时候写得多也不见得是好事。,首先把错误的代码段贴出来,看大伙能否看出:

   connect(Data,SIGNAL(valueChanged(int i)),this,SLOT(setVal(int i)));
  
   不知道大伙有没有发现其中的错误,正确的写法应该是:

   connect(Data,SIGNAL(valueChanged(int)),this,SLOT(setVal(int)));

   这是因为:connect() 语句的语法格式为:
  
   connect(sender,SIGNAL(signal),receive,SLOT(slot));

   其中sender与receive为指向QObjects的指针,而signal与slot是没有参数名的function signatures.SIGNAL()与SLOT()宏实质上把它们的参数转换成一个字符串。下图为signal与slot示意图以及trolltech公司的官方文档上关于Qt的signal与slot的一些说明。


The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches. Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type. They are completely type safe.

All classes that inherit from QObject or one of its subclasses (e.g., ) can contain signals and slots. Signals are emitted by objects when they change their state in a way that may be interesting to other objects. This is all the object does to communicate. It does not know or care whether anything is receiving the signals it emits. This is true information encapsulation, and ensures that the object can be used as a software component.

Slots can be used for receiving signals, but they are also normal member functions. Just as an object does not know if anything receives its signals, a slot does not know if it has any signals connected to it. This ensures that truly independent components can be created with Qt.

You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)

All classes that contain signals or slots must mention at the top of their declaration. They must also derive (directly or indirectly) from .

The C++ preprocessor changes or removes the signals, slots, and emit keywords so that the compiler is presented with standard C++.

By running the on class definitions that contain signals or slots, a C++ source file is produced which should be compiled and linked with the other object files for the application. If you use , the makefile rules to automatically invoke moc will be added to your project's makefile.

Signals

Signals are emitted by an object when its internal state has changed in some way that might be interesting to the object's client or owner. Only the class that defines a signal and its subclasses can emit the signal.

When a signal is emitted, the slots connected to it are usually executed immediately, just like a normal function call. When this happens, the signals and slots mechanism is totally independent of any GUI event loop. Execution of the code following the emit statement will occur once all slots have returned. The situation is slightly different when using ; in such a case, the code following the emit keyword will continue immediately, and the slots will be executed later.

If several slots are connected to one signal, the slots will be executed one after the other, in an arbitrary order, when the signal is emitted.

Signals are automatically generated by the and must not be implemented in the .cpp file. They can never have return types (i.e. use void).

A note about arguments: Our experience shows that signals and slots are more reusable if they do not use special types. If () were to use a special type such as the hypothetical QScrollBar::Range, it could only be connected to slots designed specifically for . Something as simple as the program in would be impossible.


Slots

A slot is called when a signal connected to it is emitted. Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them.

Since slots are normal member functions, they follow the normal C++ rules when called directly. However, as slots, they can be invoked by any component, regardless of its access level, via a signal-slot connection. This means that a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class.

You can also define slots to be virtual, which we have found quite useful in practice.

Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls. This is the overhead required to locate the connection object, to safely iterate over all connections (i.e. checking that subsequent receivers have not been destroyed during the emission), and to marshall any parameters in a generic fashion. While ten non-virtual function calls may sound like a lot, it's much less overhead than any new or delete operation, for example. As soon as you perform a string, vector or list operation that behind the scene requires new or delete, the signals and slots overhead is only responsible for a very small proportion of the complete function call costs.

The same is true whenever you do a system call in a slot; or indirectly call more than ten functions. On an i586-500, you can emit around 2,000,000 signals per second connected to one receiver, or around 1,200,000 per second connected to two receivers. The simplicity and flexibility of the signals and slots mechanism is well worth the overhead, which your users won't even notice.

Note that other libraries that define variables called signals or slots may cause compiler warnings and errors when compiled alongside a Qt-based application. To solve this problem, #undef the offending preprocessor symbol.

Meta-Object Information

The meta-object compiler () parses the class declaration in a C++ file and generates C++ code that initializes the meta-object. The meta-object contains the names of all the signal and slot members, as well as pointers to these functions.

The meta-object contains additional information such as the object's . You can also check if an object a specific class, for example:

        if (widget->inherits("QAbstractButton")) {
QAbstractButton *button = static_cast(widget);
button->toggle();
}

The meta-object information is also used by qobject_cast(), which is similar to () but is less error-prone:

        if (QAbstractButton *button = qobject_cast(widget))
button->toggle();

See for more information.

阅读(2408) | 评论(0) | 转发(0) |
0

上一篇:了解fonts.dir文件结构

下一篇:Qt编程资料

给主人留下些什么吧!~~