Qt implements custom icon functions

Author: hackett

Wechat official account: Overtime Ape

Build on the previous section to define the icon’s functionality

See the link from the previous section: Qt frameless window drags and shadows

Effect: All you need to do is zoom in and out, minimize, move the mouse and change color by clicking on the icon

Preparations:

Controls:

3 a PushButton

A Label

A Frame

Note: layout is required. If you do not layout, you may not be able to zoom in and out

Prepare the icon embed control

You can download the ICONS you want from alibaba vector icon library, or go to other websites or make them yourself

I’ve downloaded three ICONS here

QSS stylesheet

Right-click and select Change Style Sheet, do the same with any other key

Add color:

QPushButton changes the properties of the button

QPushButton:hover for the effect of moving the mouse to a button

QPushButton:pressed as the mouse button is pressed

Adding a resource:

You can select the image in our resources to fill the image for the key

Then you can link to the slot function and do the corresponding processing

Margin value needs to be set for the zoom in button

Void Widget::on_buttonMax_clicked() {if(this->isMaximized()) {UI ->vLayout->setMargin(9); // Margin of the widget layout this->showNormal(); }else { ui->vLayout->setMargin(0); // Margin for the widget layout in full screen is 0 this->showMaximized(); }}Copy the code

Source:

main.cpp

#include "widget.h"
#include <QApplication>
​
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
​
    return a.exec();
}
​
Copy the code

widget.cpp

#include "widget.h" #include "ui_widget.h" #include <QMouseEvent> #include <QWidget> #include <QGraphicsDropShadowEffect> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->setWindowFlags(Qt::FramelessWindowHint); QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(); shadow->setBlurRadius(5); Shadow ->setColor(Qt::black); Shadow ->setOffset(0); // Unoffset UI ->shadowWidget->setGraphicsEffect(shadow); this->setAttribute(Qt::WA_TranslucentBackground); // Set parent window transparent, leaving only child window} Widget::~Widget() {delete UI; } void Widget::mouseMoveEvent(QMouseEvent *event) {if(this->z == QPoint())} void Widget::mouseMoveEvent(QMouseEvent *event) {if(this->z == QPoint()); QPoint y = event->globalPos(); QPoint x = y - this->z; this->move(x); } void Widget::mousePressEvent(QMouseEvent *event) { QPoint y = event->globalPos(); QPoint x = this->geometry().topleft (); This ->z = y-x; this->z = y-x; / / setting value, the same} void Widget: : mouseReleaseEvent (QMouseEvent * event) {this - > z = QPoint (); } void Widget::on_buttonClose_clicked() {this->close(); } void Widget::on_buttonMax_clicked() {if(this->isMaximized()) {UI ->vLayout->setMargin(9); // Margin of the widget layout this->showNormal(); }else { ui->vLayout->setMargin(0); // Margin for the widget layout in full screen is 0 this->showMaximized(); } } void Widget::on_buttonMin_clicked() { this->showMinimized(); }Copy the code

widget.h

#ifndef WIDGET_H
#define WIDGET_H
​
#include <QWidget>
​
namespace Ui {
class Widget;
}
​
class Widget : public QWidget
{
    Q_OBJECT
​
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
​
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
​
private slots:
    void on_buttonClose_clicked();
​
    void on_buttonMax_clicked();
​
    void on_buttonMin_clicked();
​
private:
    Ui::Widget *ui;
    QPoint z;
};
​
#endif // WIDGET_H
Copy the code

If you think it’s good, “Like” it.

Follow my wechat official account [Overtime Ape] to get more content

\