• 4990阅读
  • 2回复

请教,在while循环中信号发射次数递增的问题。 [复制链接]

上一主题 下一主题
离线ericaq
 
只看楼主 正序阅读 楼主  发表于: 2010-04-13
写了个循环显示图片并在显示完10张后触发信号的程序(部分代码借鉴自贵论坛某贴),但是结果是第一个循环,没有触发信号,第二个循环,触发了一次信号,第三个循环,触发了两次次信号......
void MyMainWindow::paintEvent(QPaintEvent*)
{
    while(1)
    {
         for(i=1;i<=10;i++)
        {
            QString str = QString("%1.bmp").arg(i);
            QPixmap image(str);

            QPainter paint;
             paint.begin(this);
             paint.drawPixmap(0,0,image);
             paint.end();
             sleep(3.5);
             setValue();
         }
         connect(this,SIGNAL(signal()),this,SLOT(slot()));
     }
}

void MyMainWindow::setValue()
{
     if (i == 10)
     {
         printf("here is 10\n");
         emit signal();
     }
}

void MyMainWindow::slot()
{
      printf("here is slot\n");
}

int main(int argc,char **argv)
{
    QApplication a(argc,argv);
    MyMainWindow w;
    a.setMainWidget(&w);
    w.show();
    return a.exec();
}
以下是程序运行结果:
here is 10
here is 10
here is slot
here is 10
here is slot
here is slot
here is 10
here is slot
here is slot
here is slot
here is 10
here is slot
here is slot
here is slot
here is slot
here is 10
here is slot
here is slot
here is slot
here is slot
here is slot

希望各位高手指点迷津,关于while和信号的问题实在让我有些头疼。
离线mosjin
只看该作者 2楼 发表于: 2010-07-22
yeah
精灵实验室  jinLab | jinniLab
Enjoy Programming & Life!
http://jinLab.com | http://jinniLab.com
离线ericaq
只看该作者 1楼 发表于: 2010-04-14
哎,都没有人理啊,还好自己搞定了。
关键的一点是我没弄明白信号和槽连接的机制,信号和槽一旦连接就不用再连了,而且连接一定要写在触发事件发生之前。所以像我把连接函数写在while循环里,就会没循环一次就增加一次连接,而一旦触发事件发生,所以有效连接都会发出信号给槽。所以,把连接函数写到主函数里就好了。
快速回复
限100 字节
 
上一个 下一个