Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 样式QtComboBox,QListViewItem-设置文本填充_Css_Qt - Fatal编程技术网

Css 样式QtComboBox,QListViewItem-设置文本填充

Css 样式QtComboBox,QListViewItem-设置文本填充,css,qt,Css,Qt,信息:Qt4.8,QtDesigner 我正在努力设置组合框下拉列表中文本项的填充。我的理解是整个东西是QComboBox小部件,下拉部分是QListView。但我无法确定使用什么css选择器来更改下拉列表中文本项的样式 我希望悬停项目的背景颜色一直向左延伸,而不是留下5px的边距。文本本身排列得很好,只是左侧的白色空白让我感到不安 此外,如果我可以删除突出显示项目周围的虚线边框,我将非常高兴 我想我只是没有使用正确的项目选择器。我尝试了QListView::item和QListViewIte

信息:Qt4.8,QtDesigner

我正在努力设置组合框下拉列表中文本项的填充。我的理解是整个东西是QComboBox小部件,下拉部分是QListView。但我无法确定使用什么css选择器来更改下拉列表中文本项的样式

我希望悬停项目的背景颜色一直向左延伸,而不是留下5px的边距。文本本身排列得很好,只是左侧的白色空白让我感到不安

此外,如果我可以删除突出显示项目周围的虚线边框,我将非常高兴

我想我只是没有使用正确的项目选择器。我尝试了QListView::item和QListViewItem。下面是我应用于组合框的CSS

QComboBox{
border:                 none;
background-color:   rgb(87, 96, 134);
color:                      rgb(255, 255, 255);
font-weight:            bold;
padding:                    5px 

}

QComboBox::drop-down{
    border:                 none;
    background-color:   rgb(87, 96, 134);
    color:                      rgb(255, 255, 255);
    font-weight:            bold;
    padding:                    0px;
}

QComboBox::down-arrow{
    image:                      url(:/icons/combobox_down_arrow.png);
    padding-right:          5px;
}

QListView{
    border:                 none;
    color:                      rgb(87, 96, 134);
    background-color:   rgb(255, 255, 255);
    font-weight:            bold;
    selection-background-color: rgb(47, 175, 178);
    show-decoration-selected: 1;
    margin-left:                -10px;
    padding-left    :           15px;
}

QListView::item:hover{

    background-color:   rgb(47, 175, 178);
    border:                 none;
}

想法?

通过设置undocumented属性
outline
,可以仅使用CSS删除点边框:

QComboBox QAbstractItemView {
    outline: none;
}
QListView
也可在此选择器中使用<本例中使用了code>qabstractemview,因为它是
QListView
的基类。请注意,此属性不是针对每个项目设置的,而是针对项目的外部容器设置的

例如,还有其他方法可以使用编码删除虚线边框


看起来需要更改代码,以便在填充方面具有更好的灵活性
QComboBox
基于
QItemDelegate
使用自己的
QAbstractItemDelegate
私有实现。但是,其他控件使用
QStyledItemDelegate
(). 这就是为什么项目的CSS选择器(
QListView::item
)对
QComboBox
项目没有影响

源代码中有以下注释和解释:

请注意,此类有意不使用
QStyledItemDelegate

Vista没有为组合框使用新主题,可能存在 使用新类的其他副作用

如果上述注释没有问题,可以将
QStyledItemDelegate
设置为
QComboBox
对象:

QComboBox *combobox = new QComboBox;
combobox->setItemDelegate(new QStyledItemDelegate(combobox));
现在,不再需要属性
选择背景色
。可以自定义
::项

QComboBox QAbstractItemView::item {
    border: none;
    padding-left: 5px;
}

QComboBox QAbstractItemView::item:selected {
    background: rgb(47, 175, 178);
    padding-left: 5px;
}
styledComboBox = QtGui.QComboBox()
delegate = QtGui.QStyledItemDelegate()
styledComboBox.setItemDelegate(delegate)
请注意,要设置填充,还需要至少提供border或background属性以及填充。否则,将不考虑padding属性

使用选择器
::item:selected
,而不是
:hover
,因为也可以通过键盘选择项目


尽管有文件证明可以为
QComboBox
设置自定义视图,但它并不是那么简单<代码>QComboBox
也可以有分隔符项。如果
QComboBox
中没有分隔符项,则上述解决方案可以正常工作。要处理分隔符,项代理还应该了解它们

可以从
qcomboxdelegate
的Qt私有实现中创建子类
QStyledItemDelegate
并复制所需函数。这个解决方案不是很好。对于新的Qt版本,它可能是不可移植的。Qt5中
QComboxDelegate
的实现与Qt4不兼容。但是,Qt5可以与Qt4实现一起使用,因此该类可以取自Qt4。
QItemDelegate
基类替换为
QStyledItemDelegate

class ComboBoxDelegateStyled : public QStyledItemDelegate
{ 
    Q_OBJECT
public:
    ComboBoxDelegateStyled(QObject *parent, QComboBox *cmb) :
        QStyledItemDelegate(parent), mCombo(cmb) {}

    static bool isSeparator(const QModelIndex &index) {
        return index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator");
    }

protected:
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const {
        if (isSeparator(index)) {
            QRect rect = option.rect;
            if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option))
                if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*>(v3->widget))
                    rect.setWidth(view->viewport()->width());
            QStyleOption opt;
            opt.rect = rect;
            mCombo->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, mCombo);
        } else {
            QStyledItemDelegate::paint(painter, option, index);
        }
    }

    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const {
        if (isSeparator(index)) {
            int pm = mCombo->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, mCombo);
            return QSize(pm, pm);
        }
        return QStyledItemDelegate::sizeHint(option, index);
    }
private:
    QComboBox *mCombo;
};
现在可以使用类
ComboxStyled
代替
QComboBox
。它支持组合框分隔符绘图,还支持
::item
的CSS

定制
QComboBox
分离器的类似解决方案:


PyQt

上述行为对PyQt有效。可以使用
大纲
删除虚线边框,并设置样式化项委托以自定义CSS
::项

QComboBox QAbstractItemView::item {
    border: none;
    padding-left: 5px;
}

QComboBox QAbstractItemView::item:selected {
    background: rgb(47, 175, 178);
    padding-left: 5px;
}
styledComboBox = QtGui.QComboBox()
delegate = QtGui.QStyledItemDelegate()
styledComboBox.setItemDelegate(delegate)
在这种情况下,组合框分隔符显示为不带文本的常规项。
也可以创建自定义委托来处理分隔符。

请提供JSFIDLE。@alirezasafian JSFIDLE是否与Qt一起工作?我认为它不工作。一个FIDLE/代码片段肯定会对您有所帮助。这在QtDesigner中。您弹出一个QComboBox并应用上面包含的样式表代码。除此之外,我再也做不到任何片段或小提琴了。我的话。我很希望有一个“不傻,用这个css位代替”。明天早上我会做一次跑步,看看我能做些什么。我和PyQt一起工作,所以我不确定我能重新编码多少东西。我明天再看一遍。谢谢你这么详细的回答。@Oliver for PyQt一切都一样。我更新了答案。看起来,对于自定义分隔符,仍然需要编写自定义委托。我讨厌在项目中进行这样的调整,但这是解决办法。如果您不关心组合框分隔符,那么设置
QStyledItemDelegate
就足够了(使用
QComboBox
的自定义子类来创建许多这样的组合框是有意义的)。