一、前言
用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);
}
三、效果圖
四、開源主頁
以上作品完整源碼下載都在開源主頁,會持續不斷更新作品數量和質量,歡迎各位關注。