QWidget|QFrame sets the background to be transparent and can have a border color

QWidget|QFrame sets the background to be transparent and can have a border color

  • Chapter1 “Qt” part 6 QSS Qt style sheet – interface beautification 1($$$)
  • Chapter2 [QT] QSS beautification – basic knowledge
  • Chapter3 QWidget|QFrame sets the background to be transparent and can have a border color
  • Reference link
  • Chapter4 Several methods for setting form (QWidget) transparency in Qt
    • 1. Set the background color of the form
    • 2. Use functions
  • Chapter5 Qt QSS Basics ($$$)
  • Chapter6 Getting Started with QSS – Creating a Simple Login Interface
  • Chapter7 [QT Programming Series-39]: User Interface UI – Quick Start with Style Sheets QSS and Style Files ($$$)

Chapter1 “Qt” part 6 QSS Qt style sheet – interface beautification 1 ($$$)

Original link: https://blog.csdn.net/sinat_24206709/article/details/55805254

Style sheets add flavor to your boring Qt GUI.

For a long time, Qt has allowed you to use CSS’ish style sheets to decorate your GUI. Inspired by the web, stylesheets are a great way to stylize your Qt GUI, but few people seem to use them. In this tutorial, we will create a sample dialog box in Qt using Designer and stylesheets. This tutorial assumes that you can work around Qt Designer and that you know something about Qt layout.

First method:

ui->widget->setAutoFillBackground(true);
QPalette palette;
palette.setColor(QPalette::Background, QColor(0,0,0,150)); // The last item is transparency
//palette.setBrush(QPalette::Background, QBrush(QPixmap(":/background.png")));
ui->widget->setPalette(palette);

The second method (recommended):

this->setWindowFlags(Qt::FramelessWindowHint);//No title bar
this->setAttribute(Qt::WA_TranslucentBackground,true); //Set the background to be transparent

Remember to add an extra widget on top of the main widget.


Chapter2 [QT] QSS beautification – basic knowledge

Original link

Chapter3 QWidget|QFrame sets the background to be transparent and can have a border color

Original link: https://blog.csdn.net/Stone_OverLooking/article/details/111590068

There was a requirement in the project before: the electronic magnification function of the video playback window requires drawing a rectangular frame that can be dragged on the QWidget that plays the video, but drawRect alone cannot obtain focus for dragging. So I thought of another way to use a transparent

QWidget covers the window and sets its background to transparent. However, after setting it to be transparent, the black background color cannot be eliminated. This requirement was solved by combining the results of multiple CSDN bloggers.

In fact, these are the points:

Set background transparency

setWindowOpacity(1);
this->setAttribute(Qt::WA_TranslucentBackground, true);

Draw a rectangular frame in the window and fill the brush with an arbitrary color. The key point is to reduce its transparency to a value other than 0. Achieve transparency effect.

