(转)3.1从QMainWindow派生(Subclassing QMainWindow)

来源:互联网 发布:淘宝冷冻芝心薯球 编辑:程序博客网 时间:2024/04/30 17:28

从QMainWindow的派生类创建应用程序的主窗口。第二章中介绍的用于创建对话框的技术也适合创建主窗口,因为QDialog与QMainWindow都是从QWidget派生。

可以用Qt Designer创建主窗口,但本章还是解释用代码如何创建。如果你喜欢使用可视化的工具,可以参考在线手册“Creating Main Windows in Qt Designer”。

下面是mainwindow.h头文件:

1  #ifndef MAINWINDOW_H

2  #define MAINWINDOW_H

3  #include <QMainWindow>

4  class QAction;

5  class QLabel;

6  class FindDialog;

7  class Spreadsheet;

8   class MainWindow : public QMainWindow

9  {

10     Q_OBJECT

11 public:

12    MainWindow();

13 protected:

14    void closeEvent(QCloseEvent *event);

15 private slots:

16    void newFile();

17    void open();

18    bool save();

19    bool saveAs();

 20   void find();

 21   void goToCell();

 22   void sort();

 23   void about();

 24   void openRecentFile();

 25   void updateStatusBar();

 26   void spreadsheetModified();

 27 private:

 28   void createActions();

 29  void createMenus();

 30   void createContextMenu();

 31   void createToolBars();

 32   void createStatusBar();

 33  void readSettings();

 34   void writeSettings();

 35   bool okToContinue();

 36   bool loadFile(const QString &fileName);

 37   bool saveFile(const QString &fileName);

 38    void setCurrentFile(const QString &fileName);

 39   void updateRecentFileActions();

 40   QString strippedName(const QString &fullFileName);

 41   Spreadsheet *spreadsheet;

 42    FindDialog *findDialog;

 43   QLabel *locationLabel;

 44   QLabel *formulaLabel;

 45   QStringList recentFiles;

 46   QString curFile;

 47  enum { MaxRecentFiles = 5 };

 48   QAction *recentFileActions[MaxRecentFiles];

 49   QAction *separatorAction;

 50   QMenu *fileMenu;

 51   QMenu *editMenu;   

 52   QToolBar *fileToolBar;

 53   QToolBar *editToolBar;

 54   QAction *newAction;

 55   QAction *openAction;   

 56   QAction *aboutQtAction;

 57 };

 58 #endif 

我们定义MainWindow类继承自QMainWindow。因为它有自己的信号和槽,所以声明了Q_OBJECT宏。

closeEvent()是QWidget的虚函数,当用户关闭窗口时自动被调用。在MainWindow中它被重新实现,这样我们就可以提出用户一些常见的问题,如:是否保存所作的改变?,提示用户存盘。

有些菜单项,如File|New,Help|About等在MainWindow的私有槽中实现。多数的槽函数返回值为void,但是save()和saveAs()返回的值为bool型。当一个槽函数由信号引发时它的返回值被忽略,但是如果槽函数被当作普通函数被调用,这个返回值就可以象其他普通函数一样被得到

在这个类中还声明了很多其他的私有槽函数和私有函数实现用户界面的功能。除此之外还有很多私有变量,这些在使用的时候会解释。

下面来看源文件mainwindow.cpp代码:

#include <QtGui>

#include "finddialog.h"

#include "gotocelldialog.h"

#include "mainwindow.h"

#include "sortdialog.h"

#include "spreadsheet.h"

MainWindow::MainWindow()

{

    spreadsheet = new Spreadsheet;

    setCentralWidget(spreadsheet);

    createActions();

    createMenus();

    createContextMenu();

    createToolBars();

    createStatusBar();

    readSettings();

    findDialog = 0;

    setWindowIcon(QIcon(":/images/icon.png"));

    setCurrentFile("");

}

在包含文件中包含了<QtGUI>头文件,这包含了我们在这个类中使用的所有Qt类的定义。其他是第二章中定义的头文件,这里也使用了。

在构造函数中,我们开始创建SpreadSheet控件,并把这个控件做为主窗口的中心控件。这个控件占据主窗口的中间区域。SpreadSheet是一个QTableWidget子类,具有一些简单的列表功能,将会在第四章实现。

然后我们调用私有函数createActions(),createMenus(),createContext-Menu(),createToolBars()和createStatusBar()创建主窗口的其他部分。readSettings()读取程序保存在磁盘上的一些设置。

我们把findDialog指针为空,当MainWindow::find()第一次被调时,将会创建一个FindDialog对象。

最后,我们设置窗口的图标为icon.png。Qt支持多种格式的图片文件,包括BMP, GIF, JPEG, PNG, PNM, XBM, XPM     。调用QWidget::setWindowIcon()设置图标显示在程序主窗口的左上角。不过,Qt没有提供一个平台无关的程序的桌面图标。相关平台的处理方式可参考http://doc.trolltech.com/4.1/appicon.html.中说明。

*************************************************************

GIF格式文件QT默认情况不支持,因使用的压缩算法在有些国家有专利保护,如要使用,在编译Qt时,在configure的命令行参数中加入-qt-gif 选项。

*************************************************************

GUI程序通常会使用很多图片。提供图片的方式很多,主要有:

1、  把图片存储在文件中,程序运行时加载它们

2、  在源代码中包含XPM文件(这种文件是有效的c++文件)

3、  使用Qt提供的资源管理方案。

这里我们使用Qt提供的资源管理方案,因为它能够在运行时方便的加载图片文件,并支持以上文件格式。这里假设图片文件保存在应用程序源代码子目录images里面。

使用这个方案时,需要创建一个资源文件,并在.pro文件中添加这个资源文件的标识符。在这个例子中,定义资源文件为spreadsheet.qrc,在.pro文件中加入如下信息:

RESOURCES     = spreadsheet.qrc

在资源文件中使用了简单的XML格式:

<!DOCTYPE RCC><RCC version="1.0">

<qresource>

    <file>images/icon.png</file>

    ...

    <file>images/gotocell.png</file>

</qresource>

</RCC>

资源文件被编译到程序的可执行文件中,故它们不会丢失。使用资源时使用前缀:/。例如:/images/icon.png。除图片外,资源可以是任何格式的文件,并且可以用在Qt中需要任何文件名的地方。这将在第12章里介绍。

原创粉丝点击