Qt Rich Text Example 看看看~

news/2024/7/5 6:27:49

帮助文档中有四个Rich Text的例子:Calendar,  Order Form,  Syntax High Lighter,  Text Object。本博客展示前两个。

Calendar

这个例子很简单,MainWindow类里添加一个QTextBrowser私有对象,并将此对象默认关联的QTextCursor对象进行编辑。添加TableFrame

本例中使用到了QDate类,它是Qt专门用于处理日期的类,currentDate()获取系统的日期;setDate(),fromString()可以设置日期;year(),month(),day()返回日期的年,月,日;年月日的这信息可以有各种格式:

Expression

Output

d

the day as number without a leading zero (1 to 31)

dd

the day as number with a leading zero (01 to 31)

ddd

the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().

dddd

the long localized day name (e.g. 'Monday' to 'Sunday'). Uses QDate::longDayName().

M

the month as number without a leading zero (1 to 12)

MM

the month as number with a leading zero (01 to 12)

MMM

the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName().

MMMM

the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName().

yy

the year as two digit number (00 to 99)

yyyy

the year as four digit number. If the year is negative, a minus sign is prepended in addition.

(注意,Qt会默认使用系统的语言,例如中文的shortDayName()是周一,周二等,longDayName()是星期一,星期二等。)

增加日期的方法:addMonths(); addYears(); addDays()daysTo()可以比较两个日期相差天数。对于闰年:isLeapYear()返回是否闰年。

下面展示的是本例子的核心代码(关于Rich Text的):

void MainWindow::insertCalendar()
{
    editor->clear();                              // 清空QTextBrowser上的内容
    QTextCursor cursor = editor->textCursor();    // 获取cursor
    cursor.beginEditBlock();                      // 开始块编辑

    QDate date(selectedDate.year(), selectedDate.month(), 1);  // 创建本月的第一天的日期对象,用于绘制
//! [5]

//! [6]
    QTextTableFormat tableFormat;                   // 设置文本格式
    tableFormat.setAlignment(Qt::AlignHCenter);
    tableFormat.setBackground(QColor("#e0e0e0"));
    tableFormat.setCellPadding(2);
    tableFormat.setCellSpacing(4);
//! [6] //! [7]
    QVector<QTextLength> constraints;               // 设置列宽
    constraints << QTextLength(QTextLength::PercentageLength, 14)
                << QTextLength(QTextLength::PercentageLength, 14)
                << QTextLength(QTextLength::PercentageLength, 14)
                << QTextLength(QTextLength::PercentageLength, 14)
                << QTextLength(QTextLength::PercentageLength, 14)
                << QTextLength(QTextLength::PercentageLength, 14)
                << QTextLength(QTextLength::PercentageLength, 14);
    tableFormat.setColumnWidthConstraints(constraints);
//! [7]

//! [8]
    QTextTable *table = cursor.insertTable(1, 7, tableFormat);  // 插入table
//! [8]

//! [9]
    QTextFrame *frame = cursor.currentFrame();                  // root frame
    QTextFrameFormat frameFormat = frame->frameFormat();
    frameFormat.setBorder(1);                                   // 设置边缘宽1
    frame->setFrameFormat(frameFormat);                         // 设置frame格式
//! [9]

//! [10]
    QTextCharFormat format = cursor.charFormat();               // char格式
    format.setFontPointSize(fontSize);                          // 设置字符大小

    QTextCharFormat boldFormat = format;                        // 设置粗字体字体的格式
    boldFormat.setFontWeight(QFont::Bold);

    QTextCharFormat highlightedFormat = boldFormat;             // 设置高亮字的背景色的格式
    highlightedFormat.setBackground(Qt::yellow);
//! [10]

//! [11]
    for (int weekDay = 1; weekDay <= 7; ++weekDay) {            // 写上粗体的第一行:星期
        QTextTableCell cell = table->cellAt(0, weekDay-1);
//! [11] //! [12]
        QTextCursor cellCursor = cell.firstCursorPosition();
        cellCursor.insertText(QString("%1").arg(QDate::shortDayName(weekDay)),
                              boldFormat);
    }
//! [12]

//! [13]
    table->insertRows(table->rows(), 1);                        // 星期后插入一行
//! [13]

    while (date.month() == selectedDate.month()) {              // 月份为当前月,绘制当前月日历
        int weekDay = date.dayOfWeek();
        QTextTableCell cell = table->cellAt(table->rows()-1, weekDay-1);
        QTextCursor cellCursor = cell.firstCursorPosition();

        if (date == QDate::currentDate())                       // 将系统当前的这天高亮
            cellCursor.insertText(QString("%1").arg(date.day()), highlightedFormat);
        else
            cellCursor.insertText(QString("%1").arg(date.day()), format);

        date = date.addDays(1);                                 // date的日期加一天
        if (weekDay == 7 && date.month() == selectedDate.month())
            table->insertRows(table->rows(), 1);                // 到达一行的末端(星期天)转向下一行
    }

    cursor.endEditBlock();          // 结束块编辑
//! [14]
    setWindowTitle(tr("Calendar for %1 %2"                      // 该边标题
        ).arg(QDate::longMonthName(selectedDate.month())
        ).arg(selectedDate.year()));
}


 