void AlargeShape::paintEvent(QPaintEvent *)
{<!-- -->
    QPainter painter(this);
    painter.setBrush(QColor(9,151,247,1));//The background color of all painter areas
    painter.setPen(QPen(Qt::red,2,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
    painter.drawRect(2, 2, end_pos.x()-X-4,end_pos.y()-Y-4);
}

Source code:

#include <QFrame>
class AlargeShape : public QFrame// QWidget
{<!-- -->
    Q_OBJECT
public:
    QWidget* widgetShape;
    explicit AlargeShape(QWidget *parent = nullptr);
    void paint(QPainter & amp;painter);
    void setWidgetGeometry(QPoint point);
signals:
private:
    //Set the state when the mouse drags the form
   bool m_move;
    /*Record the world coordinates of the mouse.*/
    QPoint m_startPoint;
    /*Record the world coordinates of the form.*/
    QPoint m_windowPoint;
private:
    //Record the position of the mouse
    QPoint start_pos;
    QPoint end_pos;
    int X,Y;
    //Record the location of the parent form
    QPoint parent_pos;
    int parent_width,parent_height;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event)override;
    void mouseReleaseEvent(QMouseEvent *event)override;
    void paintEvent(QPaintEvent *)override;
public:
    void SetParentPos(QPoint pos,int w,int h)
    {<!-- -->
        parent_pos=pos;
        parent_width=w;
        parent_height=h;
    }
    void SetStartPos(const QPoint pos){<!-- -->start_pos=pos;}
    void SetEndPos(const QPoint pos){<!-- -->end_pos=pos;}
};
#include "alargeshape.h"
#include 
#include 
#include 
#include 
AlargeShape::AlargeShape(QWidget *parent) //: QWidget(parent)
{
    this->setWindowFlags(Qt::Tool| Qt::FramelessWindowHint|Qt::WindowSystemMenuHint | Qt::WindowStaysOnTopHint);
    setObjectName("frameAlarge");
 
    setWindowOpacity(1);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
}
/****************************************************** *************************
 * @Description: Change the current window size, called in the paintEvent of the parent form
 * @parameter:
 *  @return:
 * @author:zhontao
 *@Time:2020-12-23 14:57:57
*************************************************** ************************/
void AlargeShape::paint(QPainter & amp;painter)
{
    this->show();
    this->resize(QSize(end_pos.x()-X,end_pos.y()-Y));
}
 
void AlargeShape::setWidgetGeometry(QPoint point)
{
    X=point.x();
    Y=point.y();
    this->setGeometry( point.x(), point.y(),0,0);
}
 
void AlargeShape::paintEvent(QPaintEvent *)
{<!-- -->
    QPainter painter(this);
    painter.setBrush(QColor(9,151,247,1));//The background color of all painter areas
    painter.setPen(QPen(Qt::red,2,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
    painter.drawRect(2, 2, end_pos.x()-X-4,end_pos.y()-Y-4);
}
 
void AlargeShape::mousePressEvent(QMouseEvent *event)
{
    /*When the left mouse button is clicked.*/
    if (event->button() == Qt::LeftButton)
    {
        m_move = true;
        /*Record the world coordinates of the mouse.*/
        m_startPoint = event->globalPos();
        /*Record the world coordinates of the form.*/
        m_windowPoint = this->frameGeometry().topLeft();
    }
}
 
void AlargeShape::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & amp; Qt::LeftButton)
    {
        //Calculate the coordinates of the four corner points of the parent window
        QPoint p_left_top=parent_pos;
        QPoint p_left_bottom=QPoint(parent_pos.x(),parent_pos.y() + parent_height);
        QPoint p_right_top=QPoint(parent_pos.x() + parent_width,parent_pos.y());
        QPoint p_right_bottom=QPoint(parent_pos.x() + parent_width,parent_pos.y() + parent_height);
 
//Limited rectangle is limited to the current window
// /*The relative position of the moving mouse position relative to the initial position.*/
// QPoint relativePos = event->globalPos() - m_startPoint;
        if( ((event->globalPos().x()-p_left_top.x())-(m_startPoint.x()-m_windowPoint.x()) >=0 ) & amp; & amp;
              ((event->globalPos().x()-p_right_top.x())-(m_startPoint.x()-(m_windowPoint.x() + this->width())) <=0 ) & amp; & amp;
              ((event->globalPos().y()-p_left_top.y())-(m_startPoint.y()-m_windowPoint.y()) >=0) & amp; & amp;
              ((event->globalPos().y()-p_left_bottom.y())-(m_startPoint.y()-(m_windowPoint.y() + this->height())) <=0)
          )
        {
            /*The relative position of the moving mouse position relative to the initial position.*/
            QPoint relativePos = event->globalPos() - m_startPoint;
            /*Then move the form.*/
            this->move(m_windowPoint + relativePos );
        }
    }
}
void AlargeShape::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        /*Change movement status.*/
        m_move = false;
    }
}

Rendering:

Reference link

Qt realizes the overlap of two windows, the lower layer plays the video, and the last transparent display box

Summary of methods for setting background transparency in QtWidget【Transfer】

Qt’s issue regarding the invalid background transparency encountered when implementing a custom title bar/window

Chapter4 Several methods to set the transparency of the form (QWidget) in Qt

Original link: https://blog.csdn.net/m0_60259116/article/details/127887533

1. Set the background color of the form

To add code in the constructor, you need to add the header file qpalette or qgui

QPalette pal = palette();
pal.setColor(QPalette::Background, QColor(0x00,0xff,0x00,0x00));
setPalette(pal);

This is accomplished by setting the background color of the form to fully transparent.

Effect: The entire window is transparent, but the window controls are opaque. The QLabel control only displays words, and the background color of the control is transparent; the client area of the form is completely transparent.

2. Using functions

setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);//Remove the title bar
setAttribute(Qt::WA_TranslucentBackground, true);//Set the window background to be transparent

Also in the constructor, the effect is the same as the first method.

Chapter5 Qt QSS Basics ($$$)

Original link: https://blog.csdn.net/u011832219/article/details/128183316

Introduction to Chapter6 QSS – Making a Simple Login Interface

Original link: https://blog.csdn.net/qq_39347787/article/details/130102515

Chapter7 [QT Programming Series-39]: User Interface UI – Quick Start with Style Sheets QSS and Style Files ($$$)

Original link: https://blog.csdn.net/HiWangWenBing/article/details/131750858

Qt provides a mechanism called Qt Style Sheets that can be used to customize and beautify the user interface (UI) of Qt applications.

Using Qt style sheets, you can define the appearance and layout of UI elements, including colors, fonts, borders, backgrounds, etc., through CSS-like syntax.