【问题标题】:Show main windows after the Splash Screen in C++ - QT在 C++ 中的初始屏幕之后显示主窗口 - QT
【发布时间】:2019-01-08 20:02:59
【问题描述】:

我有一个带有启动画面的应用程序。我需要在主窗口出现之前显示启动画面并等待 3.5 秒。现在完成后,我需要显示主窗口。

这是我尝试过的代码,

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
#include "game.h"
#include "player.h"
#include <QSplashScreen>
#include <QObject>


Game *game;

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

{
    //Splash screen
    QApplication a(argc, argv);
    QSplashScreen *splash = new QSplashScreen;
    splash->setPixmap(QPixmap(":/images/Images/1.JPG"));
    splash->show();

    //main window
    game = new Game();

    QTimer::singleShot(3500, splash, SLOT(close()));

    QTimer::singleShot(3500, game, SLOT(show()));

    game->displayHome();
    return a.exec();
}

游戏类

Game::Game(QWidget *parent)
{

    scene = new QGraphicsScene();
    scene->setSceneRect(0,0,800,600);
    setBackgroundBrush(QBrush(QImage("img.png")));

    setScene(scene);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setFixedSize(800,600);

    show();
     qDebug() << "yoyoyoy";

     score = new Score();
    health = new Health();


    connect(this->health,SIGNAL(crash()),this,SLOT(gameover3()));
}

void Game::displayHome()
{
    logo = new Logo();
    scene->addItem(logo);

    playButton = new Menubuttons(QString("Play"));
    int bxPos = this->width()/2 - playButton->boundingRect().width()/2;
    int byPos = 275;
    playButton->setPos(bxPos,byPos);
    connect(playButton,SIGNAL(clicked()),this,SLOT(startGame()));
    scene->addItem(playButton);

    instructions = new Menubuttons(QString("Instructions"));
    int axPos = this->width()/2 - instructions->boundingRect().width()/2;
    int ayPos = 350;
    instructions->setPos(axPos,ayPos);
    connect(instructions,SIGNAL(clicked()),this,SLOT(instruct()));
    scene->addItem(instructions);

    quitButton = new Menubuttons(QString("Quit"));
    int qxPos = this->width()/2 - quitButton->boundingRect().width()/2;
    int qyPos = 425;
    quitButton->setPos(qxPos,qyPos);
    connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
    scene->addItem(quitButton);

    scene->removeItem(inst);
}

但它同时出现。我该如何解决?

【问题讨论】:

  • @eyllanesc 哦,对不起,先生。我还是没查。我会检查并让您知道先生。感谢您的回答先生
  • @eyllanesc 先生,它不工作。 diaplayHome() 是 Game 类中的一个方法。它初始化需要在窗口中打开的 UI。先生,我正在制作游戏
  • 然后提供minimal reproducible example,显示你的游戏类。
  • @eyllanesc 我添加了游戏类。先生,你能检查一下吗?

标签: c++ qt


【解决方案1】:

您描述的行为没有被下面的完整示例重现

#include <QtWidgets/QApplication>
#include <qmainwindow.h>
#include <qwidget.h>
#include <qtimer.h>
#include <qsplashscreen.h>

QMainWindow* game;

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

    QSplashScreen *splash = new QSplashScreen;
    splash->setPixmap(QPixmap(":/StackOverflow/SplashScreen"));
    splash->show();

    game = new QMainWindow();       

    QTimer::singleShot(3500, splash, SLOT(close()));
    QTimer::singleShot(3500, game, SLOT(show()));

    return a.exec();
}

运行此程序会显示 3.5 秒的初始屏幕,然后是主窗口。问题可能是您对Game 类或成员函数displayHome() 的实现。

编辑

在使用Game 类定义和实现进行编辑后,很明显问题是在Game::Game() 构造函数的末尾调用show()。这会导致game 在构造时立即显示,随后在QTimer::singleShot(3500, game, SLOT(show())) 中调用show() 在已经可见的对象上是多余的。要解决这个问题,只需从 Game 构造函数中删除 show(),即它应该是

Game::Game(QWidget *parent)
{
    scene = new QGraphicsScene();
    scene->setSceneRect(0, 0, 800, 600);
    setBackgroundBrush(QBrush(QImage("img.png")));

    setScene(scene);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setFixedSize(800, 600);
}

【讨论】:

  • 我已经添加了splash screen 代码。以前只有Game *game = new Game();才能开始游戏。我会检查这个并让你知道先生。谢谢你的回答先生。
  • @TechGuy 如果您无法使用此处的 exact 代码重现我在答案中描述的行为,那么您需要查看如何编译和执行程序问题不在代码中。
  • @TechGuy 如果这正确回答了您的问题(有编辑),请不要忘记接受
  • 我还是没有检查这个。我会检查并告诉你。感谢您的回答和支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
相关资源
最近更新 更多