Qt 字体属性在QListView项子控件上不起作用

Qt 字体属性在QListView项子控件上不起作用,qt,qtstylesheets,Qt,Qtstylesheets,我无法使用以下代码使font属性在QListView项子控件上工作: #include <QApplication> #include <QListView> #include <QFileSystemModel> // Herited model with all items flagged as disabled except the first one class Model : public QFileSystemModel { public

我无法使用以下代码使font属性在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();
}
#包括
#包括
#包括
//Herited模型,除第一个项目外,所有项目都标记为禁用
类模型:公共QFileSystemModel
{
公众:
模型():QFileSystemModel(){}
Qt::ItemFlags标志(常量QModelIndex&index)常量
{
如果(index.row()>0)
返回Qt::noitemsflags;
其他的
返回QFileSystemModel::标志(索引);
}
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QListView v;
型号*m=新型号;
m->setRootPath(“/”);
v、 setModel(m);
v、 setRootIndex(m->index(“/”);
//风格
QString style=“”;
样式+=“QListView{”;
样式+=“背景:浅灰色;”;
样式+=“}”;
样式+=“QListView::项{”;
style+=“font:bold斜体大号\“Arial\”;“;//字体不起作用
style+=“高度:40px;”;//高度有效
样式+=“}”;
style+=“QListView::item:已禁用{”;
style+=“color:blue;”;//颜色有效
style+=“font:bold斜体大号\“Times New Roman\”;“;//字体不起作用
样式+=“}”;
v、 设置样式表(样式);
v、 show();
返回a.exec();
}
如注释中所述,height属性工作得非常好,color属性也具有伪状态。 但是font属性(有或没有伪状态)不适用于item子控件。 (示例:我想设置不同于禁用项目的字体属性)

有什么想法吗?

试试这个:

   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 += "}";

嗨,谢谢你的回答。但是如果我想为禁用的项目设置不同的字体属性,它仍然不起作用。我没有找到如何使用样式表的答案。看起来像从ListView继承的默认字体,无法为子级指定它。我可能错了。