码迷,mamicode.com
首页 > Windows程序 > 详细

QMainWindow(二)

时间:2020-07-08 01:31:28      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:open   action   show   dmi   default   efi   project   bar   分割线   

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
    Q_OBJECT
public slots:
    void foo();
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    void QMenBar_init();
    void ToolBar_init();
    void DockWidget_init();//停靠窗口
    QAction *_open;
    QAction *_save;
    QAction *_exit;

    };

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include<QMenuBar>
#include<QMenu>
#include<QDebug>
#include<QToolBar>
#include<QStatusBar>
#include<QTextEdit>//多行文本
#include<QDockWidget>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QTextEdit *qtext=new QTextEdit(this);
 this->resize(600,480);
 QMenBar_init();
 ToolBar_init();
DockWidget_init();
 //this->statusBar();//状态栏,只能一个
  this->statusBar()->showMessage("就绪");
  //this->statusBar()->showMessage("就绪",5);//5秒后消失
 //this->statusBar()和this->menuBar()只需要调用就可以了,没必要用直至保存。
this->setCentralWidget(qtext);//设置中央窗口

}

 void MainWindow::DockWidget_init()
 {
     QTextEdit *qtext=new QTextEdit(this);
     QDockWidget *qd=new QDockWidget(this);
     this->addDockWidget(Qt::LeftDockWidgetArea,qd); //添加停靠窗口
     qd->setWidget(qtext);//窗口里面的类型,不固定
 }

void MainWindow::ToolBar_init()
{
QToolBar *  tb=this->addToolBar("a");//menuBar()只能添加一个,但是toolbar可以添加多个
//tb->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/4.png"),"打开");
//tb->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/2.png"),"保存");
//tb->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/3.png"),"取消",this,SLOT(close()));
//共用堆区,节省内存
tb->addAction(_open);
tb->addAction(_save);
tb->addAction(_exit);
}



void MainWindow::QMenBar_init()
{
QMenuBar * qm =this->menuBar();
QMenu * wen_jian  = new QMenu("文件(&F)",qm); //快捷方式"Alt+F"
QMenu*bian_ji   = new QMenu("编辑(&E)",qm);
QMenu * gou_jian= new QMenu("构建(&B)",qm);
QMenu* tiao_shi = new QMenu("调试(&D)",qm);
_open= wen_jian->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/4.png"),"打开");
_save=wen_jian->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/2.png"),"保存");
_exit=wen_jian->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/3.png"),"取消",this,SLOT(close()));


wen_jian->addAction("新建文件或项目(N...)",this,SLOT(foo()),QKeySequence("Ctrl+N"));
wen_jian->addSeparator(); //添加分割线
QMenu* zui_file=wen_jian->addMenu("最近访问的文件(F)");
zui_file->addAction(" ");
zui_file->addSeparator();
zui_file->addAction("清除菜单");

bian_ji->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/1.png"),"Undo");
tiao_shi->addAction("default")->setCheckable(true);


qm->addMenu(wen_jian);
qm->addMenu(bian_ji);
qm->addMenu(gou_jian);
qm->addMenu(tiao_shi);
}
void MainWindow::foo()
{
    qDebug()<<"打开成功"<<endl;
}
MainWindow::~MainWindow()
{

}

main.cpp:

#include "mainwindow.h"
#include <QApplication>

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

    return a.exec();
}

效果:

技术图片

 

QMainWindow(二)

标签:open   action   show   dmi   default   efi   project   bar   分割线   

原文地址:https://www.cnblogs.com/SunShine-gzw/p/13264302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!