Qt中对text在程序中设置字体大小的方法

1、设置字体粗细

setFontWeight(int weight)

enum QFont::Weight可取以下各值:

 

2、设置字体斜体

setFontItalic(bool italic)

true表示斜体,false为非斜体。

 

3、设置下划线

setFontUnderline(bool underline)

true表示有下划线,false无。

 

4、设置字体类型

setFontFamily(const QString & fontFamily)

 

5、设置字体大小

setFontPointSize(qreal s)

 

6、设置文本色

setTextColor(const QColor & c)

 

7、设置文本背景色

setTextBackgroundColor(const QColor & c)

 

8、设置对齐方式

setAlignment(Qt::Alignment a)

Qt::Alignment取值如下:


Qt::AlignLeft左对齐、Qt::AlignRigh右对齐、Qt::AlignCenter居中对齐

 

    好了,方法太多,而且很简单,就不一一列举了,下面看主要的:

 

9、插入图片:

void Widget::insertImage()
{
        QImage image(":/Images/qq");
        if (image.isNull()) 
                return;

        int width = text_edit->viewport()->width();
        int height = text_edit->viewport()->height();
        if (image.width() > width || image.height() > height) {
                image = image.scaled(30, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }

        QTextCursor cursor = text_edit->textCursor();
        QTextDocument *document = text_edit->document();
        cursor.movePosition(QTextCursor::End);

        
        document->addResource(QTextDocument::ImageResource, QUrl(":/Images/qq"),  QVariant(image));

        //插入图像,使用QTextCursor API文档:
        QTextImageFormat image_format;
        image_format.setName(":/Images/qq");
        cursor.insertImage(image_format);
}

    或者,使用HTML的img标记
 

10、搜索匹配文本进行高亮

void Widget::search()
{
    QString search_text = search_line_edit->text();
    if (search_text.trimmed().isEmpty()) {
        QMessageBox::information(this, tr("Empty search field"), tr("The search field is empty."));
    } else {
        QTextDocument *document = text_edit->document();
        bool found = false;
        QTextCursor highlight_cursor(document);
        QTextCursor cursor(document);

        //开始
        cursor.beginEditBlock();

        QTextCharFormat color_format(highlight_cursor.charFormat());
        color_format.setForeground(Qt::red);
        while (!highlight_cursor.isNull() && !highlight_cursor.atEnd()) {

            //查找指定的文本,匹配整个单词
            highlight_cursor = document->find(search_text, highlight_cursor, QTextDocument::FindWholeWords);
            if (!highlight_cursor.isNull()) {
           if(!found)
               found = true;
                    highlight_cursor.mergeCharFormat(color_format);
            }
        }
        cursor.endEditBlock();
        //结束

        if (found == false) {
            QMessageBox::information(this, tr("Word not found"), tr("Sorry,the word cannot be found."));
        }
    }
}
  • 4
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SunnyFish-ty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值