简体   繁体   中英

Qt splashscreen won't close

I have a QSplashScreen made that runs through a bunch of images to resemble a gif and closes when the main window opens. This works fine on windows, but when I run it on mac it gets funky. Instead of closing when it's gone through all the pictures like it should it starts going through the images in revers order when clicked.

Here is header ( splashscreen.h ):

class SplashScreen : public QObject
{
    Q_OBJECT

public:
    explicit SplashScreen(QObject *parent = 0);

private:
    QString filename0;
    QString filename1;
    QString filename;

    int frameNum;

    Qt::WindowFlags flags;

private slots:
        void showFrame(void);
};

and here is implementation ( splashscreen.cpp ):

SplashScreen::SplashScreen(QObject *parent) :
    QObject(parent)
{
    QTimer *timer = new QTimer;
    timer->singleShot(0, this, SLOT(showFrame()));
    frameNum = 0;
}

void SplashScreen::showFrame(void)
{
    QSplashScreen *splash = new QSplashScreen;
    QTimer *timer = new QTimer;

    frameNum++;

    QString filename0 = ""; //first and second half of file path
    QString filename1 = "";
    splash->showMessage("message here", Qt::AlignBottom, Qt::black); 

    filename = filename0 + QString::number(frameNum) +filename1; // the number for the file is added here
    splash->setPixmap(QPixmap(filename)); // then shown in the splashscreen
    splash->show();

    if (frameNum < 90)
    {
        timer->singleShot(75, this, SLOT(showFrame()));
    }
    else if (frameNum == 90)
    {
        splash->close();
        flags |= Qt::WindowStaysOnBottomHint;
        return;
    }
}

and here is main file ( main.cpp ):

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

    SplashScreen *splash = new SplashScreen;
    QSplashScreen *splashy = new QSplashScreen;

    View view; //main window

    QTimer::singleShot(10000, splashy, SLOT(close()));
    splashy->hide();
    QTimer::singleShot(10000, &view, SLOT(show()));

    return app.exec();
}

I've got several different ways to close the splash screen but none of them seem to be working. Is this a bug in macs or is there something I can fix in my code?

There are created 90 different QSplashScreen objects. Only the 90th object is closed.

So, it is the main reason for observed behavior.

If you create a new splash screen QSplashScreen *splash = new QSplashScreen; for each frame then the previous screen should be closed and deleted. It is possible to store QSplashScreen *splash as a class member. Otherwise there is a memory leak.

You may consider to use only one instance of QSplashScreen splash as a private SplashScreen class member. The rest of the code may be unchanged (after replacement splash-> by splash. ). It will be automatically deleted with deletion of SplashScreen .


Other issues

QTimer should not be instantiated each time to use its static member function. Each call of showFrame() and SplashScreen() creates a new QTimer object that is never deleted and never used.

The splashy also does not make any sense in main() . All three lines related to splashy may be deleted. Actual splash screens are triggered by new SplashScreen . By the way, it is also a leak. In that case it makes sense to instantiate it directly on the main() function stack: SplashScreen splash;

It looks that the private member SplashScreen::flags is not used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM