菜单界面的实现。

看书上第三章,好长,好多代码。我敲了半天,想看看效果,结果却显示不出来。仔细一看,发现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 菜单动作创建***************************///显示网格  用切换按钮 toggleshowGridAction = 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,哈哈。

转载于:https://www.cnblogs.com/dplearning/p/4073874.html

【QT】C++ GUI Qt4 学习笔记3相关推荐

  1. Qt 5.9.5学习笔记第三节课

    Qt 5.9.5学习笔记第三节课 学习目标 1.Qt资源图标添加和使用 1.1添加资源文件 1.2qmake让资源文件生效 1.3修改widget应用程序窗口ICO 1.3.1第一种方法 1.3.2 ...

  2. python的messagebox的用法_Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法详解...

    本文实例讲述了Python GUI编程学习笔记之tkinter中messagebox.filedialog控件用法.分享给大家供大家参考,具体如下: 相关内容: messagebox 介绍 使用 fi ...

  3. Java之GUI编程学习笔记六 —— AWT相关(画笔paint、鼠标监听事件、模拟画图工具)

    Java之GUI编程学习笔记六 -- AWT相关(画笔paint) 参考教程B站狂神https://www.bilibili.com/video/BV1DJ411B75F 了解paint Frame自 ...

  4. matlab 轴gui,MatlabüGUI学习笔记(4)——公共对象属性的轴,MatlabGUI,四,常用,之,Axes...

    Matlab_GUI学习笔记(四)--常用对象的属性之Axes 1. Axes Axes意为"轴",是axis的复数.使用get函数查看Axes对象的属性,有一些属性与Figure ...

  5. 基于《狂神说java》GUI编程--学习笔记

    前言: 本笔记参考于学友:小尹^_^ :本笔记仅做学习与复习使用,不存在刻意抄袭. ---------------------------------------------------------- ...

  6. python gui tkinter_Python GUI tkinter 学习笔记(一)

    第一个python程序 #!/usr/bin/python # -*- coding: UTF-8 -*- #在2.x版本上,编写为:from Tkinter import * #在3.x版本上,编写 ...

  7. Qt 串口类QSerialPort 学习笔记

    一.串口类简介 当前的QtSerialPort模块中提供了两个C++类,分别是QSerialPort 和QSerialPortInfo. QSerialPort 类提供了操作串口的各种接口. QSer ...

  8. GUI guider学习笔记1

    1.GUI Guider概述 GUI Guider是恩智浦新近推出的一个PC端开发工具,专门用于LVGL(light and Versatile Graphics Library)GUI开发. 同其他 ...

  9. C++基于ffmpeg和QT开发播放器~学习笔记

    C++基于ffmpeg和QT开发播放器 B站网址 https://www.bilibili.com/video/BV1h44y1t7D8?p=2&spm_id_from=pageDriver ...

最新文章

  1. Spring4 MVC Hibernate4集成
  2. FICO 最常用配置表
  3. 目标检测--边界框(bounding box)解析
  4. java之通过FileChannel实现文件复制
  5. 浅谈Java中的hashcode方法
  6. leetcode题解25-K个一组翻转链表
  7. ndr4108贴片晶振是多少频率_流处理器、核心频率、 位宽……这些显卡参数你知道吗?—— 电脑硬件科普篇(八)...
  8. java冒泡排序经典代码_15道经典Java算法题(含代码) 建议收藏
  9. 日期无忧,Python计算日期清单
  10. Github 插件之 Octotree 报错介绍与解决
  11. MTK 9.0平台调试gsensor
  12. DevOps—持续部署Ansible(二)
  13. 获取json文件中的URL
  14. 文件操作细致详解(下)
  15. 编译ASP.NET网站项目,以及部署网站到本地localhost服务器上实现独立运行
  16. springboot+thymeleaf访问绝对路径图片、springboot配置虚拟路径
  17. 修改Tomcat的端口号方法
  18. 本地HTML访问REST服务的实现
  19. 邮件营销软件怎么样?
  20. 电阻、电容、电感的实际等效模型

热门文章

  1. 联想z5 Android 9.0,联想Z5 Pro(安卓9.0)刷机教程 联想Z5 Pro刷机图解
  2. 代码Service方法都报 Parameter 0 of constructor in com.fan.xx required a single bean, but 2 were found原因分析
  3. 英语口语练习四之 You/We may/might (just) as well... (我们不妨……)用法
  4. mysql用insert into select 语句插入数据
  5. 计算机毕业设计(附源码)python医院预约挂号系统
  6. 台式电脑没鼠标怎么移动光标_没有鼠标怎么移动光标【设置措施】
  7. 抗战电影中出场率很高的边三轮为什么能在软件界能混的风生水起
  8. 大田智能灌溉系统解决方案
  9. 平塘天眼和大数据有什么关系_聊聊平塘“天眼”的那些事儿,“FAST”到底有多牛?...
  10. PQ和HLG标准及其转换