English 中文(简体)
怎样做 是否执行信号和位置?
原标题:How does Qt implement signals and slots?

Can someone explain to me the basic idea of Qt signals&slots mechanism IMPLEMENTATION? I want to know what all those Q_OBJECT macros do "in plain C++". This question is NOT about signals&slots usage.

added: I know that Qt uses moc compiler to transform Qt-C++ in plain C++. But what does moc do? I tried to read "moc_filename.cpp" files but I have no idea what can something like this mean

void *Widget::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_Widget))
    return static_cast<void*>(const_cast< Widget*>(this));
return QDialog::qt_metacast(_clname);
}
最佳回答

关于信号和位置,<代码>Q_OBJECT/code> 宏观在班级的声明中增加了一个虚拟功能qt_metabet(),该声明将在<代码>moc后面界定。 (它还增加了一些转换声明,但此声明并不重要)。)

<代码>moc 然后,读到标题档案,看宏观时,它生成另一个<代码>.cpp文档,名称为moc_headerfilename.cpp,其中包含对虚拟功能的定义,并询问你为什么可以删除提及<编码>签字:在您的主文档中,没有适当定义————信号。

So, when a signal is called, the definition from the mocfile is executed and QMetaObject::activate() is called with the signal’s name and the signal’s arguments. The activate() function then figures out which connections have been established and fetches the names for the appropriate slots.

然后,它打电话到qt_metabet<>/code>,其中注明了位置名称和对信号和美容功能提出的论点,这些代表得到了一个大型<代码>switch->>-对真实位置的说明。

由于在C++中没有关于信号和位置的实际名称的实时信息,如已经注意到的,这些信息将由以下编码编码编码编码:>SLOT 简单const char*的宏观编码(在名称上加上“1”或“2”来区别信号与位置)。

如下文所示:

#define SLOT(a)     "1"#a
#define SIGNAL(a)   "2"#a

——

<代码>Q_OBJECT 宏观法则界定了<代码>tr(>功能,可用于翻译您的申请。

Edit As you asked what the qt_metacast is doing. It checks whether an object belongs to certain class and if it does returns the pointer to it. If it doesn’t, it returns 0.

Widget* w = new Widget();
Q_ASSERT(w->qt_metacast("Widget") != 0);
Q_ASSERT(w->qt_metacast("QWidget") != 0);
Q_ASSERT(w->qt_metacast("QObject") != 0);
Q_ASSERT(w->qt_metacast("UnrelatedClass") == 0);

这需要提供一些时间上的思考,否则是不可能的。 这一职能载于QObject:inherits(const char *),例如,简单地检查遗产。

问题回答

这些宏观办法绝对没有“C++”——它们扩大到空洞(我想)。

QT使用一个元数据集,生成C++标准,用于Q_OBJECT-enabled班(落实你确定的信号/位置,除其他外)。

可在 官方文件上阅读更多内容。

基本想法是,在发出信号时,你可以把目标连接起来,允许它们执行某种方法(位置)。

connect(pistol,SIGNAL(sigShoot()),runner,SLOT(slotRun()))

上面说的是,当手枪穿透信号时,驾驶员将执行其位置。

为此,你必须宣布您的信号和位置。

基本想法。

亲爱!





相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签