大家一起解决!
程序代码:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtGui> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); isSaved=false; curFile=tr("未命名.txt"); setWindowTitle(curFile); init_statusBar(); connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(do_cursorChanged())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::do_file_New() { do_file_SaveOrNot(); isSaved = false; curFile = tr("未命名.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("警告")); box.setIcon(QMessageBox::Warning); box.setText(curFile + tr("尚未保存,是否保存?")); box.setStandardButtons(QMessageBox::Yes | QMessageBox::No); if(box.exec() == QMessageBox::Yes) do_file_Save(); } } void MainWindow::do_file_Save() { if(isSaved){ saveFile(curFile); } else{ do_file_SaveAs(); } } void MainWindow::do_file_SaveAs() { QString fileName = QFileDialog::getSaveFileName(this,tr("另存为"),curFile); if(!fileName.isEmpty()) { saveFile(fileName); } } bool MainWindow::saveFile(const QString& fileName) { QFile file(fileName); if(!file.open(QFile::WriteOnly | QFile::Text)) { QMessageBox::warning(this,tr("保存文件"), tr("无法保存文件 %1:\n %2").arg(fileName).arg(file.errorString())); return false; } QTextStream out(&file); out << ui->textEdit->toPlainText(); isSaved = true; curFile = QFileInfo(fileName).canonicalFilePath(); setWindowTitle(curFile); second_statusLabel->setText(tr("保存文件成功")); return true; } void MainWindow::on_action_triggered() { do_file_New(); } void MainWindow::on_action_3_triggered() { do_file_Save(); } void MainWindow::on_action_4_triggered() { do_file_SaveAs(); } void MainWindow::do_file_Open() { do_file_SaveOrNot(); QString fileName = QFileDialog::getOpenFileName(this); if(!fileName.isEmpty()) { do_file_Load(fileName); } ui->textEdit->setVisible(true); } bool MainWindow::do_file_Load(const QString& fileName) //读取文件 { QFile file(fileName); if(!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this,tr("读取文件"),tr("无法读取 文件 %1:\n%2.").arg(fileName).arg(file.errorString())); return false; //如果打开文件失败,弹出对话框,并返回 } QTextStream in(&file); ui->textEdit->setText(in.readAll()); //将文件中的所有内容都写到文本编辑器中 curFile = QFileInfo(fileName).canonicalFilePath(); setWindowTitle(curFile); second_statusLabel->setText(tr("打开文件成功")); return true; } void MainWindow::on_action_2_triggered() { do_file_Open(); } void MainWindow::on_action_5_triggered() { on_action_5_triggered(); qApp->quit(); } void MainWindow::on_action_16_triggered() { do_file_SaveOrNot(); ui->textEdit->setVisible(false); first_statusLabel->setText(tr("文本编辑器已关闭")); second_statusLabel->setText(tr("制作!")); } void MainWindow::on_action_6_triggered() { ui->textEdit->undo(); } void MainWindow::on_action_10_triggered() { ui->textEdit->cut(); } void MainWindow::on_action_7_triggered() { ui->textEdit->copy(); } void MainWindow::on_action_8_triggered() { ui->textEdit->paste(); } void MainWindow::on_action_9_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())); //设置“查找下一个”按钮的单击事件和其槽函数的关联 second_statusLabel->setText(tr("正在进行查找")); } void MainWindow::show_findText()//“查找下一个”按钮的槽函数 { QString findText = find_textLineEdit->text(); //获取行编辑器中的内容 if(!ui->textEdit ->find(findText,QTextDocument::FindBackward)) { QMessageBox::warning(this,tr("查找"),tr("找不到 %1").arg(findText)); } } void MainWindow::init_statusBar() { 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); QTextCodec::setCodecForTr(QTextCodec::codecForLocale());bar->addWidget(second_statusLabel); first_statusLabel->setText(tr("欢迎使用文本编辑器")); //初始化内容 second_statusLabel->setText(tr("刘瑞旋>>制作!")); } void MainWindow::do_cursorChanged() { int rowNum = ui->textEdit->document()->blockCount(); const QTextCursor cursor = ui->textEdit->textCursor(); int colNum = cursor.columnNumber(); //获取光标所在列的列号 rowNum = cursor.blockNumber();//获取光标所在行的行号 first_statusLabel->setText(tr("%1行 %2列").arg(rowNum).arg(colNum)); //在状态栏显示光标位置 connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(do_cursorChanged())); } void MainWindow::on_action_17_triggered() { ?这里不会用接口不会调用。?????利用转到曹添加计算器功能,计算器在另一个项目,怎么结合? }