1.

2. mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QWorkspace>
#include <QMdiArea>
#include <QMdiSubWindow> class QAction;
class QMenu;
class QToolBar;
class QTextEdit; class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(); void createMenus();
void createActions();
void createToolBars();
void loadFile(QString fileName); public slots:
void slotNewFile();
void slotOpenFile();
void slotSaveFile();
void slotCopy();
void slotCut();
void slotPaste();
void slotAbout(); protected:
//void closeEvent(QCloseEvent *); private:
QTextCodec *codec;
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 * text; QWorkspace *workSpace;
}; #endif // MAINWINDOW_H

3. mainwindow.cpp

MainWindow构造函数中可加入 setAttribute(Qt::WA_DeleteOnClose);

多窗口时关闭窗口默认是隐藏,这样设置后为关闭,防止内存泄露。

#include "mainwindow.h"

#include <QtGui>

// 翋敦諳妗珋
MainWindow::MainWindow()
{
setWindowTitle(tr("myMainWindow"));
text = new QTextEdit(this);
setCentralWidget(text); /* workSpace = new QWorkspace;
setCentralWidget(workSpace);
QMainWindow *window1 = new QMainWindow;
window1->setWindowTitle("window1");
QTextEdit *text1 = new QTextEdit;
text1->setText("text1");
window1->setCentralWidget(text1);
workSpace->addWindow(window1);
*/
createActions();
createMenus();
createToolBars();
} void
MainWindow::createActions()
{ // file open action
fileOpenAction = new QAction(QIcon(":/images/open.png"),tr("Open"),this); // 湖羲恅璃
fileOpenAction->setShortcut(tr("Ctrl+O"));
fileOpenAction->setStatusTip(tr("open a file"));
connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(slotOpenFile())); // file new action
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(slotNewFile())); // save file action
fileSaveAction = new QAction(QPixmap(":/images/save.png"),tr("Save"),this); // 悵湔恅璃
fileSaveAction->setShortcut(tr("Ctrl+S"));
fileSaveAction->setStatusTip(tr("save file"));
connect(fileSaveAction,SIGNAL(activated()),this,SLOT(slotSaveFile())); // exit action
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(tr("cut to clipboard"));
connect(cutAction, SIGNAL(triggered()), text, SLOT(cut())); copyAction = new QAction(QIcon(":/images/copy.png"), tr("Copy"), this); // 葩秶
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip(tr("copy to clipboard"));
connect(copyAction, SIGNAL(triggered()), text, SLOT(copy())); pasteAction = new QAction(QIcon(":/images/paste.png"), tr("Paste"), this); // 梜泂
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("paste clipboard to selection"));
connect(pasteAction, SIGNAL(triggered()), text, SLOT(paste())); aboutAction = new QAction(tr("About"), this); // 壽衾
connect(aboutAction, SIGNAL(triggered()), this, SLOT(slotAbout()));
} void
MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("File"));
editMenu = menuBar()->addMenu(tr("Edit"));
aboutMenu = menuBar()->addMenu(tr("Help")); fileMenu->addAction(fileNewAction);
fileMenu->addAction(fileOpenAction);
fileMenu->addAction(fileSaveAction);
fileMenu->addAction(exitAction); editMenu->addAction(copyAction);
editMenu->addAction(cutAction);
editMenu->addAction(pasteAction); aboutMenu->addAction(aboutAction);
} void
MainWindow::createToolBars()
{ fileTool = addToolBar(tr("File"));
fileTool->setMovable(false);
editTool = addToolBar(tr("Edit")); fileTool->addAction(fileNewAction);
fileTool->addAction(fileOpenAction);
fileTool->addAction(fileSaveAction); editTool->addAction(copyAction);
editTool->addAction(cutAction);
editTool->addAction(pasteAction); } void MainWindow::slotNewFile()
{
MainWindow *newWin = new MainWindow();
newWin->show();
} void MainWindow::loadFile(QString fileName)
{
QFile file( fileName );
if ( file.open (QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream textStream( &file );
while( !textStream.atEnd() )
{
text->append( textStream.readLine() );
}
}
} void MainWindow::slotOpenFile()
{
QString fileName = QFileDialog::getOpenFileName(this);
if ( !fileName.isEmpty() )
{
if( text->document()->isEmpty() )
loadFile(fileName);
else
{
MainWindow *newWin = new MainWindow;
newWin->show();
newWin->loadFile(fileName);
}
}
} void MainWindow::slotSaveFile()
{
} void MainWindow::slotCopy()
{
} void MainWindow::slotCut()
{
} void MainWindow::slotPaste()
{
} void MainWindow::slotAbout()
{
}

QT 创建主窗口 MainWindow 实例的更多相关文章

  1. Qt实现应用程序单实例运行--LocalServer方式

    使Qt应用程序能够单实例运行的典型实现方法是使用共享内存实现.该方法实现简单,代码简洁. 但有一个致命缺陷:共享内存(QSharedMemory)实现的单程序运行,当运行环境是UNIX时,并且程序不幸 ...

  2. Qt创建堆叠窗口

    1.QT创建堆叠窗口使用类,但是使用它时主窗口不能是MainWindow,否则会出现布局错误,本例中使用基类为QDialog QStackedWidget 2.可以配合列表框QListWidget和Q ...

  3. SQLServer之创建主XML索引

    创建主XML索引注意事项 若要创建主 XML 索引,请使用 CREATE INDEX (Transact-SQL) Transact-SQL DDL 语句. XML 索引不完全支持可用于非 XML 索 ...

  4. Qt 之 设置窗口边框的圆角(使用QSS和PaintEvent两种方法)

    Qt在设置窗口边框圆角时有两种方式,一种是设置样式,另一种是在paintEvent事件中绘制窗口.下面分别叙述用这两种方式来实现窗口边框圆角的效果. 一.使用setStyleSheet方法 this- ...

  5. sql语句创建主键、外键、索引、绑定默认值

    use Mengyou88_Wuliu --创建公司表 create table dbo.Company2 ( CompanyID ,) not null, CompanyName ) null, A ...

  6. oracle创建主键序列和在ibatis中应用

    oracle创建主键序列 oracle主键序列的查询和ibitas中应用

  7. QT中关于窗口全屏显示与退出全屏的实现

    近期在学习QT时遇到了很多问题这也是其中一个,个人通过在各种书籍和网络上的查阅找到了一些关于这方面的答案,希望能给大家一些帮助. 首先,在QT中对于窗口显示常用的有这么几个方法可以调用: Qt全屏显示 ...

  8. Oracle创建主键自增表

    Oracle创建主键自增表   1.创建表    create table Test_Increase(            userid number(10) NOT NULL primary k ...

  9. oracle建表的时候同时创建主键,外键,注释,约束,索引

    --主键create table emp (id number constraint id_pr primary key ,name1 varchar(8));create table emp9 (i ...

随机推荐

  1. RTSP安防摄像机(海康大华宇视等)如何推送到RTMP流媒体服务器进行直播

    方案介绍 目前互联网直播的CDN和标准RTMP流媒体服务器通常只能接收RTMP格式的音视频推流.目前市场上有一些自带RTMP推流的摄像机和编码器,可以直接在其rtmp推流配置里面配置推送到RTMP流媒 ...

  2. python清除字符串中间空格的方法

    1.使用字符串函数replace >>> a = 'hello world' >>> a.replace(' ', '') 'helloworld' 看上这种方法真 ...

  3. UI auto程序结构组织方式

    UI Auto分三个layer: 1. Object finding – 单独一个类,寻找到控件.因为UI auto最容易改动的就是UI界面,这样全部放到一起就便于统一修改. 2. Task - 对控 ...

  4. 用JS改变的元素CSS样式,css里display :none 隐藏 block 显示

    CSS样式的引用有3种方式:style引用.class引用.id引用,所以js改变元素的样式我们也分3种来说. 1.js改变由style方式引用的样式:方法一:document.divs.style. ...

  5. 客户端-服务器通信安全 sign key

    API接口签名校验,如何安全保存appsecret? - 知乎  https://www.zhihu.com/question/40855191 要保证一般的客户端-服务器通信安全,可以使用3个密钥. ...

  6. CAP theorem

    https://en.wikipedia.org/wiki/CAP_theorem

  7. checkmarx使用笔记、原理

    checkmarks是一款商业的代码静态分析工具,和pmd类似的地方是他分析的是java文件,而非class文件.checkmarks使用 .net开发,必须安装在windows上,它的规则也是类似. ...

  8. docker介绍和简单使用

    docker介绍 docker 为什么会有docker出现? 一款产品从开发到上线,从操作系统到运行环境,再到应用配置,作为开发+运维直接的协作我们需要关心很多东西,这也是互联网公司不得 不面对问题, ...

  9. python 报错——Python TypeError: 'module' object is not callable 原因分析

    原因分析:Python导入模块的方法有两种: import module 和 from module import 区别是前者所有导入的东西使用时需加上模块名的限定,而后者则不需要 例: >&g ...

  10. phonegap file api

    https://github.com/chrisben/imgcache.js/tree/master/examples 1.FILE API file api最大的两个功能是download和upl ...