QT word初始界面
来源:互联网 发布:寒战 无间道 知乎 编辑:程序博客网 时间:2024/05/16 14:38
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QAction;
class QLabel;
class FindDialog;
class Spreadsheet;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
protected:
void closeEvent(QCloseEvent *event);
/*The closeEvent() function is a virtual function in QWidget that is automatically called when the user
closes the window. It is reimplemented in MainWindow so that we can ask the user the standard question
"Do you want to save your changes?" and to save user preferences to disk.*/
private slots:
void newFile();
void open();
bool save();
bool saveAs();
void find();
void goToCell();
void sort();
void about();
//Some menu options,
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
.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);
//creating a Spreadsheet widget and setting it to be the main window's central widget
createActions();
createMenus();
createContextMenu();
createToolBars();
createStatusBar();
readSettings();
findDialog = 0;
setWindowIcon(QIcon(":/images/icon.png"));
//QWidget::setWindowIcon() sets the icon shown in the top-left corner of the window.
setCurrentFile("");
}
There are several methods for providing images to the application:
1Storing images in files and loading them at run-time.
2Using Qt's resource mechanism.
the resource file spreadsheet.qrc, so we put the following line in the .pro file:
RESOURCES = spreadsheet.qrc
The resource file itself uses a simple XML format. Here's an extract from the one we have used:
<RCC>
<qresource>
<file>images/icon.png</file>
...
<file>images/gotocell.png</file>
</qresource>
</RCC>
Resource files are compiled into the application's executable, so they can't get lost. When we refer to
resources, we use the path prefix :/ (colon slash), which is why the icon is specified
as :/images/icon.png.
An action is an item that can be added to any number of menus and toolbars. Creating menus and toolbars in Qt involves these steps:
Create and set up the actions.
Create menus and populate them with the actions.
Create toolbars and populate them with the actions.
void MainWindow::createActions()
{
newAction = new QAction(tr("&New"), this);
newAction->setIcon(QIcon(":/images/new.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new spreadsheet file"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
...
for (int i = 0; i < MaxRecentFiles; ++i) {
recentFileActions[i] = new QAction(this);
recentFileActions[i]->setVisible(false);
connect(recentFileActions[i], SIGNAL(triggered()),
this, SLOT(openRecentFile()));
}
...
...
selectAllAction = new QAction(tr("&All"), this);
selectAllAction->setShortcut(QKeySequence::SelectAll);
selectAllAction->setStatusTip(tr("Select all the cells in the "
"spreadsheet"));
connect(selectAllAction, SIGNAL(triggered()),
spreadsheet, SLOT(selectAll()));
The New action has an accelerator (New), a parent (the main window), an icon, a shortcut key, and a status
tip.connect the action's triggered() signal to the main window's private newFile() slot,
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
/*In Qt, menus are instances of QMenu. The addMenu() function creates a QMenu widget with the specified
text and adds it to the menu bar. The QMainWindow::menuBar() function returns a pointer to a QMenuBar.
The menu bar is created the first time menuBar() is called.*/
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
separatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i)
fileMenu->addAction(recentFileActions[i]);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
toolsMenu = menuBar()->addMenu(tr("&Tools"));
toolsMenu->addAction(recalculateAction);
toolsMenu->addAction(sortAction);
}
void MainWindow::createContextMenu()
{
spreadsheet->addAction(cutAction);
spreadsheet->addAction(copyAction);
spreadsheet->addAction(pasteAction);
spreadsheet->setContextMenuPolicy(Qt::ActionsContextMenu);
}
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);
}
void MainWindow::createStatusBar()
{
locationLabel = new QLabel(" W999 ");
locationLabel->setAlignment(Qt::AlignHCenter);
locationLabel->setMinimumSize(locationLabel->sizeHint());
formulaLabel = new QLabel;
formulaLabel->setIndent(3);
statusBar()->addWidget(locationLabel);
statusBar()->addWidget(formulaLabel, 1);
connect(spreadsheet, SIGNAL(currentCellChanged(int, int, int, int)),
this, SLOT(updateStatusBar()));
connect(spreadsheet, SIGNAL(modified()),
this, SLOT(spreadsheetModified()));
updateStatusBar();
}
void MainWindow::updateStatusBar()
{
locationLabel->setText(spreadsheet->currentLocation());
formulaLabel->setText(spreadsheet->currentFormula());
}
The updateStatusBar() slot updates the cell location and the cell formula indicators.
- QT word初始界面
- Qt实现Word文档界面样式--QtitanRibbon
- DXUT 初始载入界面
- 初始界面UI
- QT窗体初始最大化
- 修改初始界面的背景图片
- windows8 初始界面和功能
- QSplitter分割界面 初始比例
- qt调整初始窗口大小
- Qt调整初始窗口大小
- android 初始界面 scrollTo无法执行
- Xcode6导航控制器:设置初始启动界面
- VS2008mfc文本文档初始界面最大化
- OJ界面之初始网站页面
- 初始qt,第一个编译错误
- Qt如何使窗体初始最大化
- QT 如何使窗体初始最大化
- 使用Qt的初始配置问题
- asp蜘蛛程序
- java基础复习1--基本数据类型、运算符号、表达式语句与程序结构
- 新的开始
- 珠海南方三层构架第一天
- 转:C#调用API的一些基础(希望有人用的上)
- QT word初始界面
- browser 支持java applet
- WinCE设置线程优先级影响
- [ChneChne的故事]青春人生之十年(附1:QQ空间里第一篇文章,也许也是最后一篇...--人生该是怎么样子的呢?)
- 兄弟,需要台灯么
- JSP+JDBC+SQLSERVER2000用户登录验证实例
- pager-taglib分页
- php 如何计算两天之间相隔多少天
- 查询文件版本【exe文件】经测试可用