QT怎么设置字体轮廓、字体位置、字体样式、字体间距、窗口背景色大小、隐藏鼠标图标

QT怎么设置字体轮廓、字体位置、字体样式、字体间距、窗口背景色大小、隐藏鼠标图标

原创  2017年12月14日 19:05:21
  • 277

在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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值