欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt5中QML自定義環(huán)形菜單/環(huán)形選擇框的實現(xiàn)

 更新時間:2022年03月14日 15:58:32   作者:龔建波  
本文主要介紹了Qt5中QML自定義環(huán)形菜單/環(huán)形選擇框的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Qt5 中本身提供了扇形菜單 PieMenu,屬于 QtQuick.Extras 模塊,這個模塊是拓展自 QtQuick.Control1 的,QtQuick.Control1 在 Qt5 高版本被廢棄,并在 Qt6 移除。

不過我們也可以用 QtQuick.Control2 的組件自定義樣式來實現(xiàn)環(huán)形或扇形的菜單和選擇框。主要思路就是使用 PathView 來替換默認的 ListView,再改下彈框的背景樣式。

ItemDelegate 需要設置給 ComboBox 或者 Menu,而不是 View。最好用 Button 的相關(guān)類型(默認是 ItemDelegate 類型),因為組件默認這些小部件是 Button 類型,內(nèi)部 cast 成按鈕來處理的。而且用按鈕就不用自己處理下拉框 currentIndex,內(nèi)部會自己處理,這也避免了我們在這個 delegate 對 currentIndex 賦值后導致其屬性綁定失效的問題。

QQuickAction *QQuickMenu::actionAt(int index) const
{
    Q_D(const QQuickMenu);
    QQuickAbstractButton *item = qobject_cast<QQuickAbstractButton *>(d->itemAt(index));
    if (!item)
        return nullptr;
 
    return item->action();
}

自定義的時候遇到一點狀況,就是 PathView 替代 ListView 作為 Menu 的 contentItem 后,Menu 的 contentData 和 contentModel 始終會多一個表示高亮的 Item,這樣環(huán)形路徑就有個缺口,目前我只能將顯示的 Item 個數(shù)減去一個來使顯示效果正常。

    contentItem: PathView {
        model: control.contentModel
        //把PathView放Menu,會有一個高亮Item被放到contentModel,減去
        pathItemCount: control.count > 0 ? control.count - 1 : 0
        //... ...
    }

Demo 鏈接:https://github.com/gongjianbo/MyTestCode2021/tree/master/Qml/TestQml_20220313_PathView

 

 主要代碼:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
 
Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("PathView")
 
    Row {
        anchors.centerIn: parent
        spacing: 20
 
        MyComboBox {
            model: 10
        }
 
        Button {
            width: 60
            height: 30
            text: "menu"
            background: Rectangle {
                radius: 15
                color: "red"
                border.color: "black"
            }
            onClicked: {
                menu.popup()
            }
 
            MyMenu {
                id: menu
                anchors.centerIn: parent
                Action { text: "1" }
                Action { text: "2" }
                Action { text: "3" }
                Action { text: "4" }
                Action { text: "5" }
                Action { text: "6" }
                Action { text: "7" }
                Action { text: "8" }
                Action { text: "9" }
                Action { text: "10" }
            }
        }
    }
}
import QtQuick 2.12
import QtQuick.Controls 2.12
 
//環(huán)形選擇框
//龔建波 2022-03-13
//note:彈框為pop會被限制在window內(nèi)
ComboBox {
    id: control
 
    implicitWidth: 30
    implicitHeight: 30
    opacity: 0.9999
 
    delegate: ItemDelegate {
        width: 30
        height: width
        padding: 0
        background: Rectangle {
            radius: width / 2
            color: "green"
            border.color: "black"
        }
        contentItem: Text {
            text: modelData
            padding: 0
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }
    }
    contentItem: Text {
        text: control.displayText
        padding: 0
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
    }
    indicator: null
    background: Rectangle {
        radius: 15
        color: "green"
        border.color: "black"
    }
    popup: Popup {
        id: pop
        width: 200
        height: width
        anchors.centerIn: parent
        margins: 0
        padding: 0
        //pathview環(huán)形的角度范圍和延申半徑
        property int angle: 1
        property int spread: 1
        //pop彈出和隱藏時的過渡動畫
        enter: Transition {
            ParallelAnimation {
                NumberAnimation { property: "angle"; from: 1; to: 360; duration: 500 }
                NumberAnimation { property: "spread"; from: 1; to: 100; duration: 500 }
            }
        }
        exit: Transition {
            ParallelAnimation {
                NumberAnimation { property: "angle"; from: 360; to: 1; duration: 500 }
                NumberAnimation { property: "spread"; from: 100; to: 1; duration: 500 }
            }
        }
        background: Item { }
        contentItem: PathView {
            model: control.popup.visible ? control.delegateModel : null
            //currentIndex: control.highlightedIndex
            //highlightRangeMode: PathView.NoHighlightRange
            interactive: false
            path: Path {
                //一個圓環(huán)路徑
                PathAngleArc {
                    centerX: 100; centerY: 100
                    radiusX: pop.spread; radiusY: pop.spread
                    moveToStart: true
                    startAngle: 0
                    sweepAngle: pop.angle
                }
            }
        }
    }
}
import QtQuick 2.12
import QtQuick.Controls 2.12
 
