这个实验用Qt实现基本QMainWindow主窗口
先上实验效果图
打开一个文件,读取文件类容
详细步骤:
1.打开Qt creator新建MainWindow工程
右键工程名添加新文件,main文件,后缀自动补全后为main.cpp
选中工程,再次右键,选择模板的时候选择C++类,添加一个MainWindow类,该类继承字QMainWindow
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QApplication>
#include <QAction>
#include <QMenu>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QToolBar>
#include <QTextEdit>
#include <QMainWindow>
#include <QString>
#include <QMenuBar> class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow(); void createMenus();
void createActions();
void createToolBars();
void loadFile(QString fileName); public slots:
void slotNewFile();
void slotOpenFile();
void slotSaveFile(); private: QMenu *fileMenu;
QMenu *editMenu;
QMenu *aboutMenu;
QString fileName; QToolBar *fileTool;
QToolBar *editTool; QAction *fileOpenAction;
QAction *fileNewAction;
QAction *fileSaveAction;
QAction *exitAction;
QAction *copyAction;
QAction *cutAction;
QAction *pasteAction;
QAction *aboutAction; QTextEdit *text; }; #endif // MAINWINDOW_H
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent) {
//设置主窗口标题
setWindowTitle(tr("QMainWindow"));
text = new QTextEdit(this);
setCentralWidget(text); createActions();
createMenus();
createToolBars(); } MainWindow::~MainWindow()
{ } void MainWindow::createActions()
{
//通过图片路径来实现添加Qt资源文件目的
fileOpenAction = new QAction(QIcon("C:/qt/Mwindow/open.png"),tr("Open"),this);
fileOpenAction->setShortcut(tr("Ctrl+O"));
fileOpenAction->setStatusTip(tr("Open a file"));
connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(slotOpenFile())); fileNewAction = new QAction(QIcon("C:/qt/Mwindow/new.png"),tr("New"),this);
fileNewAction->setShortcut(tr("Ctrl+N"));
fileNewAction->setStatusTip(tr("new file"));
connect(fileNewAction,SIGNAL(triggered()),this,SLOT(slotNewFile())); fileSaveAction = new QAction(QIcon("C:/qt/Mwindow/save.png"),tr("Save"),this);
fileSaveAction->setShortcut(tr("Ctrl+S"));
fileNewAction->setStatusTip(tr("save file"));
connect(fileNewAction,SIGNAL(triggered()),this,SLOT(slotSaveFile())); 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("C:/qt/Mwindow/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("C:/qt/Mwindow/copy.png"),tr("Copy"),this);
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip(tr("copy to clipboard"));
connect(copyAction,SIGNAL(triggered()),this,SLOT(copy())); pasteAction = new QAction(QIcon("C:/qt/Mwindow/paste.png"),tr("paste"),this);
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("paste clipboard to selection"));
connect(pasteAction,SIGNAL(triggered()),this,SLOT(paste())); aboutAction = new QAction(tr("About"),this);
connect(aboutAction,SIGNAL(triggered()),this,SLOT(slotAbout())); } void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(fileNewAction);
fileMenu->addAction(fileOpenAction);
fileMenu->addAction(fileSaveAction);
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); editTool = addToolBar("Edit");
editTool->addAction(copyAction);
editTool->addAction(cutAction);
editTool->addAction(pasteAction);
} 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::loadFile(QString fileName)
{
printf("file name:%s\n",fileName.data());
QFile file(fileName);
if(file.open(QIODevice::ReadOnly|QIODevice::Text))
{
QTextStream textStream(&file);
while(!textStream.atEnd())
{
text->append(textStream.readLine());
printf("read line.\n");
}
printf("end\n"); }
}
#include <QtGui/QApplication>
#include "mainwindow.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show(); return a.exec();
}