3. Creating Main Windows
closeEvent() 是继承自QWidget 的虚函数,用户关闭窗口时被自动调用。
由signal 引发的slots 调用,slots 函数如有返回值会被忽略掉;而我们自已像调用普通C++ 函数一样调用slots,其返回值不会被忽略。
一个 #include <QtGui> 可以省掉非常多的#include
调用QWidget::setWindowIcon() 设置窗体的icon (显示在窗口左上角);不幸的是,它不是跨平台的。
资源文件(.qrc) 会嵌入到EXE 文件里面;资源文件可以是任意文件类型。
// MainWindow 派生自QMainWindow
MainWindow::MainWindow()
spreadsheet = new Spreadsheet; // Spreadsheet 派生自QTableWidget(此类拥有电子表格能力)
setCentralWidget(spreadsheet);
在构造函数中创建一个spreadsheet 对象,并将它设置成主窗体的"central widget","central widget" 占领主窗本的中间位置
Qt 支持的图片格式包括 BMP, GIF, JPEG, PNG, PNM, SVG, TIFF, XBM, and XPM
Creating Menus and Toolbars
Qt 通过action 概念简化了菜单和工具条的编程。一个action 就是菜单或工具条的一个Item。
MainWindow::createActions()
newAction = new QAction(tr("&New"), this); // 创建一个Action
newAction->setIcon(QIcon(":/images/new.png")); // 设置Action 的图片
newAction->setShortcut(QKeySequence::New); // 设置Action 的快捷键,Ctrl + N
newAction->setStatusTip(tr("Create a new spreadsheet file")); // 设置鼠标悬停时的提示文字
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile())); // 设置Action 被点时调用的函数
对于一个确定的命令,多数窗口系统拥有标准的键盘快捷键。比如说,“新建”命令对应的快捷键是Ctrl + N (Windows,KDE,和GNOME),而Mac OS X 则是Command+N。
通过使用适合的QKeySequence::StandardKey 类型的枚举值,可以确保Qt 在各平台提供正确的快捷键。
connect(selectAllAction, SIGNAL(triggered()),
spreadsheet, SLOT(selectAll()));
selectAll() slot 由 QTableWidget 类的袓先QAbstractItemView 提供,所以我们不用自已实现。
再来看看可选中的action(如果是菜单的话可能会有一个小勾勾,而工具条是以可按下的按钮)
showGridAction = new QAction(tr("&Show Grid"), this);
showGridAction->setCheckable(true);
showGridAction->setChecked(spreadsheet->showGrid());
showGridAction->setStatusTip(tr("Show or hide the spreadsheet's "
"grid"));
connect(showGridAction, SIGNAL(toggled(bool)),
spreadsheet, SLOT(setShowGrid(bool))); // 继承自QTableWidget 的setShowGrid(bool)
showGrid 这个action 选中时,Spreadsheet 组件显示为一个表格。
Qt 通过QActionGroup 类支持互斥的action (一组action 中只能选其中一个)
MainWindow::createMenus()
fileMenu = menuBar()->addMenu(tr("&File"));
QMainWindow::menuBar() 函数返回一个QMenuBar 指针, menu bar 在第一次调用menuBar() 函数时创建
菜单条(QMenuBar) 包含菜单(QMenu),菜单包含action(QAction)
void MainWindow::createMenus()
separatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i)
fileMenu->addAction(recentFileActions[i]);
fileMenu->addSeparator();
插入两个separator,将相似的action 隔在一起。我们保存其中一个separator 的指针,这样就可以动态显示或隐藏(没有recent files 就不显示)
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
selectSubMenu = editMenu->addMenu(tr("&Select"));
selectSubMenu->addAction(selectRowAction);
在一个Menu 上插入子Menu
optionsMenu = menuBar()->addMenu(tr("&Options"));
// add action
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
// add action
在两个Menu 上插入Separator
MainWindow::createContextMenu()
spreadsheet->addAction(cutAction);
spreadsheet->addAction(copyAction);
// ...
添加右键菜单只要在一个Widget 上调用addAction 函数,上下文菜单在右击一个Widget 或按下特定的键时出现
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("&File"));
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction);
editToolBar = addToolBar(tr("&Edit"));
editToolBar->addAction(cutAction);
editToolBar->addAction(copyAction);
editToolBar->addAction(pasteAction);
editToolBar->addSeparator();
editToolBar->addAction(findAction);
editToolBar->addAction(goToCellAction);
}
创建工具条