stylesheet无效
2016-09-08 18:36
190 查看
//sheetstyle file, style.css QWidget#setting_widget { background-color: white; } //child widget class CSubWidget : public QWidget { Q_OBJECT public: explicit CSettingWidget(QWidget *parent = 0); }; CSettingWidget::CSettingWidget(QWidget *parent) : QWidget(parent) { setWindowFlags(Qt::WindowStaysOnTopHint); setObjectName("subwidget"); } //main widget class MainWindow : public QWidget { Q_OBJECT public: ~MainWindow(); explicit MainWindow(QWidget *parent = 0); private: CSubWidget *m_pSubWidget; }; MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { QFile file(":/style.css"); file.open(QIODevice::ReadOnly); qApp->setStyleSheet(file.readAll()); m_pSubWidget = new CSubWidget(this); }
以上会发现, CSubWidget 设置的窗体样式是无效的,
查看文档会发现QWidget Supports only the background, background-clip and background-origin properties. If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
void CustomWidget::paintEvent(QPaintEvent *) { QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); }
The above code is a no-operation if there is no stylesheet set. Warning: Make sure you define the Q_OBJECT macro for your custom widget.
为CSubWidget 加上paintEvent就可以解决
相关文章推荐
- styleSheet样式无效
- 网页::::无法访问请求的页面,因为该页的相关配置数据无效。
- ie7下子元素设置z-index无效的解决办法
- 报错ora-04098:触发器无效且未通过重新验证
- oracle“ORA-00904”错误:标识符无效几种可能情况
- 彻底解决“安卓IMEI提示无效”的方案
- 【Android】使用requestWindowFeature(Window.FEATURE_NO_TITLE)方法去除标题无效解决办法
- 关于Jcrop在IE下(含IE10)无效的解决办法
- thinkphp中session设置session周期无效
- js使用return在forEach中跳出循环无效
- 指定的元数据路径无效。有效路径必须为现有目录、扩展名为“.csdl”、“.ssdl”或“.msl”的现有文件或标识嵌入资源的 URI。
- AngularJS ng-disabled在a内无效
- Spring配置多数据源在配置事务后无效完美解决方案
- 关于"回发或回调参数无效"
- 在Asp.Net的Global.asax中Application_Error跳转到自定义错误页无效的解决办法
- checked="checked"无效,radio未选中问题排查
- win8 去除快捷图标箭头后计算机右键管理及固定到任务栏无效的解决办法
- 报错:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- img标签 使用 margin : 0 auto 无效
- Microsoft Help查看器1.0 为帮助内容存储区指定的位置无效或者您无权访错误 的解决办法