QT实现文本文件打开与关闭----丁林松教程学习笔记

QT中函数一旦声明就必须在对应的.cpp文件中进行定义,如果不想写该函数的实现,直接将该函数实现为空函数。但是绝对不可以直接连空函数都不实现,否则会报下面的错误:

moc_mainwindow.obj:-1: error: LNK2019: 无法解析的外部符号 "private: void __cdecl MainWindow::saveAsFileslot(void)" (?saveAsFileslot@MainWindow@@AEAAXXZ),该符号在函数 "private: static void __cdecl MainWindow::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@MainWindow@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z) 中被引用

debug\QT_Fiveth.exe:-1: error: LNK1120: 1 个无法解析的外部命令

 

QT中所谓的信号与槽,其实都是函数。当特定事件被触发时(如在输入框输入了字符)将发送一个信号,而与该信号建立的连接槽,则可以接收到该信号并做出反应(激活Find按钮)。
QT中的信号槽连接函数:QObject::connect( 发送方, SIGNAL(信号), 接收方, SLOT(槽函数) );

 

QT中添加资源文件:先点击Add Prefix(添加前缀),再点击Add Files(添加文件)

 

工具栏的生成可以直接从UI设计界面中往工具栏里面拖

 

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("ultitled-----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(printFileslot())
                     );
    QObject::connect(ui->exitAction,SIGNAL(triggered()),this,

                     SLOT(exitslot()));

    //编辑菜单栏下面的信号和槽函数的连接,直接使用TextEdit自带的功能即可
    QObject::connect(ui->UndoAction,SIGNAL(triggered()),ui->textEdit,
                     SLOT( undo()) );

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

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

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

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

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

    //注意上下两处信号的接收者不一样
    QObject::connect(ui->DateTimeAction,SIGNAL(triggered()),this,
                     SLOT(CurrentDateTimeSlot())
                     );


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

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

}

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

void MainWindow::newFileSlot()
{
    if(ui->textEdit->document()->isModified())
        qDebug()<<"Current file modifiled";
    else{
        //qDebug()<<"not changed";
        ui->textEdit->clear();
        this->setWindowTitle("Untitled.txt ------newfile");
    }

}
void MainWindow::openFileSlot()
{
    //获得文件的名称
    QString filename = QFileDialog::getOpenFileName(
                this,"Open File",QDir::currentPath());
    //qDebug()<<"file name is "<<filename;
    if(filename.isEmpty())//is NULL
    {
        QMessageBox::information(this,"Error Messag"
             ,"Please Select a Text 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() );
        //read all context  from the open file
        file->close();
        delete file; //关闭所有文件
    }
    else
    {
        QMessageBox::information(this,"Error Messgae",
              "File Open Error" + file->errorString() );
        return ;
    }

}
void MainWindow:: savefileslot()
{
    if(saveFilename.isEmpty())
    {
        this->saveAsFileslot();
    }
    else
    {
     QFile *file = new QFile;
     file->setFileName(saveFilename);
     bool ok = file->open(QIODevice::WriteOnly);
     if(ok)
     {
           QTextStream out(file);
           //输出文件到外部磁盘
           out<<ui->textEdit->toPlainText();
           file->close();
           this->setWindowTitle(saveFilename);
           delete file;

     }
    }
}

void MainWindow::saveAsFileslot()
{
    QString saveFilename = QFileDialog::getSaveFileName(this,
                         "Save File",QDir::currentPath());
    if(saveFilename.isEmpty())
    {
        QMessageBox::information(this,"Error Message",
                                 "Please Select A File");
        return ;
    }
    QFile *file = new QFile;
    file->setFileName(saveFilename);
    bool ok = file->open(QIODevice::WriteOnly);
    if(ok)
    {
        QTextStream out(file);
        //cout<<
        out<<ui->textEdit->toPlainText();//这里是取出TextEdit当中的纯文本
        file->close();
        this->setWindowTitle(saveFilename+"-----notpad");
        delete  file;
    }

    else
    {
        QMessageBox::information(this,"Error Message","Save File Fasle");
        return ;
    }

}

void MainWindow::printFileslot()
{
    //QPrintDialog
}

void MainWindow::exitslot()
{
  this->close();
}


//void MainWindow::UndoSlot()//撤销
//{

//}

//void MainWindow::Redoslot()
//{

//}

//void MainWindow::CopySlot()
//{

//}

void MainWindow::CurrentDateTimeSlot()
{
    QDateTime current = QDateTime::currentDateTime();
    QString time = current.toString("yyyy-M-dd hh:mm:ss");
    ui->textEdit->append(time);
}

//void MainWindow::PasteSlot()
//{


//}

//void MainWindow::CutSlot()
//{

//}

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"
                                );
    }
}

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 ;
    }

}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值