经过三四天的学习QT,终于把自己的记事本程序搞定了,功能还比较简单,以后逐步添加!
// mywindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QPlainTextEdit;
QT_END_NAMESPACE
//! [0]
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private slots:
void newFile();
void open();
bool save();
bool saveAs();
void about();
void documentWasModified();
private:
void createAction();
void createMenu();
void createToolBar();
bool maybeSave();
void loadFile(const QString &fileName);
bool saveFile(const QString &fileName);
void setCurrentFile(const QString &fileName);
QPlainTextEdit *text;
QString curFile;
QMenu *fileMenu;
QMenu *editMenu;
QMenu *aboutMenu;
QToolBar *fileToolBar;
QToolBar *editToolBar;
QAction *newAct;
QAction *openAct;
QAction *saveAct;
QAction *saveAsAct;
QAction *cutAct;
QAction *copyAct;
QAction *pasteAct;
QAction *aboutAct;
QAction *aboutQtAct;
};
//! [0]
#endif
//mywindow.cpp
#include<QtGui>#include "mainwindow.h"MainWindow::MainWindow(){text=new QPlainTextEdit;setCentralWidget(text);
createAction();
createMenu();
createToolBar();
connect(text->document(),SIGNAL(contentsChanged()),this,SLOT(documentWasModified()));setCurrentFile("");setUnifiedTitleAndToolBarOnMac(true);}void MainWindow::documentWasModified(){setWindowModified(text->document()->isModified());
}void MainWindow::createAction()//! [17] //! [18]{newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);newAct->setShortcuts(QKeySequence::New);newAct->setStatusTip(tr("Create a new file"));connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));//! [19]openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);openAct->setShortcuts(QKeySequence::Open);openAct->setStatusTip(tr("Open an existing file"));connect(openAct, SIGNAL(triggered()), this, SLOT(open()));saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);saveAct->setShortcuts(QKeySequence::Save);saveAct->setStatusTip(tr("Save the document to disk"));connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));saveAsAct = new QAction(tr("Save &As..."), this);saveAsAct->setShortcuts(QKeySequence::SaveAs);saveAsAct->setStatusTip(tr("Save the document under a new name"));connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);//! [21]cutAct->setShortcuts(QKeySequence::Cut);cutAct->setStatusTip(tr("Cut the current selection's contents to the ""clipboard"));connect(cutAct, SIGNAL(triggered()), text, SLOT(cut()));copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);copyAct->setShortcuts(QKeySequence::Copy);copyAct->setStatusTip(tr("Copy the current selection's contents to the ""clipboard"));connect(copyAct, SIGNAL(triggered()), text, SLOT(copy()));pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);pasteAct->setShortcuts(QKeySequence::Paste);pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current ""selection"));connect(pasteAct, SIGNAL(triggered()), text, SLOT(paste()));aboutAct = new QAction(tr("&About"), this);aboutAct->setStatusTip(tr("Show the application's About box"));connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));cutAct->setEnabled(false);copyAct->setEnabled(false);connect(text, SIGNAL(copyAvailable(bool)),cutAct, SLOT(setEnabled(bool)));connect(text, SIGNAL(copyAvailable(bool)),copyAct, SLOT(setEnabled(bool)));}void MainWindow::about(){QMessageBox::about(this,tr("about"),tr("The software is created by CaoYong!"));}void MainWindow::createMenu(){fileMenu=menuBar()->addMenu(tr("&File"));fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
menuBar()->addSeparator();
editMenu=menuBar()->addMenu(tr("&Edit"));editMenu->addAction(copyAct);
editMenu->addAction(cutAct);
editMenu->addAction(pasteAct);
menuBar()->addSeparator();
aboutMenu=menuBar()->addMenu(tr("&About"));aboutMenu->addAction(aboutAct);
}void MainWindow::createToolBar(){fileToolBar=addToolBar(tr("file"));editToolBar=addToolBar(tr("edit"));fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
}void MainWindow::newFile(){if(maybeSave()){
text->clear();
setCurrentFile("");}
}void MainWindow::open(){if(maybeSave()){
QString filename=QFileDialog::getOpenFileName(this);if(!filename.isEmpty()){
loadFile(filename);
}
}
}void MainWindow::loadFile(const QString &filename){QFile file(filename);if(!file.open(QFile::ReadOnly|QFile::Text)){
QMessageBox::warning(this,tr("application"),tr("can't read %1\n.%2").arg(filename).arg(file.errorString()));return ;}
QTextStream in(&file);text->setPlainText(in.readAll());
setCurrentFile(filename);
}bool MainWindow:: maybeSave(){if(text->document()->isModified()){
QMessageBox::StandardButton ret;ret=QMessageBox::warning(this,tr("warning"),tr("content has been modified.\n""Do you want to save"),QMessageBox::Save|QMessageBox::Discard |QMessageBox::Cancel);if(ret==QMessageBox::Save){
return save();}
elseif(ret==QMessageBox::Cancel){
return false;}
}
return true;}void MainWindow::setCurrentFile(const QString &filename){curFile=filename;
text->document()->setModified(false);setWindowModified(false);QString titleName=filename;if(titleName.isEmpty()){
titleName="Untitle.txt";}
setWindowFilePath(titleName);
}bool MainWindow::saveFile(const QString &filename){QFile file(filename);if(!file.open(QFile::WriteOnly|QFile::Text)){
QMessageBox::warning(this,tr("application"),tr("%1can't open\n.%2").arg(filename).arg(file.errorString()));return false;}
QTextStream out(&file);out<<text->toPlainText();
setCurrentFile(filename);
return true;}bool MainWindow::save(){if(curFile.isEmpty())return saveAs();elsereturn saveFile(curFile);}bool MainWindow::saveAs(){QString filename=QFileDialog::getSaveFileName(this);if(filename.isEmpty())return false;return saveFile(filename);}//main.cpp
#include <QtGui/QApplication>#include "mainwindow.h"int main(int argc, char *argv[]){QApplication a(argc, argv);MainWindow w;a.setApplicationName("application");w.show();
return a.exec();}个人觉得比MFC好多了,对C++的理解也更进一步!