qt(2)工具栏和菜单栏

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

/*namespace Ui {
class MainWindow;
}*/

class MainWindow : public QMainWindow
{
    Q_OBJECT
    /*
     * 一、只有加入了Q_OBJECT,你才能在类中使用QT中的signal和slot机制

           The Q_OBJECT macro at the beginning of the class definition is necessary for all classes that define signals or slots。
                 否则connect函数完全不起作用。

      二、使用QT国际化实现中文界面时,如果没有加入Q_OBJECT,有些类定义的文本不能被翻译。*/

public:
   /* explicit MainWindow(QWidget *parent = 0);*/
    MainWindow(QWidget *parent = 0);
   /*
    * class String
     2 {
     3 public:
     4     String(const char *str = NULL); // 普通构造函数*/
    ~MainWindow();

private:
    /*Ui::MainWindow *ui;*/
    void open();
   // void close();
    QAction *openAction;
    QAction *edit;
};

#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
/*#include "ui_mainwindow.h"*/
#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QToolBar>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)//MainWindow have a member QMainWindow
    /*ui(new Ui::MainWindow)*/

{
    /*ui->setupUi(this);*/
    setWindowTitle(tr("Main Window"));
    /*
     * 调用窗体类的setWindowTitle()函数可以对窗体设置标题。
      窗体类继承于QWidget类,其setWindowTitle函数原型:
void QWidget::setWindowTitle(const QString & title); //title是标题内容字符串。
示例:MyWidget.setWindowTitle(QString("我的窗体标题"));*/
    openAction = new QAction(/*QIcon(":/open.png"),*/tr("&Open..."),this);
    /*
     *

                         QIcon类代表图标,实现在 QtGui 共享库中。QIcon 对象可以认为是一系列图像的组合,每个图像代表窗口在某种状态下应该显示的图标。
                         QIcon 类支持以下构造函数:

                         QIcon();                         // 构造一个空图像构成的图标
                         QIcon(const QPixmap &pixmap);     // 从 Pixmap 对象构造函数
                         QIcon(const QString &filename);     // 从图像文件构造图标

                         当从 QPixmap 对象构造图标时,系统会自动产生窗口不同状态下对应的图像,比如窗口在禁用状态下其图标为灰色。
                         从文件构造图标时,文件并不是立刻加载,而是当图标要显示时才加载。

                         其中QPixmap类还不了解,QIcon(“d:\QtPicture\1.PNG”)这样就可以从图像文件中构造图标。
                         其他的现在还不太了解,等真正用到或者见到例子了在学习。另外QIcon里面还有一系列的成员函数,现在一样还不懂。
                         以 : 开始,意味着从资源文件中查找资源。:/images/doc-open就是找到了这里的 document-open.png 这个文件。

     * 一旦QAction被创建了,那么就必须将它添加到相关的菜单和工具栏上,然后将它们链接到实现相应action功能的槽函数上。*/
    openAction->setShortcuts(QKeySequence::Open);
    /*
     * setShortcuts()函数,用于说明快捷键。使用QKeySequence类来添加快捷键
    QAction::setShortcuts(QKeySequence::Open)在window平台的大多数软件等同于Ctrl+O,QKeySequence::New在window平台的大多数软件等同于Ctrl+N等等*/
    openAction->setStatusTip(tr("Open an existing file"));
    /*
     * setStatusTip函数。这是添加状态栏的提示语句*/
    connect(openAction,&QAction::triggered,this,&MainWindow::open);
    /*
     *链接到实现相应action功能的槽函数上。*/
    QMenu *file = menuBar()->addMenu(tr("&File"));
    /*
     * Menu菜单,MenuBar菜单栏,MenuItem菜单项*/
    file->addAction(openAction);
    QToolBar *toolBar = addToolBar(tr("&File"));
    toolBar->addAction(openAction);

    edit = new QAction(tr("&Close"),this);
    edit->setShortcut(QKeySequence::Close);
    edit->setStatusTip(tr("close the window"));
    connect(edit,&QAction::triggered,this,&MainWindow::close);
    QMenu *edits = menuBar()->addMenu(tr("&Close"));
    edits->addAction(edit);
    QToolBar *toolBars = addToolBar(tr("&Close"));
    toolBars->addAction(edit);

    statusBar();
    /*
     *
statusbar   状态列; 状态栏; 状态条*/
}

MainWindow::~MainWindow()
{
    /*delete ui;*/
}

void MainWindow::open()
{
    QMessageBox::information(this,tr("Information"),tr("Open"));
}

/*
 * void MainWindow::close()
{
    QMessageBox::information(this,tr("Information"),tr("Close"));
}*/

//main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    /*MainWindow w;
    w.show();*/
    MainWindow win;
    win.show();

    return a.exec();
}

猜你喜欢

转载自blog.csdn.net/sinat_39028599/article/details/80436222
今日推荐