• 14944阅读
  • 21回复

多线程中主次线程之间的信号和槽 [复制链接]

上一主题 下一主题
离线butcherty
 

只看楼主 倒序阅读 楼主  发表于: 2010-05-18
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
QObject::connect: Cannot connect (null)::msgBoxsignal() to MainWindow::msgBoxslot()

编译正常,运行时候提示上面问题

msgBoxsignal()在次线程中定义,未做实现,用emit直接发送,主线程中用msgBoxslot()接收,不知是否有问题?


这是实现连接的程序
connect(0, SIGNAL(msgBoxsignal()), this, SLOT(msgBoxslot()));


离线dbzhang800

只看该作者 1楼 发表于: 2010-05-18
引用楼主butcherty于2010-05-18 17:50发表的 多线程中主次线程之间的信号和槽 :
QObject::connect: Cannot connect (null)::msgBoxsignal() to MainWindow::msgBoxslot()
这是实现连接的程序
connect(0, SIGNAL(msgBoxsignal()), this, SLOT(msgBoxslot()));
.......


这个0是怎么回事?
离线butcherty

只看该作者 2楼 发表于: 2010-05-19
引用第1楼dbzhang800于2010-05-18 18:17发表的  :
这个0是怎么回事?

有人跟我说0就代表不管什么信号都接收
离线dbzhang800

只看该作者 3楼 发表于: 2010-05-19
引用第2楼butcherty于2010-05-19 08:31发表的  :
有人跟我说0就代表不管什么信号都接收

还是认真看manual吧,估计你听别人话时断章取义了。在 disconnect 中 0 倒是有点类似你提到的作用


http://doc.qt.nokia.com/4.6/qobject.html#connect
离线butcherty

只看该作者 4楼 发表于: 2010-05-19
引用第3楼dbzhang800于2010-05-19 08:43发表的  :
还是认真看manual吧,估计你听别人话时断章取义了。在 disconnect 中 0 倒是有点类似你提到的作用
http://doc.qt.nokia.com/4.6/qobject.html#connect


我现在改了一下,在主线程中
SerialThread *serialthread = new SerialThread(this);
connect(serialthread, SIGNAL(msgBoxsignal()), this, SLOT(msgBoxslot()));

现在提示的是
Object::connect: No such signal QThread::msgBoxsignal()

帮忙指点下哈,现在比较急的这个
离线dbzhang800

只看该作者 5楼 发表于: 2010-05-19
引用第4楼butcherty于2010-05-19 09:02发表的  :
我现在改了一下,在主线程中
SerialThread *serialthread = new SerialThread(this);
connect(serialthread, SIGNAL(msgBoxsignal()), this, SLOT(msgBoxslot()));
.......


贴你的代码来证明该信号的存在!
离线butcherty

只看该作者 6楼 发表于: 2010-05-19
引用第5楼dbzhang800于2010-05-19 09:20发表的  :
贴你的代码来证明该信号的存在!


SerialThread::SerialThread(MainWindow *parent)
{
    this->parent = parent;
}

