zoukankan      html  css  js  c++  java
  • Qt 高级UI篇重写自己想要的控件以QLabel为列实现旋转,放大,缩小

    今天的风儿真喧嚣呀!

    1234

    我们开始吧

    ------------------------------------------------------------------华丽的开始线--------------------------------------------------------------

    用过QT的都知道QT的控件真的不好看,那么你可能需要自己重写了,不用担心重写还是很麻烦的。

    首先请看图:

    还是很好看的:::::

    首先博主继承的是QLabel

    下面是代码的头文件h:

     1 #pragma once
     2 #include <QLabel> 
     3 #include <QMouseEvent>
     4 #include <QPropertyAnimation>
     5 #include "Tool.h"
     6 #include "qdebug.h"
     7 #include <QTimer>
     8 #include <QTimerEvent>
     9 class LogoLabel :public QLabel
    10 {
    11     Q_OBJECT
    12 public:
    13     explicit LogoLabel(QWidget *parent = 0);
    14     LogoLabel(const QString &text, QWidget *parent = 0);
    15     void setQPixmapPath(QString path);
    16     void setImageSize(int imageWidth, int imageHeight);
    17 signals:
    18     // 鼠标单击信号  
    19     void clicked();  
    20 protected:
    21     // 鼠标单击事件  
    22     void mouseReleaseEvent(QMouseEvent *);
    23     void enterEvent(QEvent *);
    24     void leaveEvent(QEvent *);
    25     //定时器
    26     void timerEvent(QTimerEvent *event); 
    27 private:
    28     //图片缓存(使用缓存技术免得重复打开图片造成浪费)
    29     QPixmap logoPix;
    30     //是否在旋转
    31     bool isRotate = true;
    32     bool isMagnify = true;
    33     //存储图片的大小
    34     int imageWidth = 0;
    35     int imageHeight = 0;
    36     //图片的地址
    37     QString pixmapPath; 
    38     //旋转定时器
    39     int rotateTimerId;
    40     //旋转度数
    41     int rotateNumber = 0;
    42     public slots:
    43 public :
    44     //按钮是否放大
    45     void setMagnify(bool isMagnify){
    46         this->isMagnify = isMagnify;
    47     }
    48     //按钮是否旋转
    49     void setRotate(bool isRotate){
    50         this->isRotate = isRotate;
    51     }
    52     //按钮旋转
    53     void setImageTransparency(int transNumber);
    54 };

    注释已经很详细了:还有不明白的可以在下面留言。

    那么我们再来看看cpp主体文件:

     1 #include "LogoLabel.h" 
     2 LogoLabel::LogoLabel(QWidget *parent) :
     3 QLabel(parent)
     4 { 
     5 }
     6  
     7 LogoLabel::LogoLabel(const QString &text, QWidget *parent) :
     8 QLabel(parent)
     9 {
    10     setText(text);
    11 }
    12  
    13 //鼠标释放事件
    14 void LogoLabel::mouseReleaseEvent(QMouseEvent *ev)
    15 {
    16     if (ev->button() == Qt::LeftButton)
    17         emit clicked();//(this);  
    18 }
    19 //鼠标进入事件
    20 void LogoLabel::enterEvent(QEvent *)
    21 { 
    22     this->setWindowOpacity(1); 
    23     QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight);
    24     //如果可以旋转就启动定时器
    25     if (isRotate){
    26         rotateTimerId = startTimer(50);
    27     } 
    28     //如果放大就设置图片发达1.5倍
    29     if (isMagnify){
    30         setImageSize(this->imageWidth*1.5, this->imageHeight*1.5);
    31     }
    32 }
    33 //设置图片大小事件
    34 void LogoLabel::setImageSize(int imageWidth, int imageHeight){
    35     this->imageWidth = imageWidth;
    36     this->imageHeight = imageHeight;
    37 }
    38 //设置图片路劲
    39 void LogoLabel::setQPixmapPath(QString path){
    40     pixmapPath = path;
    41     logoPix = QPixmap(path);
    42     QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight);
    43     Tool::setPixMapTransparency(tempLogoPix);
    44     this->setPixmap(tempLogoPix);
    45 }
    46 //设置图片透明度
    47 void LogoLabel::setImageTransparency(int transNumber){ 
    48     QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight);
    49     Tool::setPixMapTransparency(tempLogoPix, transNumber); 
    50     this->setPixmap(tempLogoPix);
    51 }
    52 //定时器
    53 void LogoLabel::timerEvent(QTimerEvent *event){
    54     if (event->timerId() == rotateTimerId)
    55     {
    56         if (isRotate){
    57             //默认一次旋转3个角度
    58             rotateNumber += 3; 
    59             QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight);
    60             Tool::setPixMapRotate(tempLogoPix, rotateNumber);
    61             this->setPixmap(tempLogoPix);
    62         }
    63         else{
    64             killTimer(rotateTimerId);
    65             rotateTimerId = 0;
    66         } 
    67     }
    68     else{
    69         QWidget::timerEvent(event);
    70     }
    71 }
    72 //鼠标离开事件
    73 void LogoLabel::leaveEvent(QEvent *)
    74 {
    75     if (isRotate && (rotateTimerId != 0)){
    76         killTimer(rotateTimerId);
    77     }
    78     if (isMagnify){
    79         setImageSize(this->imageWidth / 1.5, this->imageHeight / 1.5);
    80     } 
    81     QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight);
    82     Tool::setPixMapTransparency(tempLogoPix);
    83     this->setPixmap(tempLogoPix);
    84     rotateNumber = 0; 
    85 }

    还有一个Tool工具类配合使用那么上代码吧:

     1 #pragma once
     2 #include <QObject>
     3 #include <QPainter>
     4 #include <QPixmap>
     5 #include <qsize.h>
     6 class Tool :public QObject
     7 {
     8     Q_OBJECT
     9 public:
    10     Tool();
    11     ~Tool();
    12     static void setPixMapTransparency(  QPixmap &pix, int transparencyNumber=100);
    13     static void setPixMapRotate(QPixmap &pix, int rotateNumber = 0);
    14 };

    定义为静态的主要是因为方便调用..................

    工具类的Cpp文件:

     1 nclude "Tool.h" 
     2 Tool::Tool()
     3 {
     4 }
     5 //设置图片旋转度数
     6 void Tool::setPixMapRotate(QPixmap &pix, int rotateNumber ){
     7     int imageWidth = pix.width();
     8     int imageHeight = pix .height();
     9  
    10     QPixmap temp(pix.size());
    11     temp.fill(Qt::transparent);
    12     QPainter  painter(&temp); 
    13     painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    14     painter.translate(imageWidth / 2, imageHeight / 2); //让图片的中心作为旋转的中心
    15     painter.rotate(rotateNumber); //顺时针旋转90度
    16     painter.translate(-(imageWidth / 2), -(imageHeight / 2)); //使原点复原
    17     painter.drawPixmap(0, 0,   pix);
    18     painter.end();
    19     pix = temp;
    20 }
    21 //设置图片透明度
    22 void Tool::setPixMapTransparency(  QPixmap &pix,int transparencyNumber){
    23     QPixmap temp(pix.size());
    24     temp.fill(Qt::transparent); 
    25     QPainter  imagePain(&temp);
    26     imagePain.setCompositionMode(QPainter::CompositionMode_Source);
    27     imagePain.drawPixmap(0, 0, pix);
    28     imagePain.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    29     imagePain.fillRect(temp.rect(), QColor(0, 0, 0, transparencyNumber));
    30     imagePain.end();
    31     pix = temp;
    32 }
    33  
    34 Tool::~Tool()
    35 {
    36 }

    一切都是这么麻烦,当然既然是C++那么麻烦也是一件好事。

    学习就像爬山,没有爬不过去的山,只有不想爬的人。

    欢迎大家一起来分享你的好的代码。

    -----------------------------------------------------------------------------------------华丽的结束线---------------------------------------------------------------------------------

     

  • 相关阅读:
    windows 查看端口被占用
    IOC控制反转
    spring配置,spring中的bean 的id不能相同
    prototype 用法
    js闭包用法
    struts2异常处理,global-results定义全局结果处理
    struts2中错误处理
    Struts2配置细节
    hql查询语句 内存中的情况,fetch迫切查询关键字
    Spring cloud微服务安全实战-4-5搭建OAuth2认证服务器
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/13900977.html
Copyright © 2011-2022 走看看