QT笔记之实现阴影窗口
2016-06-19 00:39
323 查看
方法一:
代码实现
在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明
重写void paintEvent(QPaintEvent *event);
方法二:
用代用阴影的背景图片实现
QT的窗口对于一般的窗口程序来说,已经完全够用了。但有时候我们要求界面比较精美,或者还想自定义皮肤之类的话,就需要自己定义窗口。这里介绍一种简单的自定义窗口的方法。
自定义样式可以达到很多的自定义皮肤的效果,但自定义样式有时不能指定窗口的形状,或者实现窗口的阴影效果(使用QT的QGraphicsEffect定义阴影,但运行效率较低)。这样的话可以重载窗口的paintEvent函数实现自绘制窗口。
先准备一张有窗口阴影的背景图,然后在paintEvent函数里面使用QPainterx绘制这张图。
这里将窗口类命名为GraphicDialog
示例代码如下:
class GraphicDialog :
public QDialog
{
public:
GraphicDialog(QWidget* parent = NULL, Qt::WindowFlags f = 0/* Qt::FramelessWindowHint*/);
~GraphicDialog(void);
protected:
void paintEvent(QPaintEvent *);
QPixmap background;
};
在窗口类构造函数中:
setWindowFlags(Qt::FramelessWindowHint); //无标题窗口
setAttribute(Qt::WA_TranslucentBackground);
background.load(":/Images/DialogBackground");
在paintEvent中
QPainter p(this);
p.drawPixmap(0, 0, rect().width(), rect().height(), background);
实现效果如图:
转载:http://twyok.blog.163.com/blog/static/812293032013215506418/
代码实现
在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明
重写void paintEvent(QPaintEvent *event);
void QT_Test::paintEvent(QPaintEvent *event) { QPainterPath path; path.setFillRule(Qt::WindingFill); path.addRect(10, 10, this->width()-20, this->height()-20); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.fillPath(path, QBrush(Qt::white)); QColor color(0, 0, 0, 50); for(int i=0; i<10; i++) { QPainterPath path; path.setFillRule(Qt::WindingFill); path.addRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2); color.setAlpha(150 - qSqrt(i)*50); painter.setPen(color); painter.drawPath(path); } }
方法二:
用代用阴影的背景图片实现
QT的窗口对于一般的窗口程序来说,已经完全够用了。但有时候我们要求界面比较精美,或者还想自定义皮肤之类的话,就需要自己定义窗口。这里介绍一种简单的自定义窗口的方法。
自定义样式可以达到很多的自定义皮肤的效果,但自定义样式有时不能指定窗口的形状,或者实现窗口的阴影效果(使用QT的QGraphicsEffect定义阴影,但运行效率较低)。这样的话可以重载窗口的paintEvent函数实现自绘制窗口。
先准备一张有窗口阴影的背景图,然后在paintEvent函数里面使用QPainterx绘制这张图。
这里将窗口类命名为GraphicDialog
示例代码如下:
class GraphicDialog :
public QDialog
{
public:
GraphicDialog(QWidget* parent = NULL, Qt::WindowFlags f = 0/* Qt::FramelessWindowHint*/);
~GraphicDialog(void);
protected:
void paintEvent(QPaintEvent *);
QPixmap background;
};
在窗口类构造函数中:
setWindowFlags(Qt::FramelessWindowHint); //无标题窗口
setAttribute(Qt::WA_TranslucentBackground);
background.load(":/Images/DialogBackground");
在paintEvent中
QPainter p(this);
p.drawPixmap(0, 0, rect().width(), rect().height(), background);
实现效果如图:
转载:http://twyok.blog.163.com/blog/static/812293032013215506418/
相关文章推荐
- QT笔记之自定义窗口拖拽移动
- QT浮点型输出问题
- MQTT
- 解决Qt5和VS2015中文乱码的问题
- Linux下QT及OpenCV搭建,以及交叉编译OpenCV
- QT调用dll
- Qt圆角功能和状态组合按钮,可以显示颜色或者图片
- qt对话框
- QT线程池实验研究与分析(QThread与和QThreadPool + QRunnable使用上的区别)
- 嵌入式QT SQL中文乱码问题解决
- Qt自定义窗口部件
- Qt 学习之路 :MainWindow 简介
- qt数据库多线程问题的解决(QSqlDatabase只能在创建它的线程中使用)
- qt5集成libcurl实现tftp和ftp的方法一:搭建环境
- Qt:使用Qt creator (32bit)编译Qt creator(64bit)
- qt下的跨目录多工程编译
- 《Qt》part 2 设置软件左上角图标
- Qt Creator error: LNK1123: failure during conversion to COFF: file invalid or corrupt
- QTabWidget标签实现双击关闭的方法(推荐)
- 使用PyQt4写界面后台程序方法总结