zoukankan      html  css  js  c++  java
  • Qt之主窗口设计——基于QMainWindow主窗口程序

    image

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QtGui>
    
    
    class MainWindow : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
    	~MainWindow();
    
    	void createMenus();		//创建菜单
    	void createActions();	//创建动作
    	void createToolBars();	//创建工具栏
    public slots:
    	void sl_NewFile();
    	void sl_OpenFile();
    	void sl_SaveFile();
    	void sl_About();
    protected:
    	void loadFile(QString);
    
    private:
    	QMenu *fileMenu;
    	QMenu *editMenu;
    	QMenu *aboutMenu;
    
    	QToolBar *fileTool;
    	QToolBar *editTool;
    
    	QAction *fileOpenAction;
    	QAction *fileNewAction;
    	QAction *fileSaveAction;
    	QAction *exitAction;
    
    	QAction *copyAction;
    	QAction *cutAction;
    	QAction *pasteAction;
    
    	QAction *aboutAction;
    
    	QTextEdit *edtText;	
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include <QTextStream>
    #include <QFile>
    
    MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
    	: QMainWindow(parent, flags)
    {
    	setWindowTitle(tr("QMainWindow"));
    	edtText = new QTextEdit(this);
    	setCentralWidget(edtText);
    
    	createActions();
    	createMenus();
    	createToolBars();
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::createActions()
    {
    	//"打开"动作
    	fileOpenAction = new QAction(QIcon("images/open.png"),tr("Open"),this);
    	fileOpenAction->setShortcut(tr("Ctrl+0"));
    	fileOpenAction->setStatusTip(tr("open a file"));
    	connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(sl_OpenFile()));
    
    	//"新建"动作
    	fileNewAction = new QAction(QIcon("images/new.png"),tr("New"),this);
    	fileNewAction->setShortcut(tr("Ctrl+N"));
    	fileNewAction->setStatusTip(tr("new file"));
    	connect(fileNewAction,SIGNAL(triggered()),this,SLOT(sl_NewFile()));
    
    	//"保存"动作
    	fileSaveAction = new QAction(QIcon("images/save.png"),tr("Save"),this);
    	fileSaveAction->setShortcut(tr("Ctrl+S"));
    	fileSaveAction->setStatusTip(tr("save file"));
    	connect(fileSaveAction,SIGNAL(activated()),this,SLOT(sl_SaveFile()));
    
    	//"退出"动作
    	exitAction = new QAction(tr("Exit"),this);
    	exitAction->setShortcut(tr("Ctrl+Q"));
    	exitAction->setStatusTip(tr("exit"));
    	connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));
    
    	//"剪切"动作
    	cutAction = new QAction(QIcon("images/cut.png"),tr("cut"),this);
    	cutAction->setShortcut(tr("Ctrl+X"));
    	cutAction->setStatusTip("cut to clipboard");
    	connect(cutAction,SIGNAL(triggered()),this,SLOT(cut()));
    
    	//"复制"动作
    	copyAction = new QAction(QIcon("images/copy.png"),tr("copy"),this);
    	copyAction->setShortcut(tr("Ctrl+C"));
    	copyAction->setStatusTip("copy to clipboard");
    	connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));
    
    	//"粘贴"动作
    	pasteAction = new QAction(QIcon("images/paste.png"),tr("paste"),this);
    	pasteAction->setShortcut(tr("Ctrl+V"));
    	pasteAction->setStatusTip("paste clipboard to selection");
    	connect(pasteAction,SIGNAL(triggered()),this,SLOT(paste()));
    
    	//"关于"动作
    	aboutAction = new QAction(tr("About"),this);
    	connect(aboutAction,SIGNAL(triggered()),this,SLOT(sl_About()));
    }
    
    void MainWindow::createMenus()
    {
    	//文件菜单
    	fileMenu = menuBar()->addMenu(tr("File"));
    	fileMenu->addAction(fileNewAction);
    	fileMenu->addAction(fileOpenAction);
    	fileMenu->addAction(fileSaveAction);
    	fileMenu->addSeparator();
    	fileMenu->addAction(exitAction);
    
    	//编辑菜单
    	editMenu = menuBar()->addMenu(tr("Edit"));
    	editMenu->addAction(copyAction);
    	editMenu->addAction(cutAction);
    	editMenu->addAction(pasteAction);
    
    	//帮助菜单
    	aboutMenu = menuBar()->addMenu(tr("Help"));
    	aboutMenu->addAction(aboutAction);
    }
    
    void MainWindow::createToolBars()
    {
    	//文件工具栏
    	fileTool = addToolBar("File");
    	fileTool->addAction(fileNewAction);
    	fileTool->addAction(fileOpenAction);
    	fileTool->addAction(fileSaveAction);
    	fileTool->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
    	fileTool->setMovable(false);
    
    	//编辑工具栏
    	editTool = addToolBar("Edit");
    	editTool->addAction(copyAction);
    	editTool->addAction(cutAction);
    	editTool->addAction(pasteAction);
    }
    
    void MainWindow::sl_NewFile()
    {
    	MainWindow *newWin = new MainWindow;
    	newWin->show();
    }
    
    void MainWindow::sl_OpenFile()
    {
    	QString fileName = QFileDialog::getOpenFileName(this);
    	if(!fileName.isEmpty())
    	{
    		if(edtText->document()->isEmpty())
    		{
    			loadFile(fileName);
    		}
    		else
    		{
    			MainWindow *newWin = new MainWindow;
    			newWin->show();
    			newWin->loadFile(fileName);
    		}
    	}
    }
    
    void MainWindow::sl_SaveFile()
    {
    
    }
    
    void MainWindow::sl_About()
    {
    
    }
    
    void MainWindow::loadFile(QString fileName)
    {
    	QFile file(fileName);
    	if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    	{
    		QTextStream textStream(&file);
    		while (!textStream.atEnd())
    		{
    			edtText->append(textStream.readLine());
    		}
    	}
    }

    工具条是一个可移动的窗口,它可停靠的区域由QToolBar的allowAreas()函数决定的,它的参数包括:

    Qt::LeftToolBarArea、Qt::RightToolBarArea、Qt::TopToolBarArea、Qt::BottomToolBarArea、Qt::AllToolBarArea.

    (默认为Qt::AllToolBarArea,启动时默认出现于主窗口的顶部)。

    setMovable()函数确定工具条的可移动性。默认参数为true,可移动。


  • 相关阅读:
    POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)
    HDU 2389 Rain on your Parade / HUST 1164 4 Rain on your Parade(二分图的最大匹配)
    CJOJ 1494 【网络流24题】 搭配飞行员(二分图最大匹配)
    Luogu 1402 酒店之王(二分图最大匹配)
    CJOJ 1943 【重庆八中模拟赛】寻找代表元(二分图最大匹配)
    Luogu 2756 飞行员配对方案问题(二分图最大匹配)
    Luogu 1559 运动员最佳匹配问题(带权二分图最大匹配)
    Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)
    Conda和Python的国内安装源
    快速安装Rainbond——开源企业级Paas平台
  • 原文地址:https://www.cnblogs.com/hanzhaoxin/p/2786283.html
Copyright © 2011-2022 走看看