QT用setStyleShee给边框加发光
时间: 2023-07-04 19:04:44 浏览: 132
可以使用QSS的box-shadow属性给边框加发光效果。
例如,将一个QPushButton的边框设置为红色,并添加一个发光效果:
```cpp
QPushButton {
border: 2px solid red;
border-radius: 5px;
padding: 5px 10px;
color: white;
background-color: red;
font-weight: bold;
font-size: 16px;
box-shadow: 0 0 10px red;
}
```
其中,`box-shadow`的格式为`x-offset y-offset blur color`,分别表示阴影的水平偏移、垂直偏移、模糊半径和颜色。这里设置了一个红色的发光效果,偏移量为0,模糊半径为10px。
在实际开发中,可以根据需要调整参数值以达到想要的效果。
相关问题
QT用setStyleShee给边框加内发光
在Qt中,可以使用`setStyleSheet`函数为控件添加CSS样式来设置边框和内发光效果。以下是一个示例代码:
```cpp
QLineEdit* lineEdit = new QLineEdit(this);
lineEdit->setStyleSheet("border: 2px solid gray; border-radius: 10px; padding: 2px 4px;"
"background-color: #f0f0f0; /*内发光*/"
"box-shadow: 0px 0px 5px rgba(255, 255, 255, 200);");
```
在上面的代码中,我们为QLineEdit控件设置了边框样式,同时也添加了内发光效果。主要是通过`background-color`属性来设置背景颜色,并通过`box-shadow`属性来实现内发光效果。
`box-shadow`属性的语法如下:
```css
box-shadow: h-shadow v-shadow blur spread color;
```
其中,`h-shadow`是水平阴影的位置,`v-shadow`是垂直阴影的位置,`blur`是模糊距离,`spread`是阴影的扩散程度,`color`是阴影的颜色。可以根据实际需要进行调整。
QT用setStyleShee给背景加渐变色
可以使用CSS中的线性渐变(linear gradient)来给QT的背景添加渐变色。具体步骤如下:
1. 设置QT窗口的背景透明,这可以通过设置窗口的属性为Qt::WA_TranslucentBackground来实现。
2. 使用setStyleSheet函数设置窗口的CSS样式,具体代码如下:
```
setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:1,stop:0 #00bfff, stop:1 #1e90ff);");
```
其中,qlineargradient函数表示使用线性渐变,(x1,y1)表示渐变起点坐标,(x2,y2)表示渐变终点坐标,stop表示渐变的颜色位置,#00bfff和#1e90ff是两个不同的颜色值,可以根据需要自行修改。
完整的代码实现如下:
```
#include <QApplication>
#include <QWidget>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.setWindowFlags(Qt::FramelessWindowHint);
w.setAttribute(Qt::WA_TranslucentBackground, true);
w.setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:1,stop:0 #00bfff, stop:1 #1e90ff);");
QPushButton btn("Hello, World!", &w);
btn.setGeometry(50, 50, 200, 50);
w.show();
return a.exec();
}
```
这样就可以给QT的窗口背景添加渐变色了。