嵌入式学习笔记1——点击按钮绘图

  1. 文本垂直显示

pp->drawText(65, this->rect().y(), 40,180, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextWordWrap, QString("%1").arg(tr(“纵坐标”)));

  1. 窗口构建函数中加入此句可以显示中文

QTextCodec::setCodecForTr(QTextCodec::codecForName(“utf8”));

  1. 此句的作用为为按钮添加背景图片

ui->pushButton->setStyleSheet(“QPushButton{border-image:url(/root/untitled/02.png);}”);

  1. 渐变填充

QLinearGradient ll(180,400,250,300);
ll.setColorAt(0,QColor(132,112,255));
ll.setColorAt(0.5,QColor(176,196,222));
ll.setColorAt(1,QColor(106,90,205));
pp->setBrush(ll);

代码:


**头文件**
#include 
#include  

**public声明**
    void paintEvent(QPaintEvent *e);
    QPixmap aaaa;
    QPainter *pp;
    QBrush brush;
    
**构造函数**
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this); QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
    aaaa=QPixmap(QSize(1100,900));
    aaaa.fill(Qt::white);
    pp=new QPainter(&aaaa);
}

MainWindow::~MainWindow()
{
    delete ui;  
}

void MainWindow::on_pushButton_clicked()
{   ui->pushButton->setStyleSheet("QPushButton{border-image:url(/root/untitled/02.png);}");
    QPen pen(QColor(105,139,34),3,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
    pp->setPen(pen);
    pp->drawLine(QPoint(50,70),QPoint(50,650));
    pp->drawLine(QPoint(50,650),QPoint(650,650));
    pp->drawText(65, this->rect().y(), 40,180, Qt::AlignCenter | Qt::TextSingleLine | Qt::TextWordWrap, QString("%1").arg(tr("纵坐标")));
    pp->drawText(600,625,tr("横坐标"));
    this->update();
}

void MainWindow::on_pushButton_2_clicked()
{   ui->pushButton_2->setStyleSheet("QPushButton{border-image:url(/root/untitled/02.png);}");
    QPen pen(QColor(70,130,180),3,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
    pp->setPen(pen);
    QBrush brush(QColor(135,206,250),Qt::SolidPattern);
    pp->setBrush(brush);
    pp->drawRect(QRect(200,70,250,150));
    this->update();
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->pushButton_3->setStyleSheet("QPushButton{border-image:url(/root/untitled/02.png);}");
    QPen pen(QColor(173,216,230),3,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
    pp->setPen(pen);
    QLinearGradient ll(180,400,250,300);
           ll.setColorAt(0,QColor(132,112,255));
             ll.setColorAt(0.5,QColor(176,196,222));
               ll.setColorAt(1,QColor(106,90,205));
           pp->setBrush(ll);
    pp->drawEllipse(QRect(180,400,300,140));
    this->update();
}

void MainWindow::on_pushButton_4_clicked()
{
    ui->pushButton_4->setStyleSheet("QPushButton{border-image:url(/root/untitled/02.png);}");
    QPen pen(QColor(0,0,0),3,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
    pp->setPen(pen);
    QFont drawfont("URW Palladio L",40);
    pp->setFont(drawfont);
    pp->drawText(700,150,tr("欢迎使用qt"));
    this->update();
}

void MainWindow::on_pushButton_5_clicked()
{
    ui->pushButton_5->setStyleSheet("QPushButton{border-image:url(/root/untitled/02.png);}");
    QPixmap pixmap("/root/untitled/01.png");
    pp->drawPixmap(650,220,400,350,pixmap);
    this->update();
}

void MainWindow::paintEvent(QPaintEvent*)
{
    QPainter p(this);
    p.drawPixmap(0,0,aaaa);
}

学习总结:
1.在painterEvent事件中绘图,这样使用qt可实现paintevent以外的兼容arm 和window的绘图。
2.获取图片的路径QPixmap pixmap("/root/untitled/01.png");可以从文件夹中右键图片属性查看。
3.每个按钮代码里需调用this->update来调用窗口重画函数。
4.Pen用来设置画笔的颜色和大小粗细,Brush用来设置填充的颜色。

你可能感兴趣的:(嵌入式)