• Qt 一个简单的word文档编辑器


    1.先看效果图

    可以设置文字的属性、文字颜色、字体类型。以下示例仅供参考,有的地方还是不完善。

    2.需要用到的类

    2.1字体选择下拉框:QFontComboBox。

    QFontComboBox是一个让用户选择字体的组合框。组合框中填充了按字母顺序排列的字体族名称列表。

    常用方法:

    获取当前的字体

    1. QFont currentFont() const

    还有一个信号,当字体发生改变时,发送信号。

    1. void currentFontChanged(const QFont &font)

    2.2颜色对话框:QColorDialog

    常用方法:

    获取当前选择的颜色

    1. QColor currentColor() const

    2.3QTextCharFormat

    QTextCharFormat类为QTextDocument中的字符提供格式化信息。换句话说,我们要设置鼠标选中字体的属性,就需要使用这个类。

    本例子中使用的方法:

    void setFont(const QFont &font)设置字体
    void setFontItalic(bool italic)设置是否斜体
    void setFontStrikeOut(bool strikeOut)设置删除线
    void setFontUnderline(bool underline)设置下划线

    3.源码

    为了方便,我定义了5个全局变量

    1. bool isBold = false; //是否粗体
    2. bool isUnderLine = false; //是否下划线
    3. bool isDelLine = false; //是否删除线
    4. bool isLean = false; //是否斜体
    5. QColor color(Qt::black); //字体颜色

    设置斜体、粗体等按钮可选中,因为默认是不可选中的,我们需要绑定可选中的信号。

    1. ui->btnBold->setCheckable(true);
    2. ui->btnDelLine->setCheckable(true);
    3. ui->btnLean->setCheckable(true);
    4. ui->btnUnderline->setCheckable(true);

    绑定按钮的信号

    void clicked(bool checked = false)

    1. #include "WTextEdit.h"
    2. #include "ui_WTextEdit.h"
    3. #include <QColorDialog>
    4. #include <QTextDocument>
    5. #include <QTextCursor>
    6. #include <QTextCharFormat>
    7. #include <QFont>
    8. #include <QBrush>
    9. bool isBold = false; //是否粗体
    10. bool isUnderLine = false; //是否下划线
    11. bool isDelLine = false; //是否删除线
    12. bool isLean = false; //是否斜体
    13. QColor color(Qt::black); //字体颜色
    14. WTextEdit::WTextEdit(QWidget *parent) :
    15. QWidget(parent),
    16. ui(new Ui::WTextEdit)
    17. {
    18. ui->setupUi(this);
    19. ui->btnBold->setCheckable(true);
    20. ui->btnDelLine->setCheckable(true);
    21. ui->btnLean->setCheckable(true);
    22. ui->btnUnderline->setCheckable(true);
    23. }
    24. WTextEdit::~WTextEdit()
    25. {
    26. delete ui;
    27. }
    28. void WTextEdit::on_btnBold_clicked(bool checked)
    29. {
    30. isBold = checked;
    31. updateText();
    32. }
    33. void WTextEdit::on_btnLean_clicked(bool checked)
    34. {
    35. isLean = checked;
    36. updateText();
    37. }
    38. void WTextEdit::on_btnUnderline_clicked(bool checked)
    39. {
    40. isUnderLine = checked;
    41. updateText();
    42. }
    43. void WTextEdit::on_btnDelLine_clicked(bool checked)
    44. {
    45. isDelLine = checked;
    46. updateText();
    47. }
    48. void WTextEdit::updateText()
    49. {
    50. QFont font = ui->fontComboBox->currentFont();
    51. font.setBold(isBold);
    52. font.setPointSize(ui->lineEdit->text().toInt());
    53. QTextCharFormat format;
    54. format.setFont(font);
    55. format.setFontItalic(isLean);
    56. format.setFontStrikeOut(isDelLine);
    57. format.setFontUnderline(isUnderLine);
    58. QPen pen;
    59. pen.setColor(color); //设置字体颜色
    60. format.setTextOutline(pen);
    61. ui->textEdit->textCursor().setCharFormat(format);
    62. }
    63. void WTextEdit::on_btnColor_clicked()
    64. {
    65. QColorDialog dialog;
    66. dialog.exec();
    67. color = dialog.currentColor();
    68. updateText();
    69. }
    70. void WTextEdit::on_lineEdit_textChanged(const QString &arg1)
    71. {
    72. updateText();
    73. }
    74. void WTextEdit::on_fontComboBox_currentFontChanged(const QFont &f)
    75. {
    76. updateText();
    77. }


     

  • 相关阅读:
    从零开始搭建一个组件库(二)
    【CT】LeetCode手撕—53. 最大子数组和
    推动解决新能源电车充电不便的难题
    MacOs基于docker搭建linux内核编译与调试环境
    目标检测模型 pdf (记录链接后期学习)
    大数据计算,如何优化SQL?
    内存的映射
    win11的C/C++环境配置——基于MinGW-W64 GCC-8.1.0
    Ton 区块链的官方 类ERC20-Token 智能合约代码-Transfer部分解析
    P2320 [HNOI2006] 鬼谷子的钱袋
  • 原文地址:https://blog.csdn.net/wzz953200463/article/details/125608220