设置Qt单选按钮的样式,使其看起来类似于UISegmentedControl?

设置Qt单选按钮的样式,使其看起来类似于UISegmentedControl?,qt,radio-button,uisegmentedcontrol,Qt,Radio Button,Uisegmentedcontrol,如何将Qt单选按钮设计成IOS的UISegmentedControl?下面是该控件的一个示例: “水平单选按钮集”标题下显示了一个类似的web示例,该示例也演示了按钮的预期行为: 我希望在QButtonGroup中使用QRadioButtons,但是如果其他一些小部件和/或容器使它更容易实现这种效果,那可能没问题。如果您想使用QML实现这一点,那将非常简单。这是我在一分钟内制作的东西的快速演示。然后,您需要将它适当地连接到您使用它的目的。如果你想让它比这个更别致,我建议使用图像文件而不是矩形

如何将Qt单选按钮设计成IOS的UISegmentedControl?下面是该控件的一个示例:

“水平单选按钮集”标题下显示了一个类似的web示例,该示例也演示了按钮的预期行为:


我希望在QButtonGroup中使用QRadioButtons,但是如果其他一些小部件和/或容器使它更容易实现这种效果,那可能没问题。

如果您想使用QML实现这一点,那将非常简单。这是我在一分钟内制作的东西的快速演示。然后,您需要将它适当地连接到您使用它的目的。如果你想让它比这个更别致,我建议使用图像文件而不是矩形

import QtQuick 1.1

Rectangle {
    width: 360
    height: 360
    color: "grey"

    Item{
        id: root
        anchors.centerIn: parent

        Rectangle{
            id: button
            radius: 5
            color: "transparent"
            width: 100
            height: 50
            smooth: true

            Rectangle{
                id: leftButton
                height: parent.height
                width: 45
                color: "black"
                anchors.left: parent.left
                anchors.leftMargin: parent.radius
                smooth: true
            }
            Rectangle{
                id:leftButtonEdge
                height: parent.height
                width: 10
                radius: 5
                anchors.right: leftButton.left
                anchors.rightMargin:-width / 2
                color: "black"
                smooth: true
            }

            Rectangle{
                id: rightButton
                height: parent.height
                width: 45
                color: "white"
                anchors.right: parent.right
                anchors.rightMargin: parent.radius
                smooth: true
            }
            Rectangle{
                id:rightButtonEdge
                height: parent.height
                width: 10
                radius: 5
                anchors.left: rightButton.right
                anchors.leftMargin:-width / 2
                color: "white"
                smooth: true
            }

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    console.log("Clicked")
                    if(rightButton.color == "#000000"){
                        rightButton.color = "white"
                        rightButtonEdge.color = "white"
                        leftButton.color = "black"
                        leftButtonEdge.color = "black"
                    }else{
                        rightButton.color = "black"
                        rightButtonEdge.color = "black"
                        leftButton.color = "white"
                        leftButtonEdge.color = "white"
                    }
                }
            }
        }
    }
}

通过子类化QRadioButton并重新实现paintEvent,可以获得所需的行为。paintEvent可以使用QPushButton的Qt内置渲染,并结合使用Qt样式表来实现所需的外观(将按钮推到一起,并在末端将按钮四舍五入)

下面是一些示例代码,这些代码几乎正是所需的(并且与相应的PyQt代码非常相似/相同):


这很有趣。我最终没有走这条路,因为这意味着重新实现QPushButton的内置渲染,这已经是我所需要的样式了。@Croad不确定如果您使用QML,为什么需要重新实现QPushButton的呈现?stackunderflow:因为我希望它看起来像QPushButton。@CroadLangshan嗯,您不能实现从QPushButton派生的自定义QML元素吗,类似于所显示的内容。这不会像QPushButton那么简单,但这会给你提供与QPushButton相同的样式。
def paintEvent(self, qpaintevent):
    option = QStyleOptionButton()
    option.initFrom(self)
    if isDown():
        option.state = QStyle.State_Sunken
    else:
        option.state = QStyle.State_Raised

    if self.isDefault():
        option.features = option.features or QStyleOptionButton.DefaultButton
    option.text = self.text()
    option.icon = self.icon()

    painter = QPainter(self)
    self.style().drawControl(QStyle.CE_PushButton, option, painter, self)