一對一視頻聊天app源碼Qt開源作品-無邊框背景透明窗體

一、前言
用Qt來做無邊框背景透明窗體非常簡單,根本不需要用什麼系統層的API來實現透明什麼的,Qt本身提供了很多種設置窗體透明的方法,除了可以設置窗體的屬性爲透明以外,還可以設置透明度函數,以及qss來設置透明度顏色等,方法很多,按照需要可以選用自己最合適的辦法,如果想要整個窗體的背景圖類似於無邊框的異行,你只需要準備一張美工做好的png帶透明的背景圖即可,直接用qss的形式設置爲窗體的背景圖,你也可以用painter繪製上去,這樣就可以產生各種奇形怪狀的異行窗體,比如中間挖個洞的背景圖,可以直接穿透桌面。

二、代碼思路

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
    ui->widget->installEventFilter(this);
    ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(1));
}

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

bool Widget::eventFilter(QObject *watched, QEvent *evt)
{
    static int index = 1;
    static QPoint mousePoint;
    static bool mousePressed = false;

    QMouseEvent *event = static_cast<QMouseEvent *>(evt);
    if (event->type() == QEvent::MouseButtonPress) {
        if (event->button() == Qt::LeftButton) {
            mousePressed = true;
            mousePoint = event->globalPos() - this->pos();

            if (index == 5) {
                index = 1;
            } else {
                index++;
            }

            ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(index));

            return true;
        } else {
            exit(0);
        }
    } else if (event->type() == QEvent::MouseButtonRelease) {
        mousePressed = false;
        return true;
    } else if (event->type() == QEvent::MouseMove) {
        if (mousePressed && (event->buttons() && Qt::LeftButton)) {
            this->move(event->globalPos() - mousePoint);
            return true;
        }
    }

    return QWidget::eventFilter(watched, event);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章