QT4主窗口测试demo
立即下载
资源介绍:
QT4主窗口测试demo
#include "mainwindow.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("MyMainWindow"));
text = new QTextEdit(this);
setCentralWidget(text);
createActions();
createMenus();
createToolBars();
createStatusBar();
createDockWidget();
}
MainWindow::~MainWindow()
{
}
void MainWindow::createActions()
{
//open file action
actionOpenFile = new QAction(QIcon(":images/open.png"),tr("Open"),this);
actionOpenFile->setShortcut(QKeySequence(tr("Ctrl+O")));
actionOpenFile->setStatusTip(tr("open a file"));
connect(actionOpenFile,SIGNAL(triggered()),this,SLOT(slotOpenFile()));
//new file action
actionNewFile = new QAction(QIcon(":images/new.png"),tr("New"),this);
actionNewFile->setShortcut(QKeySequence(tr("Ctrl+N")));
actionNewFile->setStatusTip(tr("new file"));
connect(actionNewFile,SIGNAL(triggered()),this,SLOT(slotNewFile()));
//save file action
actionSaveFile = new QAction(QIcon(":images/save.png"),tr("Save"),this);
actionSaveFile->setShortcut(QKeySequence(tr("Ctrl+S")));
actionSaveFile->setStatusTip(tr("save file"));
connect(actionSaveFile,SIGNAL(activated()),this,SLOT(slotSaveFile()));
//exit action
actionExit = new QAction(tr("Exit"),this);
actionExit->setShortcut(QKeySequence(tr("Ctrl+Q")));
actionExit->setStatusTip(tr("exit"));
connect(actionExit,SIGNAL(triggered()),this,SLOT(close()));
//cut action
actionCut = new QAction(QIcon(":images/cut.png"),tr("Cut"),this);
actionCut->setShortcut(QKeySequence(tr("Ctrl+X")));
actionCut->setStatusTip(tr("cut to clipboard"));
connect(actionCut,SIGNAL(triggered()),text,SLOT(cut()));
//copy action
actionCopy = new QAction(QIcon(":images/copy.png"),tr("Copy"),this);
actionCopy->setShortcut(QKeySequence(tr("Ctrl+C")));
actionCopy->setStatusTip(tr("copy to clipboard"));
connect(actionCopy,SIGNAL(triggered()),text,SLOT(copy()));
//paste action
actionPaste = new QAction(QIcon(":images/paste.png"),tr("Paste"),this);
actionPaste->setShortcut(QKeySequence(tr("Ctrl+V")));
actionPaste->setStatusTip(tr("paste clipboard to selection"));
connect(actionPaste,SIGNAL(triggered()),text,SLOT(paste()));
//about action
actionAbtoutQt = new QAction(tr("About"),this);
connect(actionAbtoutQt,SIGNAL(triggered()),this,SLOT(slotAbout()));
}
void MainWindow::createMenus()
{
menuFile = menuBar()->addMenu(tr("File"));
menuFile->addAction(actionNewFile);
menuFile->addAction(actionOpenFile);
menuFile->addAction(actionSaveFile);
menuFile->addAction(actionExit);
menuEdit = menuBar()->addMenu(tr("Edit"));
menuEdit->addAction(actionCopy);
menuEdit->addAction(actionCut);
menuEdit->addAction(actionPaste);
menuAbout = menuBar()->addMenu(tr("Help"));
menuAbout->addAction(actionAbtoutQt);
}
void MainWindow::createToolBars()
{
toolBarFile = QMainWindow::addToolBar(tr("File"));
toolBarFile->setMovable(false);
toolBarFile->setAllowedAreas(Qt::AllToolBarAreas);
toolBarFile->addAction(actionNewFile);
toolBarFile->addAction(actionOpenFile);
toolBarFile->addAction(actionSaveFile);
toolBarEdit = QMainWindow::addToolBar(tr("Edit"));
addToolBar(Qt::TopToolBarArea,toolBarEdit);
toolBarEdit->setMovable(true);
toolBarEdit->setAllowedAreas(Qt::TopToolBarArea);
toolBarEdit->setFloatable(true);
QSize size(16,15);
toolBarEdit->setIconSize(size);
toolBarEdit->addAction(actionCopy);
toolBarEdit->addAction(actionCut);
toolBarEdit->addAction(actionPaste);
}
void MainWindow::createStatusBar()
{
QLabel * tipLabel = new QLabel(tr("ready"));
tipLabel->setAlignment(Qt::AlignHCenter);
tipLabel->setMinimumSize(tipLabel->sizeHint());
statusBar()->addWidget(tipLabel);
}
void MainWindow::createDockWidget()
{
dockWidget = new QDockWidget("Qock Wigdet TiTle",this);
dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
QTextEdit *textEdit = new QTextEdit;
textEdit->setText("This is a QTextEdit inside a QDockWidget");
dockWidget->setWidget(textEdit);
this->addDockWidget(Qt::LeftDockWidgetArea,dockWidget);
}
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::slotNewFile()
{
MainWindow *newWin = new MainWindow();
newWin->show();
}
void MainWindow::slotOpenFile()
{
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()
{
}