【问题标题】:CSS to access QHeaderView item columns to set background-image, or set large icon to QHeaderViewCSS 访问 QHeaderView 项目列以设置背景图像,或将大图标设置为 QHeaderView
【发布时间】:2015-06-04 11:49:13
【问题描述】:

我需要 CSS 访问 QHeaderView 的所有列 将背景图像设置为我可以通过以下方式访问的第一列:

QHeaderView::section:horizontal:first{

    background-image: url(:/Icons/icon_1.png);
    background-position:left;
    background-repeat:no-repeat;
    border: 1px solid #4B4B4B;
}

到我可以通过以下方式访问的最后一列:

QHeaderView::section:horizontal:last{

    background-image: url(:/Icons/icon_5.png);
    background-position:left;
    background-repeat:no-repeat;
    border: 1px solid #4B4B4B;
}

知道如何获得第二个第三个...等列吗?设置背景图像或将大图标设置为 QHeaderView

【问题讨论】:

    标签: css qt qt5 qheaderview


    【解决方案1】:

    我想要的只是为 QHeaderView 设置大图标,但使用 css 我只能访问第一个和最后一个项目,我找到了一个通过 QProxyStyle 类更改 QHeaderView 项目图标大小的解决方案,我将在这里分享我的解决方案:

    你需要做的就是继承QProxyStyle类到你自己的类覆盖drawControl方法和setStyle到你的TreeView

       HeaderStyle* style= new HeaderStyle();
        treeVew->header()->setStyle(style);
    

    你可以使用

     Model->horizontalHeaderItem(0)->setIcon(QIcon(":/Icons/icon_1.png"));
    

    设置你的图标

    class HeaderStyle : public QProxyStyle
    {
    public:
        void drawControl(ControlElement element,const QStyleOption * option, QPainter * painter, const QWidget * widget = 0) const
        {
            if (element == CE_HeaderLabel) {
    
                QStyleOptionHeader *op = (QStyleOptionHeader *) option;
    
                QIcon icon = qvariant_cast<QIcon>(op->icon);
                QSize iconsize(120,120);
    
                QRect iconRect = op->rect;
                QPixmap pixmap = icon.pixmap(iconsize.width(),iconsize.height());
    
    
                painter->drawPixmap(QPoint(iconRect.left() + 5, iconRect.top()+ 5), pixmap);
    
                return;
            }
            QProxyStyle::drawControl(element, option, painter, widget);
        }
    
    };
    

    【讨论】:

      【解决方案2】:

      我只是在我的应用程序中使用:

      QHeaderView::section {
          /* MY CSS STUFF HERE */
      }
      

      这些都是样式

      【讨论】:

      • 我不需要为所有这些设置样式我需要为每一列设置特定的背景图像,这就是这里的重点......
      • 哦,好吧,应该提到我看看
      猜你喜欢
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 2013-08-02
      • 2021-12-27
      • 2015-01-24
      相关资源
      最近更新 更多