多文档(Multiple Documents)
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWin;
mainWin.show();
return app.exec();
}
这次创建MainWindow没有使用new .函数结束时MainWindow对象会自动销毁。
如果改为多窗口程序:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow *mainWin = new MainWindow;
mainWin->show();
return app.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("Close this window"));
connect(closeAction, SIGNAL(trigged()), this, SLOT(close()));
exitAction = new QAction(tr("E&xit"), this);
exitAction->setSHortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
...
}
启动界面(splash screens)
许多应用在启动是呈现一个启动界面。一些开发者使用启动界面装饰缓慢的启动,或者是市场部门满意。在Qt应用中增加启动界面是很容易的。
使用的类为QSplashScreen. QSplashScreen类显示在main window前。能够在image上写一些信息来通知用户当前的进度。
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/images/splash.png"));
splash->show();
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white);
MainWindow mainWin;
splash->showMessage(QObject::tr("Loading modules..."), topRight, Qt::white);
loadModules();
splash->showMessage(QObject::tr("Establishing connections..."), topRight, Qt::white);
establishConnections();
mainWin.show();
splash->finish(&mainWin);
delete splash;
return app.exec();
}