QT中QMessageBox的一些方式

最近由于项目中使用到了QMessageBox显示一些警告信息,经过查阅网络资料,针对这个控件定制样式,主要有两个途径:

1、设置QSS

        因为QMessageBox大概是由一个QIcon,一个QLabel,还有相应的pushButton控件组成。因此,在qss中添加对于QIcon、QLabel、QPushButton的样式设置即可。如:

QLabel {
    font: 18pt;
    color: rgb(0, 0, 127);
    border: 2px solid green;
    border-radius: 4px;
     padding: 2px;
     background-image: url(images/background.png);
}
QLabel:hover{
    font: 18pt;
    color: rgb(0, 127, 127);
    border: 2px solid green;
    border-radius: 4px;
    padding: 2px;
     background-image:url();
}

以上对QMessageBox中的文本同样会起到作用。

2、对QMessageBox进行重写。

当QMessageBox中的元素不能够满足需求时,可以有两种方法。

a)声明一个类,从QMessageBox继承而来。然后在其中添加绘制等操作。详见网上另一篇博客:

https://www.xuebuyuan.com/896335.html

b)自定义一个类,其中包含对QMessageBox的更丰富操作

https://blog.csdn.net/liang19890820/article/details/50586031

猜你喜欢

转载自www.cnblogs.com/yy-86/p/9785348.html