一、首页布局界面,如图所示的界面及对象名称:
二、在头部文件中添加槽:
代码语言:javascript复制 private slots:
void calcSlot();
//新建文件
void createNewFile();
//打开文件
void openFileSlot();
//保存文件
三、实现槽函数:
代码语言:javascript复制//新建文件
void MainWindow::createNewFile(){
//如果当前文档的内容已经改变了
if(ui->textAreaInput->document()->isModified()){
qDebug()<<"is aready";
return;
}else{
//qDebug()<<"is null for ecit";
ui->textAreaInput->clear();
this->setWindowTitle("新文档.txt");
}
}
//打开文件
void MainWindow::openFileSlot(){
QString fileName = QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
//qDebug()<<"文件名是:"<<fileName;
if(fileName.isEmpty()){
QMessageBox::information(this,"错误消息","请选择一个文件");
return;
}
QFile *file = new QFile;
file->setFileName(fileName);
bool openFileOk = file->open(QIODevice::ReadOnly);
if(openFileOk){
//文件与文本流相关联
QTextStream in(file);
ui->textAreaInput->setText(in.readAll());
file->close();
delete file;
}else{
QMessageBox::information(this,"错误提示","打开文件失败:" file->errorString());
return;
}
}
四、添加信号与槽的连接:
代码语言:javascript复制QObject::connect(ui->equalButton,SIGNAL(clicked()),this,SLOT(calcSlot()));
QObject::connect(ui->createNewAction,SIGNAL(triggered()),this,SLOT(createNewFile()));
QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));
版权声明: 此文为本站源创文章[或由本站编辑从网络整理改编], 转载请备注出处: 狂码一生 http://www.sindsun.com/article-details-66.html [若此文确切存在侵权,请联系本站管理员进行删除!]