ComboBox下拉框样式,带设置圆角外框和选中内容

本文介绍如何为ComboBox组件定制样式,实现圆角外框及个性化选中内容显示,适用于前端界面设计,提升用户体验。
    <!-- 下拉列表中ToggleButton -->
    <Style TargetType="ToggleButton" x:Key="stlToggleButton">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="ClickMode" Value="Press"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type ToggleButton}">
                    <Grid>
                        <TextBlock></TextBlock>
                        <Border Width="22.5"  HorizontalAlignment="Right"  BorderBrush="White" >
                            <Border.Background>
                                <SolidColorBrush Color="#2ED1D0"/>
                            </Border.Background>
                            <Path Data="M0,0L3.5,4 7,0z" Fill="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!-- 短下拉框样式 -->
    <Style TargetType="{x:Type ComboBox}" x:Key="ShortComboxStyle">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="Margin" Value="2,5,2,5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Border BorderBrush="{StaticResource Primary}" BorderThickness="2" CornerRadius="3" Background="White">
                        <Grid>
                            <!-- 下拉箭头 -->
                            <ToggleButton Style="{StaticResource stlToggleButton}" 
                                          IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"/>
                            <!-- 项内容 -->
                            <ContentPresenter Margin="0,0,22,1" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <!-- 下拉显示面板HorizontalOffset:设置下拉面板的相对位置 -->
                            <Popup HorizontalOffset="-1" 
                                   VerticalOffset="5"
                                   Width="{TemplateBinding ActualWidth}" 
                                   IsOpen="{TemplateBinding IsDropDownOpen}" 
                                   AllowsTransparency="True"
                                   Focusable="False" 
                                   PopupAnimation="Slide" >
                                <Grid SnapsToDevicePixels="True" HorizontalAlignment="Stretch">
                                    <!-- 圆角背景 -->
                                    <Border x:Name="Bd"
                                            BorderThickness="1" 
                                            CornerRadius="5"
                                            BorderBrush="{StaticResource Kerley}" 
                                            Background="White" 
                                            HorizontalAlignment="Stretch">
                                        <!-- 面板内容 -->
                                        <StackPanel Margin="0,10" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" HorizontalAlignment="Stretch"/>
                                    </Border>
                                </Grid>
                            </Popup>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Grid>
                                    <Border x:Name="back" BorderThickness="0" BorderBrush="Transparent" Background="Transparent"/>
                                    <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center" SnapsToDevicePixels="True"/>
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Background" TargetName="back" Value="{StaticResource Primary}"/>
                                    </Trigger>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" TargetName="back" Value="{StaticResource Primary}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

 

在C/C++Qt 5.15.2环境下,可使用`setStyleSheet`方法设置`QComboBox`下拉框滑块样式。下面详细介绍设置步骤与示例代码。 ### 步骤 1. **创建`QComboBox`对象**:在代码里创建一个`QComboBox`对象。 2. **使用`setStyleSheet`方法设置样式**:借助`setStyleSheet`方法为`QComboBox`及其下拉框滑块设定样式。 ### 示例代码 ```cpp #include <QApplication> #include <QComboBox> #include <QVBoxLayout> #include <QWidget> int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建主窗口 QWidget window; // 创建QComboBox对象 QComboBox comboBox; comboBox.addItem("Item 1"); comboBox.addItem("Item 2"); comboBox.addItem("Item 3"); // 设置QComboBox下拉框滑块样式 comboBox.setStyleSheet( // 设置QComboBox样式 "QComboBox {" " border: 1px solid gray;" " border-radius: 3px;" " padding: 1px 18px 1px 3px;" " min-width: 6em;" "}" // 设置QComboBox下拉箭头的样式 "QComboBox::drop-down {" " subcontrol-origin: padding;" " subcontrol-position: top right;" " width: 15px;" " border-left-width: 1px;" " border-left-color: darkgray;" " border-left-style: solid;" " border-top-right-radius: 3px;" " border-bottom-right-radius: 3px;" "}" // 设置QComboBox下拉框样式 "QComboBox QAbstractItemView {" " border: 2px solid darkgray;" " selection-background-color: lightgray;" "}" // 设置QComboBox下拉框滚动条的样式 "QComboBox QScrollBar:vertical {" " border: 1px solid #999999;" " background: white;" " width: 15px;" " margin: 15px 0 15px 0;" "}" // 设置QComboBox下拉框滚动条滑块的样式 "QComboBox QScrollBar::handle:vertical {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:0," " stop: 0 #bbbbbb, stop: 0.5 #aaaaaa, stop: 1 #bbbbbb);" " min-height: 20px;" "}" // 设置QComboBox下拉框滚动条向上箭头的样式 "QComboBox QScrollBar::add-line:vertical {" " border: 1px solid gray;" " background: #cccccc;" " height: 15px;" " subcontrol-position: bottom;" " subcontrol-origin: margin;" "}" // 设置QComboBox下拉框滚动条向下箭头的样式 "QComboBox QScrollBar::sub-line:vertical {" " border: 1px solid gray;" " background: #cccccc;" " height: 15px;" " subcontrol-position: top;" " subcontrol-origin: margin;" "}" ); // 创建布局并添加QComboBox QVBoxLayout layout; layout.addWidget(&comboBox); window.setLayout(&layout); // 显示窗口 window.show(); return a.exec(); } ``` ### 代码解释 - **`QComboBox`样式**:设定`QComboBox`的边框圆角、内边距最小宽度。 - **`QComboBox::drop-down`样式**:设置下拉箭头的位置、宽度与左边框样式。 - **`QComboBox QAbstractItemView`样式**:设置下拉框边框选中项的背景颜色。 - **`QComboBox QScrollBar:vertical`样式**:设置垂直滚动条的边框、背景、宽度边距。 - **`QComboBox QScrollBar::handle:vertical`样式**:设置垂直滚动条滑块的背景颜色与最小高度。 - **`QComboBox QScrollBar::add-line:vertical`样式**:设置垂直滚动条向下箭头的样式。 - **`QComboBox QScrollBar::sub-line:vertical`样式**:设置垂直滚动条向上箭头的样式。 ### 编译与运行 把上述代码保存为`.cpp`文件,然后使用Qt Creator或者`qmake``make`进行编译与运行。 ### 注意事项 - 样式表的语法要正确,不然样式可能无法生效。 - 可依据实际需求调整样式表中的颜色、尺寸等参数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值