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 ...
随机推荐
- CSV导出
CSV 导入导出工具类 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; impor ...
- 路径 php中'.'和'..'还有'./'和'../'
./当前目录(就是当前执行文件所在目录) ../上级目录 / 这个才是根目文件名/ 同级目录 例子如图 1.cart下的index.php 1)要引用Public->css->index. ...
- JavaScript方法splice()和slice()
1 splice() 1.1 说明 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目.该方法会改变原始数组.Link 1.2 语法 arrayObject.splice(inde ...
- Linux查找命令find、locate、whereis、which、type
find:查找指定下目录的文件 -empty:查找空文件 -name:按名字查找 -type f(文件 or d:目录):按类型查找
- python并发编程&IO模型
一 IO模型介绍 为了更好地了解IO模型,可先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(n ...
- Linux下环境变量配置错误 导致大部分命令不可以使用的解决办法
直接解决方法:在命令行中输入:export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 后 Enter
- 使用Kotlin开发Android应用(I):简介
Kotlin是一门基于JVM的编程语言,它正成长为Android开发中用于替代Java语言的继承者.Java是世界上使用最多的编程语言之一,当其他编程语言为更加便于开发者使用而不断进化时,Java并没 ...
- Eclipse解决JSP页面乱码问题
解决方案 1.首先选择 Window --> Preferences --> General--> Content Types --> text (点击左边的+,显示 ...
- java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
Links: 1.Getting availableProcessors is already set to [1], rejecting [1] IllegalStateException exce ...
- django 查询
mail = UserProfile.objects.get(email = email) get如果没有查询到会抛出一个不存在的异常 ...