QT怎么设置字体轮廓、字体位置、字体样式、字体间距、窗口背景色大小、隐藏鼠标图标
在qt中经常会对字体设置一些属性字体轮廓、字体位置、字体样式、字体间距、窗口背景色大小。
下面是设置这些属性的代码:
/*mainwindow.cpp*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QFont>
#include <QLayout>
#include <QTextCodec>
ArrowWidget::ArrowWidget(QWidget *parent)
: QWidget(parent)
{
this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
this->showMaximized();
QTextCodec::setCodecForLocale (QTextCodec::codecForName("utf8"));//设置编码格式
resize(700,300);//设置窗口大小
}
void ArrowWidget::paintEvent(QPaintEvent *event)
{
int i = 100,j = 180; //str位置
QString str("ABCDE");
QPainterPath path;
QPen pen(Qt::black);//轮廓为黑色的
QPainter painter(this);
pen.setStyle(Qt::SolidLine);//轮廓线为实线
pen.setWidth(3);//设置字体轮廓线条粗细
QFont font("Times New Roman", 100);//设置字体样式
font.setLetterSpacing(QFont::PercentageSpacing,-2); //设置字间距 默认100%
font.setLetterSpacing(QFont::AbsoluteSpacing,-4); //默认1
font.setBold(true);//设置粗体
path.addText(i, j, font, str);//add path
painter.strokePath(path, pen);//set SolidLine style
painter.fillPath(path, QBrush(Qt::white));//字体颜色
painter.drawPath(path);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
/*main.cpp*/
#include "mainwindow.h"
#include <QApplication>
#include"qpainter.h"
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ArrowWidget w;
//设置背景色为red
QPalette palette(w.palette());
palette.setColor(QPalette::Background, Qt::red);
w.setPalette(palette);
w.show();
return a.exec();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
代码最后编译完后,要移植到开发板,如果要隐藏鼠标图标加如下参数:
例如我编译完代码生成可执行文件是:
untitled8 -qws --nomouse
- 1
完整工程地址下载:
http://download.csdn.net/download/yang_quan_yang/10159005