赞
踩
首先实现自己的label类:
myLabel.h:
- #ifndef MYLABEL_H
- #define MYLABEL_H
- #include <QEvent>
- #include <QLabel>
- class myLabel : public QLabel
- {
- public:
- myLabel();
- ~myLabel();
- void enterEvent(QEvent *e); //hover鼠标进入
- void leaveEvent(QEvent *e); //hover鼠标离开
- void mousePressEvent(QMouseEvent *e); //鼠标按下
- private:
- bool ifPressed;
- };
-
- #endif // MYLABEL_H

myLabel.cpp:
- #include "myLabel.h"
- #include <QPalette>
-
- myLabel::myLabel()
- {
- //setFrameShape(QFrame::Box); //先设置边框为矩形
- //setStyleSheet("border-width: 0px;");
- ifPressed = false;
- }
-
- myLabel::~myLabel()
- {
-
- }
-
- void myLabel::enterEvent(QEvent *e)
- {
- if(ifPressed == false)
- setStyleSheet("border-width: 4px;border-style: solid;border-color: rgb(220,20,60);");
- }
-
- void myLabel::leaveEvent(QEvent *e)
- {
- if(ifPressed == false)
- setStyleSheet("border-width: 0px;");
- }
-
- //鼠标按下
- void myLabel::mousePressEvent(QMouseEvent *e)
- {
-
- if(ifPressed == false)
- {
- ifPressed = true;
- QColor highLightColor = palette().color(QPalette::Highlight);
- QString stringColor = QString("rgb(%1,%2,%3)")
- .arg(highLightColor.red())
- .arg(highLightColor.green())
- .arg(highLightColor.blue());
- QString stringStyleSheet = QString("border-width: 4px;border-style: solid;border-color: %1;").arg(stringColor);
- setStyleSheet(stringStyleSheet);
- }
- else
- {
- ifPressed = false;
- setStyleSheet("border-width: 0px;");
- }
-
- }

这里用QColor获取了palette调色板的颜色,并用QString的方式进行设置(也许有更简单的方式实现。。。。我直接用palette不行)。
mainwindow.cpp部分代码:
- ui->setupUi(this);
-
- myLabel *pic = new myLabel;
- pic -> setParent(this);
- pic->setGeometry(rect().x()+80, rect().y()+30,400, 400);
-
- QPixmap pix("/home/kylin-fc/image/123.png");
- pic->setPixmap(pix);
- //pic->setPixmap(QPixmap::fromImage(QIcon::fromTheme("ukui-control-center").pixmap(24,24).toImage()));
完整项目地址:
https://download.csdn.net/download/IT8343/15840250?spm=1001.2014.3001.5503
附Qwidget显示图片:
- QSize IMAGE_SIZE(381, 221);
- ui->previewWidget->setAutoFillBackground(true);
- QPalette palette;
- palette.setBrush(QPalette::Background, QBrush(QPixmap("/home/kylin-fc/TEMP.bmp").scaled(IMAGE_SIZE)));
- ui->previewWidget->setPalette(palette);
-
- /*显示屏保
- QStringList args;
- QString screensaver_bin = "/usr/lib/ukui-screensaver/ukui-screensaver-default";
- args << "-window-id" << QString::number(ui->previewWidget->winId());
- process->startDetached(screensaver_bin, args);
- */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。