zoukankan      html  css  js  c++  java
  • 主窗口类

    QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar)、及一个中心部件(central widget),是许多应用程序的基础,如文本编辑器等。
     
     
     
    QMainWindow中菜单需要QMenu类和QAction类来实现。
    QAction类定义了菜单的具体行为。
    QMainWindow中提供了menuBar()函数返回一个menuBar。
    通过调用menuBar的addMenu函数就可以生成一个新的菜单项。
    QMenu类addAction函数为菜单指定一个QAction。
    QMainWindow中提供了自己的布局控件,所以不需要再为QMainWindow定义布局控件。
     
     
    新建Qt  应用,基类选择“QMainWindow”,取消“创建界面”复选框的选中状态。在mainwindow.h中添加如下代码
    #include <QMainWindow>
    #include <QAction>
    #include <QMenu>
    #include <QTextEdit>
    #include <QMenuBar>
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
        QAction *open;
        QMenu *menu;
        QTextEdit *text;
    private slots:
        void openfile();
    };
    在mainwindow.cpp中添加如下代码
    #include "mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        open = new QAction(tr("打开"), this);
        open->setShortcut(tr("Ctrl+O"));
        connect(open, SIGNAL(triggered()), this, SLOT(openfile()));
        menu= menuBar()->addMenu(tr("文件"));
        menu->addAction(open);
        menu->addSeparator();
        text = new QTextEdit(this);
        setCentralWidget(text);
    }
    void MainWindow::openfile()
    {
    }
  • 相关阅读:
    日志组件一:Log4j
    HTTPS加密那点事--轻松秒懂HTTPS非对称加密
    图解Git
    Python 迭代器 & __iter__方法
    Fiddler 抓包工具总结
    Python使用struct处理二进制(pack和unpack用法)
    Python binascii
    常见证书格式及相互转换
    MyBatis Generator 详解
    MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
  • 原文地址:https://www.cnblogs.com/shichuan/p/4497921.html
Copyright © 2011-2022 走看看