简单记事本和简单四则运算计算器源代码(*.h *.cpp main.cpp)

原创
2016/03/07 09:38
阅读数 150

记事本代码:

“mainwindow.h”

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include <QMainWindow>

#include <QMessageBox>

#include <QtDebug>

#include <QFile>

#include <QFileDialog>

#include <QDir>

#include <QTextStream>

#include <QFont>

#include <QFontDialog>

#include <QColor>

#include <QColorDialog>

#include <QDateTime>

#include <QAction>

namespace Ui {

class MainWindow;

}

class MainWindow : public QMainWindow

{

    Q_OBJECT

public:

explicit MainWindow(QWidget *parent = 0);

~MainWindow();

private:

    Ui::MainWindow *ui;

    QString fileName;

private slots:

//    void newFileSlot();

//    void openFileSlot();

void newFileSlot();

void openFileSlot();

void saveFileSlot();

void saveAsFileSlot();

//    void undoSlot();

//    void redoSlot();

//    void copySlot();

//    void cutSlot();

//    void pasteSlot();

//    void seleteAllSlot();

void setFontSlot();

void setColorSlot();

void currentDateTimeSlot();

};

#endif // MAINWINDOW_H

“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();

}

“mainwindow.cpp”

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);

this->setWindowTitle("Untitled-notepad");

    QObject::connect(ui->newAction,SIGNAL(triggered()),this,SLOT(newFileSlot()));

    QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));

    QObject::connect(ui->saveAction,SIGNAL(triggered()),this,SLOT(saveFileSlot()));

    QObject::connect(ui->saveAsAction,SIGNAL(triggered()),this,SLOT(saveAsFileSlot()));

    QObject::connect(ui->exitAction,SIGNAL(triggered()),this,SLOT(close()));

//    QObject::connect(ui->copyAction,SIGNAL(triggered()),this,SLOT(copySlot()));

//    QObject::connect(ui->seleteAllAction,SIGNAL(triggered()),this,SLOT(seleteAllSlot()));

//    QObject::connect(ui->cutAction,SIGNAL(triggered()),this,SLOT(cutSlot()));

//    QObject::connect(ui->pasteAction,SIGNAL(triggered()),this,SLOT(pasteSlot()));

//    QObject::connect(ui->undoAction,SIGNAL(triggered()),this,SLOT(undoSlot()));

//    QObject::connect(ui->redoAction,SIGNAL(triggered()),this,SLOT(redoSlot()));

    QObject::connect(ui->copyAction,SIGNAL(triggered()),ui->textEdit,SLOT(copy()));

    QObject::connect(ui->seleteAllAction,SIGNAL(triggered()),ui->textEdit,SLOT(selectAll()));

    QObject::connect(ui->cutAction,SIGNAL(triggered()),ui->textEdit,SLOT(cut()));

    QObject::connect(ui->pasteAction,SIGNAL(triggered()),ui->textEdit,SLOT(paste()));

    QObject::connect(ui->undoAction,SIGNAL(triggered()),ui->textEdit,SLOT(undo()));

    QObject::connect(ui->redoAction,SIGNAL(triggered()),ui->textEdit,SLOT(redo()));

    QObject::connect(ui->colorAction,SIGNAL(triggered()),this,SLOT(setColorSlot()));

    QObject::connect(ui->FontAction,SIGNAL(triggered()),this,SLOT(setFontSlot()));

    QObject::connect(ui->dateTimeAction,SIGNAL(triggered()),this,SLOT(currentDateTimeSlot()));

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::newFileSlot()

{

if (ui->textEdit->document()->isModified())

{

        qDebug() << "current file modified";

}

else

{

        ui->textEdit->clear();

this->setWindowTitle("Untitled.txt");

}

}

void MainWindow::openFileSlot()

{

    fileName = QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());

if (fileName.isEmpty())

{

        QMessageBox::information(this,"ERROR MESSAGE","Please selete a file");

return;

}

    QFile *file = new QFile;

    file->setFileName(fileName);

bool ok = file->open(QIODevice::ReadOnly);

if (ok)

{

        QTextStream in(file);

        ui->textEdit->setText(in.readAll());

        file->close();

delete file;

}

else

{

        QMessageBox::warning(this,"ERROR MESSAGE","File Open FiLE"+file->errorString());

return;

}

}

void MainWindow::saveFileSlot()

