本文主要是介绍Qt stylesheet border-color属性,QFontMetrics Class,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、border-color
border-color 属性设置四条边框的颜色。此属性可设置 1 到 4 种颜色。
border-color 属性是一个简写属性,可设置一个元素的所有边框中可见部分的颜色,或者为 4 个边分别设置不同的颜色。请看下面的例子:
border-color:red green blue pink;
- 上边框是红色
- 右边框是绿色
- 下边框是蓝色
- 左边框是粉色
border-color:red green blue;
- 上边框是红色
- 右边框和左边框是绿色
- 下边框是蓝色
border-color:dotted red green;
- 上边框和下边框是红色
- 右边框和左边框是绿色
border-color:red;
- 都为红色边框
二、QTextEdit文字修改行距
QTextDocument *doc = ui->textEdit_label->document();
QTextCursor textcursor = ui->textEdit_label->textCursor();
for(QTextBlock it = doc->begin(); it !=doc->end();it = it.next())
{QTextBlockFormat tbf = it.blockFormat();tbf.setLineHeight(lineSpacing,QTextBlockFormat::LineDistanceHeight);textcursor.setPosition(it.position());textcursor.setBlockFormat(tbf);ui->textEdit_label->setTextCursor(textcursor);
}
三、想要根据文字字数,适配控件背景长度,所以需要计算字符串个数,以及单个字符宽度
如果程序不支持中文,需要设置编码
QTextCodec *codec = QTextCodec::codecForName("System"); //获取系统编码
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
int tlength = msg.toLocal8Bit().length();//计算英文字符数
msg="中" tlength = 2
msg="Qt" tlength = 2
四、Qt 中获取字体的像素高度和宽度
QLabel::fontMetrics().width(QString s): 获取字符串s的总像素宽度。
int QFontMetrics::width ( const QString & text, int len = -1 ) const
Returns the width in pixels of the first len characters of text. If len is negative (the default), the entire string is used.Note that this value is not equal to boundingRect().width(); boundingRect() returns a rectangle describing the pixels this string will cover whereas width() returns the distance to where the next string should be drawn.
See also boundingRect().
返回文本的第一个len字符的宽度(以像素为单位)。如果“len”为负(默认值),则使用整个字符串。
请注意,该值不是等于boundingrect()。width();boundingrect()返回一个矩形,描述该字符串将覆盖的像素,而width()返回应绘制下一个字符串的距离。
另请参见boundingrect()。
QLabel::fontMetrics().height(): 获取字体的高度。
int QFontMetrics::height () const
Returns the height of the font.This is always equal to ascent()+descent()+1 (the 1 is for the base line).
See also leading() and lineSpacing().
QLabel::fontMetrics().lineSpacing(): 获取字体的高度,包括文字的实际宽度和行距。
int QFontMetrics::lineSpacing () const
Returns the distance from one base line to the next.This value is always equal to leading()+height().
See also height() and leading().
QLabel::fontMetrics().leading(): 行间距
int QFontMetrics::leading () const
Returns the leading of the font.This is the natural inter-line spacing.See also height() and lineSpacing().
这篇关于Qt stylesheet border-color属性,QFontMetrics Class的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!