字体属性不适用于QListView项子控件

时间:2015-11-29 19:38:51

标签: qt qtstylesheets

我无法使用以下代码获取字体属性以处理QListView项目子控件:

#include <QApplication>
#include <QListView>
#include <QFileSystemModel>

// Herited model with all items flagged as disabled except the first one
class Model : public QFileSystemModel
{
    public:
        Model() : QFileSystemModel() { }
        Qt::ItemFlags flags(const QModelIndex &index) const
        {
            if(index.row() > 0)
                return Qt::NoItemFlags;
            else
                return QFileSystemModel::flags(index);
        }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QListView v;
    Model* m = new Model;
    m->setRootPath("/");
    v.setModel(m);
    v.setRootIndex(m->index("/"));

    // STYLE
    QString style = "";

    style += "QListView { ";
    style += "background: lightgrey;";
    style += "}";

    style += "QListView::item { ";
    style += "font: bold italic large \"Arial\";"; // Font doesn't work
    style += "height: 40px;"; // Height works
    style += "}";

    style += "QListView::item:disabled { ";
    style += "color: blue;"; // Color works
    style += "font: bold italic large \"Times New Roman\";"; // Font doesn't work
    style += "}";

    v.setStyleSheet(style);

    v.show();

    return a.exec();
}

正如评论中所写,高度属性完全正常,颜色属性也具有伪状态。 但是font属性对于item子控件不起作用(有或没有伪状态)。 (示例:我想设置与禁用项不同的字体属性)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

   QString style = "";

   style += "QListView { ";
   style += "background: lightgrey;";
   // Font works here
   style += "font-family: Times New Roman;font-style: italic;font-size: 20pt;font-weight: bold;";
   style += "}";

   style += "QListView::item { ";
   style += "height: 40px;"; // Height works
   style += "}";

   style += "QListView::item:disabled { ";
   style += "color: blue;"; // Color works
   style += "}";