Order Form

本例是一个订货小模拟软件,主程序打开后生成一个默认的订单。菜单中选择File->New可以订购新订单:将弹出如图中的Details对话框。填写信息后点击OK,程序将为之创建一个订单标签展示于当前TabWidget中。这里只展示出在住程序中是如何编辑rich text的代码。

 

void MainWindow::createLetter(const QString &name, const QString &address,
                              QList<QPair<QString,int> > orderItems,
                              bool sendOffers)
{
    QTextEdit *editor = new QTextEdit;              // 创建QTextEdit到标签页标签
    int tabIndex = letters->addTab(editor, name);
    letters->setCurrentIndex(tabIndex);             // 设置标签当前索引
//! [1]

//! [2]
    QTextCursor cursor(editor->textCursor());       // 获取QTextEdit对象的cursor
    cursor.movePosition(QTextCursor::Start);        // 移到开始位置
//! [2] //! [3]
    QTextFrame *topFrame = cursor.currentFrame();   // root frame
    QTextFrameFormat topFrameFormat = topFrame->frameFormat();
    topFrameFormat.setPadding(16);
    topFrame->setFrameFormat(topFrameFormat);       // root frame设置frame格式

    QTextCharFormat textFormat;
    QTextCharFormat boldFormat;
    boldFormat.setFontWeight(QFont::Bold);

    QTextFrameFormat referenceFrameFormat;          // 用于右上方显示信息的框格式
    referenceFrameFormat.setBorder(1);
    referenceFrameFormat.setPadding(8);
    referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
    referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));
    cursor.insertFrame(referenceFrameFormat);       // 添加refernceFrameFormat格式的框到root frame

    cursor.insertText("A company", boldFormat);
    cursor.insertBlock();
    cursor.insertText("321 City Street");
    cursor.insertBlock();
    cursor.insertText("Industry Park");
    cursor.insertBlock();
    cursor.insertText("Another country");
//! [3]

//! [4]
    cursor.setPosition(topFrame->lastPosition());   // 光标移出到上一位置,这里是指开始位置

    cursor.insertText(name, textFormat);            // 插入文本,订货者信息
    QString line;
    foreach (line, address.split("\n")) {           // 将address信息分解换行展示
        cursor.insertBlock();
        cursor.insertText(line);
    }
//! [4] //! [5]
    cursor.insertBlock();                           // 插入新块,分隔作用
    cursor.insertBlock();

    QDate date = QDate::currentDate();              // 当前系统时间,即生成订单的时间
    cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
                      textFormat);                  // 插入文本:订单生成时间
    cursor.insertBlock();                           // 换行

    QTextFrameFormat bodyFrameFormat;       // 定义一个文本框bodyFrameFormat,没有边框
    bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
    cursor.insertFrame(bodyFrameFormat);    // 插入框到root frame
//! [5]

//! [6]
    // 插入文本
    cursor.insertText(tr("I would like to place an order for the following "
                         "items:"), textFormat);
    cursor.insertBlock();
//! [6] //! [7]
    cursor.insertBlock();      // 间隔行
//! [7]

//! [8]
    QTextTableFormat orderTableFormat;                        // 订单表
    orderTableFormat.setAlignment(Qt::AlignHCenter);          // 横向中央对齐
    QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);      // 插入一行两列的表

    QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat(); // 包含订单表的框格式
    orderFrameFormat.setBorder(1);                            // 设置1个单位边缘
    cursor.currentFrame()->setFrameFormat(orderFrameFormat);  // 插入框到 bodyFrame
//! [8]

//! [9]
    cursor = orderTable->cellAt(0, 0).firstCursorPosition();  // 添加表元素
    cursor.insertText(tr("Product"), boldFormat);
    cursor = orderTable->cellAt(0, 1).firstCursorPosition();
    cursor.insertText(tr("Quantity"), boldFormat);
//! [9]

