记录:
void MainWindow::do_file_New()
{
do_file_SaveOrNot();
isSaved = false;
curFile = tr("name.txt");
setWindowTitle(curFile);
ui->textEdit->clear();//清空文本编辑器
ui->textEdit->setVisible(true);//文本编辑器可见
}
void MainWindow::do_file_SaveOrNot() //弹出是否保存文件的对话框
{
if(ui->textEdit->document()->isModified())//如果文件被更改过,弹出保存对话框
{
QMessageBox box;
box.setWindowTitle(tr("Warning!"));
box.setIcon(QMessageBox::Warning);
box.setText(curFile + tr(" save or not ?"));
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
if(box.exec() == QMessageBox::Yes)//如果选择保存文件,则执行操作
do_file_Save();
}
}
bool MainWindow::saveFile(const QString &fileName)
{
QFile file(fileName);
if(!file.open(QFile::WriteOnly | QFile::Text))
//以只写方式打开文件,如果打开失败则弹出提示框,并返回
{
QMessageBox::warning(this,tr("Save file"),
tr("can't save file %1: \n %2").arg(fileName).arg(file.errorString()));
return false;
}
//%1 ,%2表示后面的两个arg参数的值
QTextStream out(&file);//新建流对象,指向选定的文件
out<<ui->textEdit->toPlainText();//将文本编辑器里内容以纯文本的形式输出流对象中
isSaved = true;
curFile = QFileInfo(fileName).canonicalFilePath();//获得文件的标准路径
setWindowTitle(curFile);//将窗口名称改为现在的窗口的路径
return true;
}
查找对话框的编写
void MainWindow::on_Find_triggered()
{
QDialog *findDlg = new QDialog(this);
//新建一个对话框,用于查找操作,this表明它的父窗口是MainWindow
findDlg->setWindowTitle(tr("查找"));
//设置对话框的标题
find_textLineEdit = new QLineEdit(findDlg);
//将行编辑器加入到新建的查找对话框
QPushButton *find_Btn = new QPushButton(tr("先查找下一个"),findDlg);
//加入一个“查找下一个”的按钮
QVBoxLayout* layout = new QVBoxLayout(findDlg);
layout->addWidget(find_textLineEdit);
layout->addWidget(find_Btn);
//新建一个垂直布局管理器,并加入行编辑器和按钮加入其中
findDlg->show();
//显示对话框
connect(find_Btn,SIGNAL(clicked()),this,SLOT(show_findText());
//设置“查找下一个”按钮的单击事件和其槽函数的关联
}
获取状态栏,对状态栏操作必须在MainWindow中操作:
MainWindow()
{
//QStatusBar* bar = ui->statusBar;//获取状态栏
//first_statusLabel = new QLabel;
//first_statusLabel->setMinimumSize(150,20);//设置标签最小尺寸
//first_statusLabel->setFrameShape(QFrame::WinPanel);//设置标签形状
//first_statusLabel->setFrameShadow(QFrame::Sunken);//设置标签阴影
//second_statusLabel = new QLabel;
//second_statusLabel->setMinimumSize(150,20);
//second_statusLabel->setFrameShape(QFrame::WinPanel);
//second_statusLabel->setFrameShadow(QFrame::Sunken);
//bar->addWidget(first_statusLabel);
//bar->addPermanentWidget(second_statusLabel);
//first_statusLabel->setText(tr("Welcome to use !"));
//second_statusLabel->setText(tr("hao zi !"));
}