如果我设置:QHeaderView::down-arrow { subcontrol-position: center left}
,向下箭头在列的左侧,如果我设置center right
,它位于列的右侧,但我想将箭头放在标题旁边(在右侧)。
问问题
3270 次
2 回答
2
你需要设置subcontrol-origin: margin | border | padding | content
;
查看下面的文档链接以了解框模型(它解释了边距矩形、边框矩形、填充矩形和内容矩形)。
http://doc.qt.io/qt-5/stylesheet-customizing.html#the-box-model
因此,请尝试添加subcontrol-origin:padding
到您的代码中,这可能会添加到您的内容旁边。
试试下面的方法:
QHeaderView::down-arrow { subcontrol-origin:padding; subcontrol-position: center right;}
于 2017-11-06T20:02:32.020 回答
2
我排序的方式是为标题创建一个 QProxyStyle,并覆盖 drawControl。我还分配了一个空图标来隐藏默认图标。
treeviewwidget.cpp
在初始化():
treeviewHeaderProxy* m_oHeaderStyle = new treeviewHeaderProxy();
treeview->header()->setStyle(m_oHeaderStyle);
treeview->header()->setDefaultAlignment(Qt::AlignCenter);
treeview->header()->setStyleSheet("QHeaderView::down-arrow { image: url(:/shared/empty); }"
"QHeaderView::up-arrow { image: url(:/shared/empty); } ");
treeviewHeaderProxy.h
:
class treeviewHeaderProxy : public QProxyStyle
{
public:
explicit treeviewHeaderProxy();
void drawControl(ControlElement oCtrElement, const QStyleOption * poStylrOptionption, QPainter * poPainter, const QWidget * poWidget = 0) const;
};
treeviewHeaderProxy.cpp
:
void treeviewHeaderProxy::drawControl(ControlElement oCtrElement, const QStyleOption *poStyleOptionption, QPainter *poPainter, const QWidget *poWidget) const
{
// Header label?
if (oCtrElement == CE_HeaderLabel) {
QStyleOptionHeader *poStyleOptionHeader = (QStyleOptionHeader *) poStyleOptionption;
QStyleOptionHeader::SortIndicator sortOption = poStyleOptionHeader->sortIndicator;
QRect oRect = poStyleOptionHeader->rect;
// Text
int iTextWidth = poStyleOptionHeader->fontMetrics.width(poStyleOptionHeader->text);
int iTextHeight = poStyleOptionHeader->fontMetrics.height();
QRect oTextRect = QRect(oRect.left() + oRect.width(), catRect.top() + (oRect.height() - iTextHeight)/2,
iTextWidth*1.2, iTextHeight);
poPainter->setPen(SUPER_DARK_GREY);
poPainter->drawText(oTextRect, poStyleOptionHeader->text); // Draw text
// Sort Indicator
QPixmap oSortPixmap;
switch(sortOption){
case QStyleOptionHeader::SortDown:
oSortPixmap = QIcon(":/shared/drop_up_grey").pixmap(10,10);
break;
case QStyleOptionHeader::SortUp:
oSortPixmap = QIcon(":/shared/drop_down_grey").pixmap(10,10);
break;
}
if(!oSortPixmap.isNull()){
QRect oSortRect = QRect(oTextRect.left() + oTextRect.width(), oRect.top() + (oRect.height() - oSortPixmap.height())/2,
oSortPixmap.width(), oSortPixmap.height());
poPainter->drawPixmap(oSortRect, oSortPixmap); // Draw sortIcon
}
return;
}
QProxyStyle::drawControl(oCtrElement, poStyleOptionption, poPainter, poWidget);
}
于 2017-11-16T18:59:34.797 回答