Qt4读书笔记15

来源:互联网 发布:软件项目招标书 编辑:程序博客网 时间:2024/05/07 17:13

 

多文档(MultipleDocuments)

 

#include <QApplication>

#include "mainwindow.h"

 

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

{

       QApplicationapp(argc, argv);

       MainWindowmainWin;

       mainWin.show();

       returnapp.exec();

}

 

这次创建MainWindow没有使用new .函数结束时MainWindow对象会自动销毁。

 

如果改为多窗口程序:

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

{

       QApplicationapp(argc, argv);

       MainWindow*mainWin = new MainWindow;

       mainWin->show();

       returnapp.exec();

}

 

File|new修改:

void MainWindow::newFile()

{

       MainWindow*mainWin = new MainWindow;

       mainWin->show();

}

 

奇怪的是我们没有保存window的指针,这没有什么问题,Qt保留了;

 

void MainWindow::createActions()

{

       ...

       closeAction= new QAction(tr("&Close"), this);

       closeAction->setShortcut(QKeySequence::Close);

       closeAction->setStatusTip(tr("Closethis window"));

       connect(closeAction,SIGNAL(trigged()), this, SLOT(close()));

 

       exitAction= new QAction(tr("E&xit"), this);

       exitAction->setSHortcut(tr("Ctrl+Q"));

       exitAction->setStatusTip(tr("Exitthe application"));

       connect(exitAction,SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

       ...

}

 

启动界面(splashscreens)

许多应用在启动是呈现一个启动界面。一些开发者使用启动界面装饰缓慢的启动,或者是市场部门满意。在Qt应用中增加启动界面是很容易的。

 

使用的类为QSplashScreen.QSplashScreen类显示在mainwindow前。能够在image上写一些信息来通知用户当前的进度。

 

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

{

       QApplicationapp(argc, argv);

 

       QSplashScreen*splash = new QSplashScreen;

       splash->setPixmap(QPixmap(":/images/splash.png"));

       splash->show();

 

       Qt::AlignmenttopRight = Qt::AlignRight | Qt::AlignTop;

       splash->showMessage(QObject::tr("Settingup the main window..."), topRight, Qt::white);

      

       MainWindowmainWin; 

 

       splash->showMessage(QObject::tr("Loadingmodules..."), topRight, Qt::white);

 

       loadModules();

 

       splash->showMessage(QObject::tr("Establishingconnections..."), topRight, Qt::white);

       establishConnections();

 

       mainWin.show();

       splash->finish(&mainWin);

       deletesplash;

 

       returnapp.exec();

}