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()
    {
    }
  • 相关阅读:
    zoj 1239 Hanoi Tower Troubles Again!
    zoj 1221 Risk
    uva 10192 Vacation
    uva 10066 The Twin Towers
    uva 531 Compromise
    uva 103 Stacking Boxes
    稳定婚姻模型
    Ants UVA
    Golden Tiger Claw UVA
    关于upper、lower bound 的探讨
  • 原文地址:https://www.cnblogs.com/shichuan/p/4497921.html
Copyright © 2011-2022 走看看