{

//QString fileName = ui->textEdit->QDir::currentPath();//QFileDialog::getOpenFileName(this,"Save File",QDir::currentPath());

if (fileName.isEmpty())

{

        saveAsFileSlot();

}

else

{

        QFile *file = new QFile;

        file->setFileName(fileName);

bool ok = file->open(QIODevice::WriteOnly);

if (ok)

{

            QTextStream out(file);

            out << ui->textEdit->toPlainText();

            file->close();

delete file;

}

else

{

            QMessageBox::information(this,"ERROR MESSAGE","save file error");

return;

}

}

}

void MainWindow::saveAsFileSlot()

{

    QString AnotherFileName = QFileDialog::getSaveFileName(this,"save as Another",QDir::currentPath());

if (AnotherFileName.isEmpty())

{

        QMessageBox::warning(this,"ERROR MESSAGE","selete a file");

return;

}

else

{

        QFile *file = new QFile;

        file->setFileName(AnotherFileName);

bool ok = file->open(QIODevice::WriteOnly);

if (ok)

{

            QTextStream out(file);

            out << ui->textEdit->toPlainText();

            file->close();

delete file;

}

else

{

            QMessageBox::warning(this,"ERROR MESSAGE","save file Error");

return;

}

}

}

/*

void MainWindow::undoSlot()

{

    ui->textEdit->undo();

}

void MainWindow::redoSlot()

{

    ui->textEdit->redo();

}

void MainWindow::copySlot()

{

    ui->textEdit->copy();

}

void MainWindow::cutSlot()

{

    ui->textEdit->cut();

}

void MainWindow::pasteSlot()

{

    ui->textEdit->paste();

}

void MainWindow::seleteAllSlot()

{

    ui->textEdit->selectAll();

}

*/

void MainWindow::setFontSlot()

{

bool ok;

    QFont font = QFontDialog::getFont(&ok,this);

if (ok)

{

        ui->textEdit->setFont(font);

}

else

{

        QMessageBox::information(this,"Error Message","Error Set Font!!");

return;

}

}

void MainWindow::setColorSlot()

{

    QColor color = QColorDialog::getColor(Qt::red,this);

if (color.isValid())

{

        ui->textEdit->setTextColor(color);

}

else

{

        QMessageBox::information(this,"Error Message","Color is Unvalid");

return;

}

}

void MainWindow::currentDateTimeSlot()

{

    QDateTime current = QDateTime::currentDateTime();

    QString time = current.toString("yyyy-MM-dd hh:mm:ss");

    ui->textEdit->append(time);

}

四则运算计算器代码:

“mainwindow.h”

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include <QMainWindow>

#include <QMessageBox>

namespace Ui {

class MainWindow;

}

class MainWindow : public QMainWindow

{

    Q_OBJECT

public:

explicit MainWindow(QWidget *parent = 0);

~MainWindow();

private:

    Ui::MainWindow *ui;

private slots:

void calSlot();

};

#endif // MAINWINDOW_H

“mian.cpp”

#include <QtGui/QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    MainWindow w;

    w.show();

return a.exec();

}

“mainwindow.cpp”

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);

    QObject::connect(ui->calButton,SIGNAL(clicked()),this,SLOT(calSlot()));

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::calSlot()

{

int firth = ui->firstLineEdit->text().toInt();

int second = ui->secondLineEdit->text().toInt();

int result;

if (ui->comboBox->currentIndex() == 0)

{

        result = firth + second;

        ui->resultLineEdit->setText(QString::number(result));

        QMessageBox::information(this,"RESULT:",QString::number(result));

}

else if (ui->comboBox->currentIndex() == 1)

{

        result = firth - second;

        ui->resultLineEdit->setText(QString::number(result));

        QMessageBox::information(this,"RESULT:",QString::number(result));

}

else if (ui->comboBox->currentIndex() == 2)

{

        result = firth * second;

        ui->resultLineEdit->setText(QString::number(result));

        QMessageBox::information(this,"RESULT:",QString::number(result));

}

else

{

if (second == 0)

{

            QMessageBox::warning(this,"ERROR MESSAGE:","secondValue can't be zero!");

return;

}

else

{

            result = firth / second;

            ui->resultLineEdit->setText(QString::number(result));

            QMessageBox::information(this,"RESULT:",QString::number(result));

}

}

}

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部