3.1 继承QMainWindow类(Subclassing QMainWindow)

    技术2022-05-11  83

    3.1 继承QMainWindow

    一个应用程序的主窗口要从QMainWindow继承。我们在第二章看到的创建对话框的方法可以用来创建主窗口,QDialogQMainWindow都是继承自QWidget类。

    主窗口可用Qt Designer创建。但是在这一章,我们使用c++代码实现。如果你喜欢使用可视化的工具,可以参考在线手册“Creating Main Windows in Qt Designer”。

    SpreadSheet应用程序的主窗口类定义文件和实现文件分别在mainwindow.hmainwindow.cpp中,首先看头文件:

    #ifndef MAINWINDOW_H

    #define MAIhNWINDOW_H

    #include <QMainWindow>

    class QAction;

    class QLabel;

    class FindDialog;

    class Spreadsheet;

    class MainWindow : public QMainWindow

    {

        Q_OBJECT

    public:

        MainWindow();

    protected:

        void closeEvent(QCloseEvent *event);

    private slots:

        void newFile();

        void open();

        bool save();

        bool saveAs();

        void find();

        void goToCell();

        void sort();

    void about();

    void openRecentFile();

    void updateStatusBar();

    void spreadsheetModified();

    private:

    void createActions();

        void createMenus();

        void createContextMenu();

        void createToolBars();

        void createStatusBar();

        void readSettings();

        void writeSettings();

        bool okToContinue();

        bool loadFile(const QString &fileName);

        bool saveFile(const QString &fileName);

        void setCurrentFile(const QString &fileName);

        void updateRecentFileActions();

        QString strippedName(const QString &fullFileName);

        Spreadsheet *spreadsheet;

        FindDialog *findDialog;

        QLabel *locationLabel;

        QLabel *formulaLabel;

        QStringList recentFiles;

        QString curFile;

        enum { MaxRecentFiles = 5 };

        QAction *recentFileActions[MaxRecentFiles];

        QAction *separatorAction;

        QMenu *fileMenu;

        QMenu *editMenu;

       

        QToolBar *fileToolBar;

        QToolBar *editToolBar;

        QAction *newAction;

        QAction *openAction;

       

        QAction *aboutQtAction;

    };

    #endif

     

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

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

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

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

    下面来看源文件代码:

    #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.pngQt支持多种格式的图片文件,包括BMP, GIF, JPEG, PNG, PNM, XBM, XPM     。在QWidget::setWindowIcon()中设置的图标显示在程序主窗口的左上角。不过,Qt没有提供一个平台无关的程序的桌面图标。相关平台的处理方式可参考http://doc.trolltech.com/4.1/appicon.html.中说明。

    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。除图片外,资源可以是任何格式的文件,这将在第12章里介绍。

     

     

    最新回复(0)