1

我正在尝试使用显示启动图像,QSplashScreen并且我想显示图像约 2 秒钟。

int main(int argc, char *argv[]) {

    QApplication a(argc, argv);
    QPixmap pixmap(":/images/usm.png");
    QSplashScreen splash(pixmap);
    splash.show();
     splash.showMessage("Loading Processes");
    QTimer::singleShot(2000, &splash, SLOT(close()));
    MainWindow w;
      w.show();

    splash.finish(&w);
    return a.exec();
}

但这不起作用。QSplashScreen出现几毫秒然后消失。尝试修改时间段,但对象似乎QSplashScreen未连接到插槽。有什么问题以及如何避免它?

4

2 回答 2

4

您的代码的问题是计时器没有阻止执行,因此启动画面已经随着splash.finish(&w)调用而关闭。你需要的是一个睡眠。你可以使用QWaitCondition这样的:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSplashScreen splash(QPixmap(":/images/usm.png"));
    splash.show();
    splash.showMessage("Loading Processes");

    // Wait for 2 seconds
    QMutex dummyMutex;
    dummyMutex.lock();
    QWaitCondition waitCondition;
    waitCondition.wait(&dummyMutex, 2000);

    MainWindow w;
    w.show();

    splash.finish(&w);
    return a.exec();
}

这种方法的缺点是您正在阻止执行。如果您不想阻止它,那么您可以简单地删除呼叫splash.finish(&w)

int main(int argc, char *argv[]) {

    QApplication a(argc, argv);
    QPixmap pixmap(":/images/usm.png");
    QSplashScreen splash(pixmap);
    splash.show();
    splash.showMessage("Loading Processes");
    QTimer::singleShot(2000, &splash, SLOT(close()));
    MainWindow w;
    w.show();
    return a.exec();
}
于 2012-03-09T19:02:15.310 回答
1

此代码应该可以工作:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QSplashScreen splash(QPixmap(":/images/usm.png"));
    splash.showMessage("Loading Processes");
    splash->show();

    QMainWindow w;

    QTimer::singleShot(2000, splash, SLOT(close()));
    QTimer::singleShot(2500, &w, SLOT(show()));

    return a.exec();
}
于 2012-03-09T18:09:07.597 回答