实现简单的notepad功能

先看下效果:
 
文件: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<<ui->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<<ui->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("
www.baidu.com"));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值