【问题标题】:Problems in setting styles via setStyleSheet() in Qt?在 Qt 中通过 setStyleSheet() 设置样式的问题?
【发布时间】:2014-07-16 08:07:33
【问题描述】:

我尝试做的是使用setStyleSheet() 封装自定义组合框。如何将此处描述的所有样式customizing-qcombobox 应用到我的QComboBox 对象?当我试图实现我的目标时,我遇到了一些问题。

我知道我可以在必要时使用以下附加样式:ui.comboxFps->setStyleSheet(ui.comboxFps->styleSheet().append(QString("some styles you need to set")));

我遇到了一个问题,描述如下。ui.comboxFps->setStyleSheet(ui.comboxFps->styleSheet().append(QString("color:red;"))); 之类的东西按预期运行。

但是:具有QComboBox{ui.comboxFps->setStyleSheet(ui.comboxFps->styleSheet().append(QString("QComboBox{color:red;} QComboBox::down-arrow:on { left: 1px;}")));不起作用。

我用另一种方式尝试过。

QFile styleFile( ":Resources/comboxStyle.qss" ); styleFile.open( QFile::ReadOnly ); QString style( styleFile.readAll() ); ui.comboxFps->setStyleSheet( style );

comboxStyle.qss 中类似于 customizing-qcombobox。它也没有工作。

【问题讨论】:

  • 你到底想做什么?为什么要将样式表附加到已经存在的样式表?顺便说一句,QComboBox 没有 QColor 属性。我不知道这是否是问题中的错字,或者是否在您的实际代码中。
  • @thuga Qcolor 这是我的问题中的一个错字,这里的代码是用于表达的sn-ps。对不起,附加不是这里的关键。我到底想要什么这样做是设置 QComboBox 的下拉和向下箭头样式,如此处所述customizing-qcombobox
  • @thuga 实际上当我只是 setStyleSheet 喜欢 'ui.comboxFps->setStyleSheet((QString("QComboBox::down-arrow:on{Qcolor:red;} QComboBox ::down-arrow {image: url(……);}")));',它也不起作用。我只想知道如何将此处描述的样式customizing-qcombobox 应用到我的 QComboBox 对象。
  • 这行得通吗:ui.comboxFps->setStyleSheet("QComboBox{color: red; background-color: blue;}");?

标签: qt qtstylesheets


【解决方案1】:

我发现了我遇到的问题。 (假设 btnTest 是一个QPushButton 对象)

当您通过 Qt-Designer 的 styleSheet 属性设置样式时,QWidget 的 styleSheet() 返回一个 QString 类似

color: gray;border-width:1px;border-style:solid;border-radius:3px;

没有前缀和后缀

QPushButton{……}`.Then `btnTest->setStyleSheet(btnTest->styleSheet().append(QString(QPushButton{border:url(……correct image path);})))

效果不好,正确的是

btnTest->setStyleSheet( QString("{") + btnTest->styleSheet()+ QString("}") + QString("QPushButton{border:url(……correct image path);}") )

请关注QString("{")QString("}")

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2012-06-17
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    相关资源
    最近更新 更多