QT 创建主窗口 MainWindow 实例
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 实例的更多相关文章
- Qt实现应用程序单实例运行--LocalServer方式
使Qt应用程序能够单实例运行的典型实现方法是使用共享内存实现.该方法实现简单,代码简洁. 但有一个致命缺陷:共享内存(QSharedMemory)实现的单程序运行,当运行环境是UNIX时,并且程序不幸 ...
- Qt创建堆叠窗口
1.QT创建堆叠窗口使用类,但是使用它时主窗口不能是MainWindow,否则会出现布局错误,本例中使用基类为QDialog QStackedWidget 2.可以配合列表框QListWidget和Q ...
- SQLServer之创建主XML索引
创建主XML索引注意事项 若要创建主 XML 索引,请使用 CREATE INDEX (Transact-SQL) Transact-SQL DDL 语句. XML 索引不完全支持可用于非 XML 索 ...
- Qt 之 设置窗口边框的圆角(使用QSS和PaintEvent两种方法)
Qt在设置窗口边框圆角时有两种方式,一种是设置样式,另一种是在paintEvent事件中绘制窗口.下面分别叙述用这两种方式来实现窗口边框圆角的效果. 一.使用setStyleSheet方法 this- ...
- sql语句创建主键、外键、索引、绑定默认值
use Mengyou88_Wuliu --创建公司表 create table dbo.Company2 ( CompanyID ,) not null, CompanyName ) null, A ...
- oracle创建主键序列和在ibatis中应用
oracle创建主键序列 oracle主键序列的查询和ibitas中应用
- QT中关于窗口全屏显示与退出全屏的实现
近期在学习QT时遇到了很多问题这也是其中一个,个人通过在各种书籍和网络上的查阅找到了一些关于这方面的答案,希望能给大家一些帮助. 首先,在QT中对于窗口显示常用的有这么几个方法可以调用: Qt全屏显示 ...
- Oracle创建主键自增表
Oracle创建主键自增表 1.创建表 create table Test_Increase( userid number(10) NOT NULL primary k ...
- oracle建表的时候同时创建主键,外键,注释,约束,索引
--主键create table emp (id number constraint id_pr primary key ,name1 varchar(8));create table emp9 (i ...
随机推荐
- Linux 并发服务器雏形总结
如下介绍一个并发回射客户端/服务器的雏形,所谓回射:就是客户端输入一条数据,服务器端读取并显示,然后服务器端再把刚读取的信息发送回客户端进行显示.示意图如下: 所谓并发服务器:就是一个服务器可以同时为 ...
- android菜鸟学习笔记28----Android中的Service生命周期及本地和远程服务绑定的实现
Service是Android中长期在后台运行的没有界面的组件,使用服务的优势在于:能够提高进程的优先级,系统不容易回收掉进程,即便回收了,内存充足的时候,会把进程重新创建. 1.服务的简单使用示例: ...
- JQUERY几个操作action,随意记录
.click:鼠标单击 .mouseenter 鼠标滑动
- 九度OJ 1360:乐透之猜数游戏 (递归)
时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:955 解决:261 题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些 ...
- curl学习总结
1.接口 function interface($postfields=array(),$url){ //设置post请求HTTP头字段的数组 $httpheader ...
- python3 中引用 HTMLTestRunner.py 模块的注意事项
HTMLTestRunner.py支持python2中运行,如果在python3.6.2中引用HTMLTestRunner.py模块,需要做一下更改: 1.更改HTMLTestRunner.py模块中 ...
- Android:日常学习笔记(6)——探究活动(4)
Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...
- 【转载】openwrt框架分析
文章出处:http://blog.csdn.net/kingvenll/article/details/27545221 这次讲讲openwrt的结构. 1. 代码上来看有几个重要目录package, ...
- 苹果终端wifi图标点亮慢和portal弹窗机制分析以及处理办法和建议
转:http://kms.h3c.com/View.aspx?id=52875 问题现象 在无线环境中经常碰到苹果终端连接无线后wifi图标无法点亮导致终端无法上网.在起portal的网络中认证页面无 ...
- Class文件结构(更新中)
Class文件是一组以8位字节为单位的二进制流,当遇见需要占用8位字节以上空间的数据项时,则会按照高位在前的方式分割成若干个8位字节进行存储. 格式,采用伪结构,只有两种数据结构:无符号数和表. 无符 ...