方法一:
MainWindow w;
//方法一:主界面透明,界面里面的控件不透明
w.setWindowFlags(Qt::FramelessWindowHint);//设置无窗口框架边界
w.setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
w.show();
编辑界面:
运行效果:
方法二:
w.setWindowOpacity(0.5);//直接设置整个窗口(包含里面的控件)0完全透明,1不透明
运行效果是整个界面包括里面的控件都是透明度50%:
注意:
如果用方法一的话,一定要设置成无边框模式,否则界面会显示黑色,如下图:
具体代码如下: main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//方法一:主界面透明,界面里面的控件不透明
w.setWindowFlags(Qt::FramelessWindowHint);//设置无窗口框架边界
w.setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
//方法二:
// w.setWindowOpacity(0.5);//直接设置整个窗口(包含里面的控件)0完全透明,1不透明
//方法三:
//w.setStyleSheet("background-color:transparent;");//主界面背景变黑
w.show();
return a.exec();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>261</width>
<height>211</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background:rgb(0, 255, 0)</string>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
工程的.pro文件:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target