//! [10]
    for (int i = 0; i < orderItems.count(); ++i) {            // 添加order表中的数据
        QPair<QString,int> item = orderItems[i];
        int row = orderTable->rows();

        orderTable->insertRows(row, 1);
        cursor = orderTable->cellAt(row, 0).firstCursorPosition();
        cursor.insertText(item.first, textFormat);
        cursor = orderTable->cellAt(row, 1).firstCursorPosition();
        cursor.insertText(QString("%1").arg(item.second), textFormat);
    }
//! [10]

//! [11]
    cursor.setPosition(topFrame->lastPosition());

    cursor.insertBlock();
//! [11] //! [12]
    cursor.insertText(tr("Please update my records to take account of the "
                         "following privacy information:"));
    cursor.insertBlock();
//! [12]

//! [13]
    QTextTable *offersTable = cursor.insertTable(2, 2);         // 插入一个2*2的表

    cursor = offersTable->cellAt(0, 1).firstCursorPosition();   // 为第二列位置插入文本
    cursor.insertText(tr("I want to receive more information about your "
                         "company's products and special offers."), textFormat);
    cursor = offersTable->cellAt(1, 1).firstCursorPosition();
    cursor.insertText(tr("I do not want to receive any promotional information "
                         "from your company."), textFormat);

    if (sendOffers)    // 根据是否选中了sendOffers选择光标放置位置
        cursor = offersTable->cellAt(0, 0).firstCursorPosition();
    else
        cursor = offersTable->cellAt(1, 0).firstCursorPosition();

    cursor.insertText("X", boldFormat);  // 插入文本"X"
//! [13]

//! [14]
    cursor.setPosition(topFrame->lastPosition());       // 在最后插入文本
    cursor.insertBlock();
    cursor.insertText(tr("Sincerely,"), textFormat);
    cursor.insertBlock();
    cursor.insertBlock();
    cursor.insertBlock();
    cursor.insertText(name);

    printAction->setEnabled(true);      // 设置打印动作可用了
}


 


http://www.niftyadmin.cn/n/4364468.html

相关文章

分页方案

分页方案 下面的存储过程不仅含有分页方案&#xff0c;还会根据页面传来的参数来确定是否进行数据总数统计。 -- 获取指定页的数据 Create PROCEDURE pagination3 tblName varchar(255), -- 表名 strGetFields varchar(1000) *, -- 需要返回的列 fldName varch…

cocos2d-x动画制作(cocos2d-x2.1)

本博客参考资料&#xff1a;http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d 预备 用到的图片下载 TexturePacker是资源打包器&#xff0c;将资源打包&#xff0c;一起载入到游戏既方便又可以提升性能。 将下载好的图片解压缩&#…

用户控件使用一例

用户控件使用一例 用户控件的简单使用例子&#xff0c;做一个用户登陆的用户控件。用户登陆某个系统是个非常常见的问题&#xff0c;这里笔者想把这样的问题作为一个用户控件来使用&#xff0c;方便各个用户。 1、在工程中新建立一个Web用户控件&#xff0c;此时加入的名称为F…

cocos2d-x按钮菜单(cocos2d-x2.1)

首先看看程序运行时&#xff1a; 程序中有四种菜单项按钮&#xff1a;文本菜单项&#xff08;stop walk&#xff09;&#xff0c;图字菜单项&#xff08;Hide Bear&#xff09;&#xff0c;Toggle菜单项&#xff08;Go Right&#xff09;以及图片菜单&#xff08;程序开关&…

用下拉列表控制gridview的分页

用下拉列表控制gridview的分页 protected void GridView1_DataBound(object sender, EventArgs e) { if (!IsPostBack) {//判断页是否第一次载入&#xff0c;如果你想知道为什么&#xff0c;恰恰我又不想解释&#xff0c;那你去掉这个试下即可 …

cocos2d-x GUI控件的使用(cocos2d-x2.1)

在使用extensions时都需要额外指定include、lib&#xff0c;例如笔者的VS11环境&#xff0c;右键项目 属性-->配置属性-->VC目录在包含目录中添加F:\cocos2d-2.1beta3-x-2.1.1\extensions;以及在链接器-->命令行添加libextensions.lib或者使用代码添加 #pragma comme…

用正则表达式突出显示字符串中查询到的单词的函数

用正则表达式突出显示字符串中查询到的单词的函数 Function BoldWord(strContent,word)dim objRegExpSet objRegExpnew RegExpobjRegExp.IgnoreCase trueobjRegExp.GlobalTrue objRegExp.Pattern"(" & word & ")"strContentobjRegExp.Replace(st…

box2d弹球 cocos2d-x重力感应(cocos2d-x2.1)

本博客例子参考&#xff1a;raywenderlich 我将上面obj-c的翻译成了C&#xff0c;改动很少。首先在win32下新建新工程 修改HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "Box2D/Box2D.h&qu…