zoukankan      html  css  js  c++  java
  • C++ GUI Qt 编程(第二版)第8章 Diagram_2(实现主对话框)

      现在要加上餐单和浮动对话框的快捷菜单,首先把你要用到的图标放到一个文件夹里面,后单击File and Class 选项中QT选项在你的右侧选择Qt Resource file 按照提示就可以。不会的参考以前章节的文章。

    main.cpp的源代码始终不变,现在我们要在上一次的代码中添加一写代码(蓝色)

    Diagramwindow.h的源代码为:

    #ifndef DIAGRAM_H

    #define DIAGRAM_H
    #include<QMainWindow>
    class QAction;
    class QGraphicsItem;
    class QGraphicsScene;
    class QGraphicsView;
    class Diagram : public QMainWindow
    {
        Q_OBJECT
    public:
        Diagram();
    private:

      void createActions();

        void createMenus();
        void createToolBars();
        QMenu *fileMenu;
        QMenu *editMenu;
        QToolBar *editToolBar;
        QAction *exitAction;
        QAction *addNodeAction;
        QAction *addLinkAction;
        QAction *deleteAction;
        QAction *cutAction;
        QAction *copyAction;
        QAction *pasteAction;
        QAction *bringToFrontAction;
        QAction *sendToBackAction;
        QAction *propertiesAction;
        QGraphicsScene *scene;
        QGraphicsView *view;
    };
    #endif // DIAGRAM_H
    
    

    Diagramwindow.cpp的源代码为:

    #include<QtGui>

    #include "Diagram.h"
    Diagram::Diagram()
    {
        scene = new QGraphicsScene(0, 0, 600, 500); new a scene
        view = new QGraphicsView;new a view
        view->setScene(scene);set the vscene in the view
        view->setDragMode(QGraphicsView::RubberBandDrag);set the view mode
        view->setRenderHints(QPainter::Antialiasing
                             | QPainter::TextAntialiasing);
        view->setContextMenuPolicy(Qt::ActionsContextMenu);
        setCentralWidget(view);

        createActions();

        createMenus();
        createToolBars();
    }

    void Diagram::createActions()

    {
        exitAction = new QAction(tr("E&xit"), this);
        exitAction->setShortcut(tr("Ctrl+Q"));
        connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
        addNodeAction = new QAction(tr("Add &Node"), this);
        addNodeAction->setIcon(QIcon(":/images/node.png"));
        addNodeAction->setShortcut(tr("Ctrl+N"));
        connect(addNodeAction, SIGNAL(triggered()), this, SLOT(addNode()));
        addLinkAction = new QAction(tr("Add &Link"), this);
        addLinkAction->setIcon(QIcon(":/images/link.png"));
        addLinkAction->setShortcut(tr("Ctrl+L"));
        connect(addLinkAction, SIGNAL(triggered()), this, SLOT(addLink()));
        deleteAction = new QAction(tr("&Delete"), this);
        deleteAction->setIcon(QIcon(":/images/delete.png"));
        deleteAction->setShortcut(tr("Del"));
        connect(deleteAction, SIGNAL(triggered()), this, SLOT(del()));
        cutAction = new QAction(tr("Cu&t"), this);
        cutAction->setIcon(QIcon(":/images/cut.png"));
        cutAction->setShortcut(tr("Ctrl+X"));
        connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
        copyAction = new QAction(tr("&Copy"), this);
        copyAction->setIcon(QIcon(":/images/copy.png"));
        copyAction->setShortcut(tr("Ctrl+C"));
        connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
        pasteAction = new QAction(tr("&Paste"), this);
        pasteAction->setIcon(QIcon(":/images/paste.png"));
        pasteAction->setShortcut(tr("Ctrl+V"));
        connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
        bringToFrontAction = new QAction(tr("Bring to &Front"), this);
        bringToFrontAction->setIcon(QIcon(":/images/bringtofront.png"));
        connect(bringToFrontAction, SIGNAL(triggered()),
                this, SLOT(bringToFront()));
        sendToBackAction = new QAction(tr("&Send to Back"), this);
        sendToBackAction->setIcon(QIcon(":/images/sendtoback.png"));
        connect(sendToBackAction, SIGNAL(triggered()),
                this, SLOT(sendToBack()));
        propertiesAction = new QAction(tr("P&roperties..."), this);
        connect(propertiesAction, SIGNAL(triggered()),
                this, SLOT(properties()));
    }
    void Diagram::createMenus()
    {
        fileMenu = menuBar()->addMenu(tr("&File"));
        fileMenu->addAction(exitAction);
        editMenu = menuBar()->addMenu(tr("&Edit"));
        editMenu->addAction(addNodeAction);
        editMenu->addAction(addLinkAction);
        editMenu->addAction(deleteAction);
        editMenu->addSeparator();
        editMenu->addAction(cutAction);
        editMenu->addAction(copyAction);
        editMenu->addAction(pasteAction);
        editMenu->addSeparator();
        editMenu->addAction(bringToFrontAction);
        editMenu->addAction(sendToBackAction);
        editMenu->addSeparator();
        editMenu->addAction(propertiesAction);
    }
    void Diagram::createToolBars()
    {
        editToolBar = addToolBar(tr("Edit"));
        editToolBar->addAction(addNodeAction);
        editToolBar->addAction(addLinkAction);
        editToolBar->addAction(deleteAction);
        editToolBar->addSeparator();
        editToolBar->addAction(cutAction);
        editToolBar->addAction(copyAction);
        editToolBar->addAction(pasteAction);
        editToolBar->addSeparator();
        editToolBar->addAction(bringToFrontAction);
        editToolBar->addAction(sendToBackAction);
    }
    现在编译就可以发现我的主程序有点结果比较丰满了点结果如下:
    diagram1 
     

     

    
    
    
  • 相关阅读:
    Pandas也能轻松绘图,简单而又漂亮
    笔试题: 二叉排序数左移k个
    补题next_permutation
    从HTTP到HTTPS
    HTTP首部字段详解
    HTTP请求方法及响应状态码详解
    HTTP报文格式详解
    TCP/IP网络基础
    Netty学习笔记
    ZooKeeper学习笔记
  • 原文地址:https://www.cnblogs.com/xmphoenix/p/1930601.html
Copyright © 2011-2022 走看看