void SerialThread::run()
{
    extern int i;
    MainWindow *parent;

    if(0 == i)
      {
    emit msgBoxsignal_0();
      }

    if(1 == i)
      {
    emit msgBoxsignal_1();
    connect(parent, SIGNAL(serialsignal_1()), this, SLOT(serialslot_1()));
      }

    if(2 == i)
      {
    emit msgBoxsignal_2();
    connect(parent, SIGNAL(serialsignal_2()), this, SLOT(serialslot_2()));
      }

    if(3 == i)
      {
    emit msgBoxsignal_3();
    connect(parent, SIGNAL(serialsignal_3()), this, SLOT(serialslot_3()));
      }

    if(4 == i)
      {
    emit msgBoxsignal_4();
    connect(parent, SIGNAL(serialsignal_4()), this, SLOT(serialslot_4()));
      }

    if(5 == i)
      {
    emit msgBoxsignal_5();
    connect(parent, SIGNAL(serialsignal_5()), this, SLOT(serialslot_5()));
      }

    if(6 == i)
      {
    emit msgBoxsignal_6();
    connect(parent, SIGNAL(serialsignal_6()), this, SLOT(serialslot_6()));
      }

    if(7 == i)
      {
    emit msgBoxsignal_7();
    connect(parent, SIGNAL(serialsignal_7()), this, SLOT(serialslot_7()));
      }

    if(8 == i)
      {
    emit msgBoxsignal_8();
    connect(parent, SIGNAL(serialsignal_8()), this, SLOT(serialslot_8()));
      }

    if(9 == i)
      {
    emit msgBoxsignal_9();
    connect(parent, SIGNAL(serialsignal_9()), this, SLOT(serialslot_9()));
      }

    if(10 == i)
      {
    emit msgBoxsignal_10();
    connect(parent, SIGNAL(serialsignal_10()), this, SLOT(serialslot_10()));
      }

    if(11 == i)
      {
    emit msgBoxsignal_11();
    connect(parent, SIGNAL(serialsignal_11()), this, SLOT(serialslot_11()));
      }

    if(12 == i)
      {
    emit msgBoxsignal_12();
    connect(parent, SIGNAL(serialsignal_12()), this, SLOT(serialslot_12()));
      }

    if(13 == i)
      {
    emit msgBoxsignal_13();
    connect(parent, SIGNAL(serialsignal_13()), this, SLOT(serialslot_13()));
      }

接收的是combobox的currentIndex的值后发送信号,用emit发送的,具体函数没有去实现
离线butcherty

只看该作者 7楼 发表于: 2010-05-19
分别在头文件中用sigenals:  声明了
离线butcherty

只看该作者 8楼 发表于: 2010-05-19
人呢?
离线dbzhang800

只看该作者 9楼 发表于: 2010-05-19
贴你的头文件来证明 msgBoxsignal 信号存在!

题外,感觉你的run函数的具体实现相当诡异,不清楚你在实现什么功能 ^_^
离线wato
只看该作者 10楼 发表于: 2010-05-19
LZ
msgBoxsignal()和msgBoxsignal_n()可完全不一样  没有任何关系  别以为计算机和你一样聪明
离线butcherty

只看该作者 11楼 发表于: 2010-05-19
引用第10楼wato于2010-05-19 11:41发表的  :
LZ
msgBoxsignal()和msgBoxsignal_n()可完全不一样  没有任何关系  别以为计算机和你一样聪明



这是我自己定义的signal,都是对应的,这点我还是清楚的
离线foxyz

只看该作者 12楼 发表于: 2010-05-19
好像线程间是不能用SIGNAL-SLOT这套机制的
离线foxyz

只看该作者 13楼 发表于: 2010-05-19
Signals and Slots Across Threads

Qt supports these signal-slot connection types:

Auto Connection (default) The behavior is the same as the Direct Connection, if the emitter and receiver are in the same thread. The behavior is the same as the Queued Connection, if the emitter and receiver are in different threads.
Direct Connection The slot is invoked immediately, when the signal is emitted. The slot is executed in the emitter's thread, which is not necessarily the receiver's thread.
Queued Connection The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
Blocking Queued Connection The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns. Note: Using this type to connect objects in the same thread will cause deadlock.
Unique Connection The behavior is the same as the Auto Connection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection is not made and connect() returns false.
The connection type can be specified by passing an additional argument to connect(). Be aware that using direct connections when the sender and receiver live in different threads is unsafe if an event loop is running in the receiver's thread, for the same reason that calling any function on an object living in another thread is unsafe.

QObject::connect() itself is thread-safe.

The Mandelbrot example uses a queued connection to communicate between a worker thread and the main thread. To avoid freezing the main thread's event loop (and, as a consequence, the application's user interface), all the Mandelbrot fractal computation is done in a separate worker thread. The thread emits a signal when it is done rendering the fractal.

Similarly, the Blocking Fortune Client example uses a separate thread for communicating with a TCP server asynchronously.
离线butcherty

只看该作者 14楼 发表于: 2010-05-19
楼上能把你摘的这段文字的网址发出来不?我需要的就是主次线程的通信,你这里写的例子我想看下
离线butcherty

只看该作者 15楼 发表于: 2010-05-19
找到了,原来是REFERENCE里的
离线foxyz

只看该作者 16楼 发表于: 2010-05-19
呵呵,是啊! 我怎么印象中, SIGNAL-SLOT这一套不能用在MultiThread里边的。这个reference里边说是可以的。
离线butcherty

只看该作者 17楼 发表于: 2010-05-19
引用第16楼foxyz于2010-05-19 15:25发表的  :
呵呵,是啊! 我怎么印象中, SIGNAL-SLOT这一套不能用在MultiThread里边的。这个reference里边说是可以的。


应该是可以的吧,我看很多地方都说的是要使用SIGNAL-SLOT的
离线梅克斯
只看该作者 18楼 发表于: 2011-01-13
那个  0   真的是大亮啊.....
离线XChinux

只看该作者 19楼 发表于: 2011-01-17
signa/slot可以在不同线程中使用的。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线jetcai1900
只看该作者 20楼 发表于: 2011-03-25
不会出现紊乱吗?
离线voidbroken

只看该作者 21楼 发表于: 2011-03-30
所以要同步,需要Qmutex, Qsemaphore,QWaitCondition等
[ 此帖被voidbroken在2011-03-30 15:33重新编辑 ]
快速回复
限100 字节
 
上一个 下一个