qt文本编辑器之升级版

查询相应基本函数对编辑器的功能实现


从上往下进行讲解

设置窗口的图标与文本

  QIcon icon("C:/Users/Administrator/Desktop/qtcode/images/notebook.png");
    w.setWindowIcon(icon);
    w.setWindowTitle("notebook");

调用QIcon类插入图片

首先你要知道几乎qt上的没一个小窗口都有调色板、Icon、text、等内置环境 你都可以对这些进行设置

设置添加菜单与菜单栏

QMenuBar 生成菜单 QAction 生成菜单栏 

   QMenu *pFile = ui->menuBar->addMenu("文件(F)");
    QMenu *pEdit = ui->menuBar->addMenu("编辑(E)");
    QMenu *pOption = ui->menuBar->addMenu("格式(O)");
    QMenu *pView = ui->menuBar->addMenu("查看(V)");
    QMenu *pHelp = ui->menuBar->addMenu("帮助(H)");
 QAction *pOpen = pFile->addAction("新建(N)");
    pOpen->setShortcut(QKeySequence("Ctrl+N"));
    connect(pOpen, &QAction::triggered,
            [=]()
            {
                   QString str = "未命名.txt";
                   setWindowTitle(str);
                   textEdit->clear();
                   textEdit->setVisible(true);
            }
            );

同理工具栏也是相同操作toolBar  这里我通过插入小控件进行快捷方式的设置

 QToolBar *toolBar = addToolBar("toolBar");
   QPushButton *tNew = new QPushButton(this);
   QPixmap icon0("C:/Users/Administrator/Desktop/qtcode/images/new.png");
   tNew->setIcon(icon0);
   toolBar->addWidget(tNew);
   connect(tNew, &QPushButton::pressed,
           [=]()
   {
       QString str = "未命名.txt";
       setWindowTitle(str);
       textEdit->clear();
       textEdit->setVisible(true);
   }
           );

这里忘说要在中央控件中插入textedit 因为后面对这个edit访问 我在头文件里面声明 进行全局的变化

private: QtextEdit *textEdit;
 ......
  textEdit = new QTextEdit(this);
    setCentralWidget(textEdit);

在设置help工具时我通过Action打开一个对话框进行信息的显示 用Qdialong  和QLabel 实现

 QAction *PHELP = pHelp->addAction("版本信息");
   connect(PHELP, &QAction::triggered,
           [=]()
           {
             QDialog *info = new QDialog(this);
              info->resize(200,100);
              QLabel *text = new QLabel(info);
              text->setText("你好嘛");
              info->show();
           }
           );

对剪切、复制、粘贴等功能的实现 利用textedit自带paste ,copy,cut等函数调用实现

void MainWindow::do_actCut()
{
    textEdit->cut();
}
void MainWindow::do_actCopy()
{
    textEdit->copy();
}
void MainWindow::do_actPaste()
{
    textEdit->paste();
}
void MainWindow::do_actClear()
{
    QMessageBox box;
    box.setWindowTitle("警告");
    box.setText("是否清空(不可恢复?)");
    box.setIcon(QMessageBox::Warning);
    box.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
    if(box.exec() == QMessageBox::Yes)
    {
        textEdit->clear();
    }
}

通过信号与槽 利用C++11特性的Lamdba表达式进行连接 CONFIG +=C++11

状态栏的显示 利用标签 和获取text当前的位置 用Label进行行号 列号的显示

void MainWindow::do_cursorchange()
{
     QTextCursor cursor = textEdit->textCursor();
     int row = cursor.blockNumber();
     int col = cursor.columnNumber();
     firStatusLabel->setText(tr("%1   %2 列").arg(row).arg(col));
}

源码地址:

https://download.csdn.net/download/capricorngud/10463746

需要的可以进行下载 


猜你喜欢

转载自blog.csdn.net/capricorngud/article/details/80604867