实现简单的notepad功能

430阅读 0评论2014-12-09 644924073
分类:

先看下效果:
http://control.cublog.cn/index.php?j=http://control.cublog.cn/photo.php
 
文件: notepad.rar
大小: 245KB
下载: 下载

 

具体实现:

notepad.cpp代码:

notepad::notepad(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::notepad)
{
    ui->setupUi(this);
    this->setWindowTitle("new file");

    //关联信号与槽函数

    QObject::connect(ui->NewFileaction, SIGNAL(triggered()),
                     this, SLOT(NewFile()));
    QObject::connect(ui->OpenFileaction, SIGNAL(triggered()),
                     this, SLOT(OpenFile()));
    QObject::connect(ui->SaveFileaction, SIGNAL(triggered()),
                     this, SLOT(SaveFile()));
    QObject::connect(ui->SaveAsFileaction, SIGNAL(triggered()),
                     this, SLOT(SaveAsFile()));
    QObject::connect(ui->Coloraction, SIGNAL(triggered()),
                     this, SLOT(SetColor()));
    QObject::connect(ui->Fontaction, SIGNAL(triggered()),
                     this, SLOT(SetFont()));
    QObject::connect(ui->Aboutaction, SIGNAL(triggered()),
                     this, SLOT(About()));
    QObject::connect(ui->Helpaction, SIGNAL(triggered()),
                     this, SLOT(Help()));
}

notepad::~notepad()
{
    delete ui;
}

void notepad::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void notepad::NewFile() //新建
{
    this->setWindowTitle("new file");
    ui->Text->clear();
}

void notepad::OpenFile()
{
    QString filename = QFileDialog::getOpenFileName
                       ( this, "get file",QDir::currentPath(), "(*.*)");
    if (!filename.isEmpty())
    {
        QFile *file = new QFile;
        file->setFileName(filename);
        if (file->open(QIODevice::ReadOnly) == true)
        {
            QTextStream in(file);
            ui->Text->setText(in.readAll());
            this->setWindowTitle(filename);
        }
        else
        {
            QMessageBox::information(this, "ERROR Occurs", "file not exist");
        }
        file->close();
        delete file;
    }
}

void notepad::SaveFile()
{
    QString filename = this->windowTitle();
 //   if (filename.compare("new file") != 0)
  //  {
        QFile *file = new QFile;
        file->setFileName(filename);
        if (file->open(QIODevice::WriteOnly) == true)
        {
            QTextStream out(file);
            out<Text->toPlainText();
            file->close();
            delete file;
        }
        else
        {
            QMessageBox::information(this, "ERROR Occurs", "file open error");
        }
 //   }
}

void notepad::SaveAsFile()
{
    QString filename = QFileDialog::getSaveFileName
                       ( this, "save file",QDir::currentPath());
    QFile *file = new QFile;
    file->setFileName(filename);
    if (file->open(QIODevice::WriteOnly) == true)
    {
        QTextStream out(file);
        out<Text->toPlainText();
        file->close();
        delete file;
    }
    else
    {
        QMessageBox::information(this, "ERROR Occurs", "file open error");
    }
}

void notepad::SetColor()
{
    QColor color = QColorDialog::getColor(Qt::white, this);
    if (color.isValid() == true)
    {
        ui->Text->setTextColor(color);
    }
    else
    {
        QMessageBox::information(this, "ERROR Occurs", "set color error");
    }
}

void notepad::SetFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, QFont("Arial", 18), this, "set font");
    if (ok)
    {
        ui->Text->setFont(font);
    }
    else
    {
        QMessageBox::information(this, "ERROR Occurs", "set font error");
    }
}

void notepad::About()
{
    Dialog mychild;
    mychild.exec();
}

void notepad::Help()
{
    QDesktopServices::openUrl(QUrl("
"));
}

上一篇:使用UDP套接字实现简单的通信
下一篇:简单signal的使用