简体   繁体   English

信号和插槽QT

[英]Signals and slots QT

I have asked a number of different questions now all regarding one main issue in my program and still not solved it at all, I'm using threading to keep my UI from locking up, but basically it still does because apparently you can't do UI stuff in threads. 我现在已经针对程序中的一个主要问题问了很多不同的问题,但仍然根本没有解决它,我正在使用线程来防止UI锁定,但是基本上它仍然可以,因为显然您不能做UI线程中的内容。

So I've been told to use custom signals and slots (not that any examples were given). 因此,有人告诉我使用自定义信号和插槽(没有给出任何示例)。

So from the documentation I've read I came up with this code: 因此,我从阅读的文档中得出了以下代码:

.h 。H

signals:

void paint_signal(double x, double y);

.cpp .cpp

  connect(this,SIGNAL(paint_signal(double x, double y)), this, SLOT(PaintSomething(x,y)));

the Paintsomething function is within the same class as all of this.... Paintsomething函数与此所有类都在同一个类中。

thread: 线:

*future2 = QtConcurrent::run(this, &GUI::paintAll);

paint all emits the paint_signal and passes 2 doubles 全部绘制都发出paint_signal并传递2个双打

emit paint_signal(x, y);

but I get this error which I just don't understand at all 但是我得到了一个我根本不理解的错误

 connect: No such signal GUI::paint_signal(double x, double y)
connect(this,
        SIGNAL(paint_signal(double, double)), 
        this, 
        SLOT(PaintSomething(x,y)));

Remove parameter names and it should work. 删除参数名称,它将起作用。 If this one doesn't work this one will: 如果此方法不起作用,则它将:

    connect(this,
        SIGNAL(paint_signal(double, double)), 
        this, 
        SLOT(PaintSomething(double,double)));

Let me know if this works out for you :) 让我知道这是否对您有用:)

Update 更新资料

The idea is that you cannot use the UI in a thread, instead you emit signals from the thread to the UI. 这个想法是您不能在线程中使用UI,而是从线程向UI发出信号。 Because this answer probably gets you back to the beginning (and possibly a new question) here is a working example of how to emit signals from threads: 因为此答案可能使您回到起点(可能还有一个新问题),所以这里是一个如何从线程发出信号的可行示例:

QT Signals to UI in a thread QT在线程中向UI发出信号

Floris Velleman's answer is ok, however by using the new signal slot syntax , you can catch errors during compile time and get rid of the redundant paranthesis. Floris Velleman的答案是可以的,但是通过使用新的信号槽语法 ,您可以在编译时捕获错误并摆脱多余的局面。

Old Syntax: 旧语法:

connect(this,
        SIGNAL(paint_signal(double, double)), 
        this, 
        SLOT(PaintSomething(double,double)));

New Syntax: 新语法:

connect(this,
        &SenderClass::paint_signal, 
        this, 
        &ReceiverClass::PaintSomething);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM