首页 > 代码库 > 【QT】C++ GUI Qt4 学习笔记3
【QT】C++ GUI Qt4 学习笔记3
菜单界面的实现。
看书上第三章,好长,好多代码。我敲了半天,想看看效果,结果却显示不出来。仔细一看,发现spreadsheet的实现在第四章。郁闷....
又到官网上下代码,结果居然不能运行。难道是因为我的版本太高了?
只好自己改,把没实现的部分都先忽略掉,即忽略掉具体的功能,只是显示菜单。折腾了半天,搞定了。
总结一下:
创建菜单:
需要再主窗口类中声明
1.QMenu * 代表一个菜单
2.QAction * 代表菜单中的一个动作 一般一个菜单里会有很多歌动作
3.每个动作对应的槽函数
然后在实现中:
QMenu 的创建,拿fileMenu举例:
主要是1.在窗口中生成菜单 2.添加动作
//文件菜单 fileMenu = menuBar()->addMenu(tr("&File")); //设置显示的名称 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);
QAction 的创建,主要包括:
1.新建动作
2.设置动作的图标
3.设置快捷键
4.设置提示 (好奇怪,我设置的提示都显示不出来)
5.添加信号和槽的连接
拿newAction举例:
newAction = new QAction(tr("&New"),this); newAction->setIcon(QIcon(":file/images/icon.jpg")); newAction->setShortcut(QKeySequence::New); //有标准化序列的 没有就用 tr("Ctrl + Q") 之类的 newAction->setStatusTip(tr("Create a new spreadsheet file")); connect(newAction, SIGNAL(triggered()),this, SLOT(newFile()));
功能函数:
由于只看看样子,都设成空函数就好了。
创建工具栏:
需要声明 QToolBar *
需要的动作和上面是一样的
创建:
fileToolBar = addToolBar(tr("&File")); fileToolBar->addAction(newAction); fileToolBar->addAction(openAction); fileToolBar->addAction(saveAction);
创建状态栏:
用 QLabel
代码如下:
mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"#include "finddialog.h"#include "gotocelldialog.h"#include "sortdialog.h"MainWindow::MainWindow(){ //spreadsheet = new Spreadsheet; //setCentralWidget(spreadsheet); //设置sheet为主窗口的中央窗口 //创建主窗口的其他部分 createActions(); createMenus(); //createContextMenu(); createToolBars(); //createStatusBar(); //读取应用程序存储的设置 //readSettings(); //find 初始化为空指针 //findDialog = 0; setWindowIcon(QIcon(":file/images/icon.jpg")); //setCurrentFile("");}void MainWindow::createActions(){ /**************File 菜单动作创建***************************/ //新建 newAction = new QAction(tr("&New"),this); newAction->setIcon(QIcon(":file/images/icon.jpg")); newAction->setShortcut(QKeySequence::New); newAction->setStatusTip(tr("Create a new spreadsheet file")); connect(newAction, SIGNAL(triggered()),this, SLOT(newFile())); //打开 openAction = new QAction(tr("&Open"),this); openAction->setIcon(QIcon(":file/images/icon.jpg")); openAction->setShortcut(QKeySequence::Open); openAction->setStatusTip(tr("Open a file")); connect(openAction, SIGNAL(triggered()),this, SLOT(open())); //保存 saveAction = new QAction(tr("&Save"),this); saveAction->setIcon(QIcon(":file/images/icon.jpg")); saveAction->setShortcut(QKeySequence::Save); saveAction->setStatusTip(tr("Save the file")); connect(saveAction, SIGNAL(triggered()),this, SLOT(save())); //另存为 saveAsAction = new QAction(tr("&Save As"),this); saveAsAction->setIcon(QIcon(":file/images/icon.jpg")); saveAsAction->setShortcut(QKeySequence::SaveAs); saveAsAction->setStatusTip(tr("Save the file As")); connect(saveAsAction, SIGNAL(triggered()),this, SLOT(saveAs())); //最近打开的文件 for(int i = 0; i < MaxRecentFiles; i++) { recentFileActions[i] = new QAction(this); recentFileActions[i]->setVisible(false); connect(recentFileActions[i], SIGNAL(triggered()),this,SLOT(openRecentFile())); } //退出 exitAction = new QAction(tr("E&xit"),this); exitAction->setShortcut(tr("Ctrl+Q"));//没有终止程序的标准化序列键 需要明确指定 exitAction->setStatusTip(tr("Exit the application")); connect(exitAction, SIGNAL(triggered()),this,SLOT(close())); /********************Edit 菜单动作创建***************************/ cutAction = new QAction(tr("Cu&t"), this); cutAction->setIcon(QIcon(":file/images/icon.jpg")); cutAction->setShortcut(QKeySequence::Cut); cutAction->setStatusTip(tr("Cut the current selection‘s contents " "to the clipboard")); // connect(cutAction, SIGNAL(triggered()), spreadsheet, SLOT(cut())); copyAction = new QAction(tr("&Copy"), this); copyAction->setIcon(QIcon(":file/images/icon.jpg")); copyAction->setShortcut(QKeySequence::Copy); copyAction->setStatusTip(tr("Copy the current selection‘s contents " "to the clipboard")); // connect(copyAction, SIGNAL(triggered()), spreadsheet, SLOT(copy())); pasteAction = new QAction(tr("&Paste"), this); pasteAction->setIcon(QIcon(":file/images/icon.jpg")); pasteAction->setShortcut(QKeySequence::Paste); pasteAction->setStatusTip(tr("Paste the clipboard‘s contents into " "the current selection")); // connect(pasteAction, SIGNAL(triggered()), // spreadsheet, SLOT(paste())); deleteAction = new QAction(tr("&Delete"), this); deleteAction->setShortcut(QKeySequence::Delete); deleteAction->setStatusTip(tr("Delete the current selection‘s " "contents")); //connect(deleteAction, SIGNAL(triggered()), // spreadsheet, SLOT(del())); //全选 selectAllAction = new QAction(tr("&All"),this); selectAllAction->setShortcut(QKeySequence::SelectAll); selectAllAction->setStatusTip(tr("Select all the cells in the ‘spreadsheet‘")); connect(selectAllAction, SIGNAL(triggered()),this, SLOT(selectAll())); //Qt已经实现了全选 selectRowAction = new QAction(tr("&Row"), this); selectRowAction->setStatusTip(tr("Select all the cells in the " "current row")); //connect(selectRowAction, SIGNAL(triggered()), // spreadsheet, SLOT(selectCurrentRow())); selectColumnAction = new QAction(tr("&Column"), this); selectColumnAction->setStatusTip(tr("Select all the cells in the " "current column")); //connect(selectColumnAction, SIGNAL(triggered()), // spreadsheet, SLOT(selectCurrentColumn())); findAction = new QAction(tr("&Find..."), this); findAction->setIcon(QIcon(":file/images/icon.jpg")); findAction->setShortcut(QKeySequence::Find); findAction->setStatusTip(tr("Find a matching cell")); connect(findAction, SIGNAL(triggered()), this, SLOT(find())); goToCellAction = new QAction(tr("&Go to Cell..."), this); goToCellAction->setIcon(QIcon(":file/images/icon.jpg")); goToCellAction->setShortcut(tr("Ctrl+G")); goToCellAction->setStatusTip(tr("Go to the specified cell")); connect(goToCellAction, SIGNAL(triggered()), this, SLOT(goToCell())); /********************Tools 菜单动作创建***************************/ recalculateAction = new QAction(tr("&Recalculate"), this); recalculateAction->setShortcut(tr("F9")); recalculateAction->setStatusTip(tr("Recalculate all the " "spreadsheet‘s formulas")); // connect(recalculateAction, SIGNAL(triggered()), // spreadsheet, SLOT(recalculate())); sortAction = new QAction(tr("&Sort..."), this); sortAction->setStatusTip(tr("Sort the selected cells or all the " "cells")); // connect(sortAction, SIGNAL(triggered()), this, SLOT(sort())); /********************options 菜单动作创建***************************/ //显示网格 用切换按钮 toggle 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)),this, SLOT(setShowGrid(bool))); autoRecalcAction = new QAction(tr("&Auto-Recalculate"), this); autoRecalcAction->setCheckable(true); //autoRecalcAction->setChecked(spreadsheet->autoRecalculate()); autoRecalcAction->setStatusTip(tr("Switch auto-recalculation on or " "off")); //connect(autoRecalcAction, SIGNAL(toggled(bool)), // spreadsheet, SLOT(setAutoRecalculate(bool))); aboutAction = new QAction(tr("&About"), this); aboutAction->setStatusTip(tr("Show the application‘s About box")); //connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); aboutQtAction = new QAction(tr("About &Qt"), this); aboutQtAction->setStatusTip(tr("Show the Qt library‘s About box")); //connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));}void MainWindow::createMenus(){ //文件菜单 fileMenu = menuBar()->addMenu(tr("&File")); 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); //编辑菜单 editMenu = menuBar()->addMenu(tr("&Edit")); editMenu->addAction(cutAction); editMenu->addAction(copyAction); editMenu->addAction(pasteAction); editMenu->addAction(deleteAction); selectSubMenu = editMenu->addMenu(tr("&Select")); //edit菜单的子菜单 selectSubMenu->addAction(selectRowAction); selectSubMenu->addAction(selectColumnAction); selectSubMenu->addAction(selectAllAction); editMenu->addSeparator(); editMenu->addAction(findAction); editMenu->addAction(goToCellAction); //工具菜单 toolsMenu = menuBar()->addMenu(tr("&Tools")); toolsMenu->addAction(recalculateAction); toolsMenu->addAction(sortAction); //options 菜单 optionsMenu = menuBar()->addMenu(tr("&Options")); optionsMenu->addAction(showGridAction); optionsMenu->addAction(autoRecalcAction); menuBar()->addSeparator(); //help 菜单 helpMenu = menuBar()->addMenu(tr("&Help")); helpMenu->addAction(aboutAction); helpMenu->addAction(aboutQtAction);}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();}bool MainWindow::okToContinue(){ if (isWindowModified()) { int r = QMessageBox::warning(this, tr("Spreadsheet"), tr("The document has been modified.\n" "Do you want to save your changes?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); if (r == QMessageBox::Yes) { return save(); } else if (r == QMessageBox::Cancel) { return false; } } return true;}void MainWindow::newFile(){ if (okToContinue()) { }}void MainWindow::open(){ if (okToContinue()) { }}bool MainWindow::save(){ if (curFile.isEmpty()) { return saveAs(); } else { return saveFile(curFile); }}bool MainWindow::saveAs(){ return false;}void MainWindow::openRecentFile(){}bool MainWindow::saveFile(const QString &fileName){ return true;}
mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QMessageBox>class QAction;class QLabel;class FindDialog;class Spreadsheet;class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow();//protected: // void closeEvent(QCloseEvent * event); //QWidget类中的虚函数 重新实现 可以询问是否保存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;// QStringList recentFiles; QString curFile; /**********菜单栏*************/ QMenu *fileMenu; QMenu *editMenu; QMenu *selectSubMenu; QMenu *toolsMenu; QMenu *optionsMenu; QMenu *helpMenu; enum{MaxRecentFiles = 5}; QAction *recentFileActions[MaxRecentFiles]; QAction *separatorAction; /*********工具栏**********/ QToolBar * fileToolBar; QToolBar * editToolBar; /***********状态栏*********/ QLabel * locationLabel; QLabel * formulaLabel; //file菜单选项 QAction * newAction; QAction * openAction; QAction * saveAction; QAction * saveAsAction; QAction * exitAction; //edit菜单选项 QAction * cutAction; QAction * copyAction; QAction * pasteAction; QAction * deleteAction; QAction * selectColumnAction; QAction * selectRowAction; QAction * selectAllAction; QAction * findAction; QAction * goToCellAction; //tools菜单选项 QAction * recalculateAction; QAction * sortAction; //options菜单选项 QAction * showGridAction; QAction * autoRecalcAction; //help菜单选项 QAction * aboutAction; QAction * aboutQtAction;};#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}
效果如下:我偷懒,所有的图标都用同一张图。图是我用processing画的,是匡的第一个拼音K,哈哈。
【QT】C++ GUI Qt4 学习笔记3
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。