Qt中对text在程序中设置字体⼤⼩的⽅法1、设置字体粗细
setFontWeight(int weight)
enum QFont::Weight可取以下各值:
2、设置字体斜体
setFontItalic(bool italic)
true表⽰斜体,false为⾮斜体。
fontweight默认值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();
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 (immed().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;
CharFormat(color_format);
}
}
//结束
if (found == false) {
QMessageBox::information(this, tr("Word not found"), tr("Sorry,the word cannot be found."));
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论