//環(huán)形菜單
//龔建波 2022-03-13
//note:彈框為pop會被限制在window內(nèi)
Menu {
    id: control
 
    implicitWidth: 250
    implicitHeight: 250
    margins: 0
    padding: 0
 
    //pathview環(huán)形的角度范圍和延申半徑
    property int angle: 1
    property int spread: 1
    //pop彈出和隱藏時的過渡動畫
    enter: Transition {
        ParallelAnimation {
            NumberAnimation { property: "angle"; from: 1; to: 360; duration: 500 }
            NumberAnimation { property: "spread"; from: 1; to: 100; duration: 500 }
        }
    }
    exit: Transition {
        ParallelAnimation {
            NumberAnimation { property: "angle"; from: 360; to: 1; duration: 500 }
            NumberAnimation { property: "spread"; from: 100; to: 1; duration: 500 }
        }
    }
    delegate: MenuItem {
        id: item
        width: 30
        height: width
        padding: 0
        spacing: 0
        indicator: null
        arrow: null
        background: Rectangle {
            radius: width / 2
            color: "red"
            border.color: "black"
        }
        contentItem: Text {
            text: item.text
            padding: 0
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }
    }
    contentItem: PathView {
        implicitWidth: 250
        implicitHeight: 250
        model: control.contentModel
        //把PathView放Menu,會有一個高亮Item被放到contentModel,減去
        pathItemCount: control.count > 0 ? control.count - 1 : 0
        //currentIndex: control.currentIndex
        //highlightRangeMode: PathView.NoHighlightRange
        interactive: false
        path: Path {
            //一個圓環(huán)路徑
            PathAngleArc {
                centerX: 125; centerY: 125
                radiusX: control.spread; radiusY: control.spread
                moveToStart: true
                startAngle: 0
                sweepAngle: control.angle
            }
        }
    }
    background: Item { }
}

到此這篇關(guān)于Qt5中QML自定義環(huán)形菜單/環(huán)形選擇框的實現(xiàn)的文章就介紹到這了,更多相關(guān)Qt5 QML環(huán)形菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++使用動態(tài)內(nèi)存分配的原因解說

    C++使用動態(tài)內(nèi)存分配的原因解說

    這篇文章主要介紹了C++使用動態(tài)內(nèi)存分配的原因解說,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • C語言棧順序結(jié)構(gòu)實現(xiàn)代碼

    C語言棧順序結(jié)構(gòu)實現(xiàn)代碼

    一個能夠自動擴容的順序結(jié)構(gòu)的棧 ArrStack 實例 (GCC編譯),有需要的朋友可以參考一下
    2013-10-10
  • c語言的程序環(huán)境與預處理詳解

    c語言的程序環(huán)境與預處理詳解

    大家好,本篇文章主要講的是c語言的程序環(huán)境與預處理詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • C++輸入流和輸出流 超級詳細

    C++輸入流和輸出流 超級詳細

    C++ 的開發(fā)者認為數(shù)據(jù)輸入和輸出的過程也是數(shù)據(jù)傳輸?shù)倪^程,數(shù)據(jù)像水一樣從一個地方流動到另一個地方,所以 C++ 中將此過程稱為“流”,實現(xiàn)此過程的類稱為“流類”。下面小編將詳細介紹這個話題,需要的朋友可以參考一下
    2021-09-09
  • C語言實現(xiàn)修改文本文件中特定行的實現(xiàn)代碼

    C語言實現(xiàn)修改文本文件中特定行的實現(xiàn)代碼

    最近由于項目需要實現(xiàn)修改文件的功能,所以,博主認真查閱了一些資料,但是,很遺憾,并沒有太多的收獲
    2013-06-06
  • C語言實現(xiàn)控制臺掃雷小游戲

    C語言實現(xiàn)控制臺掃雷小游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)控制臺掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • C語言超細致講解循環(huán)語句

    C語言超細致講解循環(huán)語句

    我們說到當滿足特定條件時,就會執(zhí)行if語句或者switch語句后面的語句,否則不執(zhí)行,但是這只能執(zhí)行一次,在日常生活中,有些事情是需要重復去做的,C語句就為此引入了循環(huán)語句。所以今天繼續(xù)為大家分享C語言循環(huán)家族
    2022-05-05
  • C++函數(shù)模板的使用詳解

    C++函數(shù)模板的使用詳解

    大家好,本篇文章主要講的是C++函數(shù)模板的使用詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • zlib庫壓縮和解壓字符串STL string的實例詳解

    zlib庫壓縮和解壓字符串STL string的實例詳解

    這篇文章主要介紹了zlib庫壓縮和解壓字符串STL string的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • C++中如何將數(shù)據(jù)保存為CSV文件

    C++中如何將數(shù)據(jù)保存為CSV文件

    這篇文章主要介紹了C++中如何將數(shù)據(jù)保存為CSV文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評論