开发者

Making connections in Qt

If I want to make a connection in Qt as follows:

QObject::connect(quitButton, SIGNAL(clicked()), &myapp,开发者_运维知识库 SLOT(quit());

What does &myapp refer to here? Why should it be used?

Thanks.


In this case what you get is that whenever the quitButton sends the signal clicked it will be sent to the slot quit in myapp. If the names mean what I think this is probably a button to, well..., quit your app.

Notice there are different versions of connect. It's hard to get the context from a single line of code, but anyway you might wanna check qApp which already represents your application.

EDIT: From another post from you I noticed you asked a trivial question about pointers. So if this is more about what the &character means, it takes the address of your object. This is pure C++ (nothing specific to Qt).


What does &myapp refer to here? Why should it be used?

This is the object that will handle the signal (The & takes the objects address (ie. the underlying code uses pointers).

QObject::connect(quitButton, SIGNAL(clicked()), &myapp, SLOT(quit());

Translation:

When the signal clicked is activated on the object quitButton
Call the slot quit on the object myapp.

What this means:

A signal is just a method that is called by the object when certain internal state changes. In this case the object will call signal when the user interfaced element is clicked on by the mouse.

The signal method will then call all (slot) methods that have been registered. So in this case when you click on the button signal() is called this in turn will call the quit() method on the object 'myapp`.

Given the way QT examples are normally done. myapp is an application object and the quit() method will cause the main thread to exit from the call to exec().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