写了个循环显示图片并在显示完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和信号的问题实在让我有些头疼。