qt中使用启动画面

                    在程序打开的时候,启动画面是很正常的。

                    对于这个qt提供了QSplashScreen类,可是我在使用过程中,他总是一闪而过,不是我们想要的。我们使用启动画面,如果没有模块检测,那我们只是想它显示几秒钟而已。下面是我的办法,继承QSplashScreen,在加个定时器就行了。

#ifndef SPLASHSCREEN_H

#define SPLASHSCREEN_H
 
#include <QSplashScreen>
#include <QTimer>
 
class splashScreen : public QSplashScreen
{
    Q_OBJECT
public:
    explicit splashScreen(const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0);
   // splashScreen(QWidget * parent, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0);
    void setShowSecond(int _second);//设置启动画面的显现时长,单位是秒,不是微秒
    void startTimer()  {show();timer.start();}//定时器开始工作
signals:
    void timeOver();//达到规定的显示时长发出此信号
public slots:
protected slots:
    void stopTimer();//关闭定时器
private:
    ~splashScreen(){}
    QTimer timer;//显示时长定时器
};
 
#endif // SPLASHSCREEN_H
 
#include "splashscreen.h"
splashScreen::splashScreen(const QPixmap &pixmap, Qt::WindowFlags f) :
    QSplashScreen(pixmap,f)
{
}
//splashScreen::splashScreen(QWidget * parent, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0):
//    QSplashScreen(parent, pixmap, f)
//{
//}
void splashScreen::setShowSecond(int _second)
{
    timer.setInterval(_second*1000);
    connect(&timer,SIGNAL(timeout()),this,SLOT(stopTimer()));
}
void splashScreen::stopTimer()
{
    timer.stop();
    emit timeOver();
    close();
    deleteLater();//此处是特意添加,只能在一次使用,且只能是在heap区使用
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPixmap pix(":/picture/splashScreen.jpg");
    splashScreen *ps = new splashScreen(pix);
    ps->setShowSecond(3);
    ps->startTimer();
    MainWindow w;
    QObject::connect(ps,SIGNAL(timeOver()),&w,SLOT(show()));
    w.resize(QApplication::desktop()->size()*0.9);//以设备的显示器件的大小来确定主界面的大小
    return a.exec();
}
为什么我想在启动画面结束之后之后直接释放,因为它就是这时候有用,我不想在程序运行结束的时候在释放,资源是宝贵的。

还有就是默认的鼠标点击操作是hide()。如果你不想不小心点了鼠标画面消失,并且主界面还没有出来,还是重写void mousePressEvent(QMousePressEvent *),函数体为空就行。

在C++和Qt中创建动态启动画面通常涉及到窗口管理、图形界面设计以及动画效果。以下是大致步骤: 1. **设置Qt环境**:首先确保已经安装了Qt开发环境,并配置好其集成开发环境(IDE),如Qt Creator。 2. **创建项目**:新建一个Qt Widgets Application项目,因为Qt提供了一套丰富的图形用户界面组件,适合制作启动画面。 3. **UI设计**:在`.ui`文件中使用Qt Designer设计启动画面的基本布局,例如可以包含一个图片区域用于显示动态加载的图像,还可以加入进度条或其他动画元素。 4. **QSplashScreen**:利用Qt的`QSplashScreen`来实现启动画面。这是一个特殊的窗口,会在应用程序启动时显示,直到主窗口准备好为止。你可以通过`show()`方法开始显示它,然后在后台执行一些初始化操作。 5. **加载资源**:将所需的图片或动态动画作为资源添加到项目中,使用`QPixmap`从资源中加载并显示。 6. **动画或进度更新**:如果需要动态效果,可以在后台线程中加载资源,然后定期更新`QSplashScreen`上的图像,或者使用进度条展示加载进度。 7. **隐藏启动画面**:当所有初始化工作完成后,使用` QSplashScreen::finish(QWidget *mainWindow)`方法关闭启动画面并切换到主窗口。 8. **错误处理**:别忘了添加适当的错误处理机制,比如在加载资源失败时提供反馈。 ```cpp // 示例代码片段 QSplashScreen splash; splash.setPixmap(QPixmap(":/resources/startup.png")); // 加载启动图片 splash.show(); QThread thread; Worker worker(&thread); // 创建一个工人线程来处理初始化任务 worker.moveToThread(&thread); thread.start(); // 主线程等待工人线程完成 worker.wait(); splash.finish(mainWindow); // 关闭启动画面 ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值