mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
- #include <boost/scoped_ptr.hpp>
- using namespace boost;
-
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- void readFile(const QString& fileName);
- void writeFile(const QString& fileName);
- public slots:
- void open();
- void save();
- void saveAs();
- void close();
- void quit();
- protected:
- void dragEnterEvent(QDragEnterEvent *);
- void dropEvent(QDropEvent *);
- private:
- class MainWindowUi;
- scoped_ptr<MainWindowUi> mui;
- class MainWindowData;
- scoped_ptr<MainWindowData> mData;
- };
-
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include <QFileDialog>
- #include <QString>
- #include <QTextStream>
- #include <QCloseEvent>
- #include <QDesktopWidget>
- #include <QUrl>
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <iostream>
- using namespace std;
-
- class MainWindow::MainWindowData{
- public:
- QString mFileName;
- QString mText;
- QFile mFile;
- QTextStream mStream;
- };
-
- class MainWindow::MainWindowUi:public Ui::MainWindow{
- public:
- QFileDialog* mFileDialog;
- public:
- MainWindowUi(){
-
- }
- };
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent)
- {
- mui.reset(new MainWindowUi);
- mui->setupUi(this);
- mData.reset(new MainWindowData);
- mui->mFileDialog = new QFileDialog(this);
- mui->textEdit->setAcceptDrops(false);
- setAcceptDrops(true);
- //把所有action与对应的槽相连
- connect(mui->action_open, SIGNAL(triggered()), this, SLOT(open()));
- connect(mui->action_save, SIGNAL(triggered()), this ,SLOT(save()));
- connect(mui->actionSave_As, SIGNAL(triggered()), this ,SLOT(saveAs()));
- connect(mui->action_close,SIGNAL(triggered()), this, SLOT(close()));
- connect(mui->action_quit, SIGNAL(triggered()), this, SLOT(quit()));
- //放到屏幕中间
- int screenCenterX =(QDesktopWidget().screenGeometry().width()-geometry().width())/2;
- int screenCenterY =(QDesktopWidget().screenGeometry().height()-geometry().height())/2;
- //宽和高就用设计器中设置的吧
- setGeometry(screenCenterX,screenCenterY,width(),height());
- }
-
- /*
- 根据文件名读文本文件
- */
- void MainWindow::readFile(const QString &fileName){
- mData->mFile.setFileName(fileName);
- if(!mData->mFile.open(QIODevice::ReadOnly|QIODevice::Text))
- return;
- //怕文件太大,一行一行地读
- mData->mStream.setDevice(&mData->mFile);
- QString line = mData->mStream.readLine();
- while(not line.isNull()){
- mData->mText.append(line).append("\n");
- line = mData->mStream.readLine();
- }
- //读完得关了它,最好一开始就写上,我在测试writeFile能否读text时没写后面的代码,
- //结果一关程序就把我的源码清空了2个才发现,悲剧啊...
- mData->mFile.close();
- mui->textEdit->clear();
- mui->textEdit->setText(mData->mText);
- }
-
- /*
- 根据文件名写文本文件
- */
- void MainWindow::writeFile(const QString &fileName){
- mData->mFile.setFileName(fileName);
- if(not mData->mFile.open(QIODevice::WriteOnly|QIODevice::Truncate))
- return;//can not write
- mData->mStream.setDevice(&mData->mFile);
- QStringList currentTextList = mui->textEdit->toPlainText().split("\n");
- foreach(QString str,currentTextList){
- mData->mStream << str << "\n";
- }
- mData->mFile.close();
- }
-
- void MainWindow::open(){
- mData->mFileName = mui->mFileDialog->getOpenFileName(this,tr("Open File"),//这个对话框的标题
- "/home/xiangjie/code/unix/T1/",
- tr("files(*.c *.cc *.cpp *.txt)"));//这里相当于是过滤器
- readFile(mData->mFileName);
- }
-
- /*
- 保存
- */
- void MainWindow::save(){
- if(mData->mFileName.isNull())
- return;
- writeFile(mData->mFileName);
- }
-
- /*
- 另存为
- */
- void MainWindow::saveAs(){
- mData->mFileName = mui->mFileDialog->getSaveFileName(this,tr("Save File"),
- "/home/xiangjie/code",
- tr("files(*.c *.cc *.cpp *.txt)"));
- save();
- }
-
- /*
- 清空textEdit和fileName
- */
- void MainWindow::close(){
- mui->textEdit->clear();
- mData->mFileName.clear();
- }
-
- /*
- 关闭所有窗口
- */
- void MainWindow::quit(){
- qApp->closeAllWindows();
- }
-
- /*
- 拖放
- */
- void MainWindow::dragEnterEvent(QDragEnterEvent *event){
- if(event->mimeData()->hasFormat("text/uri-list"))
- event->acceptProposedAction();
- }
-
- void MainWindow::dropEvent(QDropEvent *event){
- QList<QUrl> urls = event->mimeData()->urls();
- if(urls.isEmpty())
- return;
- QString fileName = urls.first().toLocalFile();
- if(fileName.isEmpty())
- return;
- readFile(fileName);
- setWindowTitle(tr("%1-%2").arg(fileName).arg(tr("Drag file")));
- }
-
- MainWindow::~MainWindow()
- {
- }
main.cpp
- #include <QtGui/QApplication>
- #include "mainwindow.h"
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
|