Skip to content

Commit 3248caf

Browse files
committed
Feat: 添加[DelSelect]实现.
1 parent f481ba1 commit 3248caf

File tree

8 files changed

+297
-0
lines changed

8 files changed

+297
-0
lines changed

DelSelect/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(DelSelect VERSION 1.0 LANGUAGES CXX)
4+
5+
set(CMAKE_AUTOMOC ON)
6+
set(CMAKE_AUTORCC ON)
7+
8+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Quick)
9+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Quick)
10+
11+
set(SOURCES main.cpp)
12+
13+
qt5_add_resources(SOURCES qml.qrc)
14+
15+
add_executable(${PROJECT_NAME} ${SOURCES} "${CMAKE_SOURCE_DIR}/common/QmlControls_Resource.rc")
16+
17+
set_target_properties(${PROJECT_NAME} PROPERTIES
18+
WIN32_EXECUTABLE TRUE
19+
RUNTIME_OUTPUT_DIRECTORY $<IF:$<BOOL:${BUILD_OUTPUT_DIRECTORY}>,${BUILD_OUTPUT_DIRECTORY},${CMAKE_RUNTIME_OUTPUT_DIRECTORY}>
20+
)
21+
22+
target_link_libraries(${PROJECT_NAME} PRIVATE
23+
Qt::Quick
24+
)

DelSelect/DelSelect.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
QT += quick
2+
3+
# You can make your code fail to compile if it uses deprecated APIs.
4+
# In order to do so, uncomment the following line.
5+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
6+
7+
SOURCES += \
8+
main.cpp
9+
10+
RESOURCES += qml.qrc
11+
12+
# Additional import path used to resolve QML modules in Qt Creator's code model
13+
QML_IMPORT_PATH =
14+
15+
# Additional import path used to resolve QML modules just for Qt Quick Designer
16+
QML_DESIGNER_IMPORT_PATH =
17+
18+
# Default rules for deployment.
19+
qnx: target.path = /tmp/$${TARGET}/bin
20+
else: unix:!android: target.path = /opt/$${TARGET}/bin
21+
!isEmpty(target.path): INSTALLS += target

DelSelect/DelSelect.qml

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import QtQuick 2.15
2+
import QtQuick.Templates 2.15 as T
3+
4+
import "qrc:/../common"
5+
import "qrc:/../DelPopup"
6+
import "qrc:/../DelScrollBar"
7+
import "qrc:/../DelToolTip"
8+
9+
T.ComboBox {
10+
id: control
11+
12+
property bool animationEnabled: true
13+
property int hoverCursorShape: Qt.PointingHandCursor
14+
property bool tooltipVisible: false
15+
property bool loading: false
16+
property int defaulPopupMaxHeight: 240
17+
property color colorText: enabled ? "#000" : Qt.rgba(0,0,0,0.45)
18+
property color colorBorder: enabled ? hovered ? "#1677ff" : Qt.rgba(0,0,0,0.25) : "transparent"
19+
property color colorBg: enabled ? "#fff" : Qt.rgba(0,0,0,0.1)
20+
21+
property int radiusBg: 6
22+
property int radiusPopupBg: 6
23+
property string contentDescription: ""
24+
25+
property Component indicatorDelegate: DelIconText {
26+
iconSize: 12
27+
iconSource: control.loading ? DelIcon.LoadingOutlined : DelIcon.DownOutlined
28+
29+
NumberAnimation on rotation {
30+
running: control.loading
31+
from: 0
32+
to: 360
33+
loops: Animation.Infinite
34+
duration: 1000
35+
}
36+
}
37+
38+
Behavior on colorText { enabled: control.animationEnabled; ColorAnimation { duration: 100 } }
39+
Behavior on colorBorder { enabled: control.animationEnabled; ColorAnimation { duration: 100 } }
40+
Behavior on colorBg { enabled: control.animationEnabled; ColorAnimation { duration: 100 } }
41+
42+
rightPadding: 8
43+
topPadding: 5
44+
bottomPadding: 5
45+
implicitWidth: implicitContentWidth + implicitIndicatorWidth + leftPadding + rightPadding
46+
implicitHeight: implicitContentHeight + topPadding + bottomPadding
47+
textRole: "label"
48+
valueRole: "value"
49+
objectName: "__DelSelect__"
50+
font {
51+
family: "微软雅黑"
52+
pixelSize: 14
53+
}
54+
delegate: T.ItemDelegate { }
55+
indicator: Loader {
56+
x: control.width - width - control.rightPadding
57+
y: control.topPadding + (control.availableHeight - height) / 2
58+
sourceComponent: indicatorDelegate
59+
}
60+
contentItem: Text {
61+
leftPadding: 8
62+
rightPadding: control.indicator.width + control.spacing
63+
text: control.displayText
64+
font: control.font
65+
color: control.colorText
66+
verticalAlignment: Text.AlignVCenter
67+
elide: Text.ElideRight
68+
}
69+
background: Rectangle {
70+
color: control.colorBg
71+
border.color: control.colorBorder
72+
border.width: control.visualFocus ? 2 : 1
73+
radius: control.radiusBg
74+
}
75+
popup: DelPopup {
76+
y: control.height + 2
77+
implicitWidth: control.width
78+
implicitHeight: implicitContentHeight + topPadding + bottomPadding
79+
leftPadding: 4
80+
rightPadding: 4
81+
topPadding: 6
82+
bottomPadding: 6
83+
enter: Transition { NumberAnimation { property: "opacity"; from: 0.0; to: 1.0; duration: control.animationEnabled ? 200 : 0 } }
84+
exit: Transition { NumberAnimation { property: "opacity"; from: 1.0; to: 0; duration: control.animationEnabled ? 200 : 0 } }
85+
contentItem: ListView {
86+
id: __popupListView
87+
clip: true
88+
implicitHeight: Math.min(control.defaulPopupMaxHeight, contentHeight)
89+
model: control.popup.visible ? control.model : null
90+
currentIndex: control.highlightedIndex
91+
boundsBehavior: Flickable.StopAtBounds
92+
delegate: T.ItemDelegate {
93+
id: __popupDelegate
94+
95+
required property var modelData
96+
required property int index
97+
property alias model: __popupDelegate.modelData
98+
99+
width: __popupListView.width
100+
height: implicitContentHeight + topPadding + bottomPadding
101+
leftPadding: 8
102+
rightPadding: 8
103+
topPadding: 4
104+
bottomPadding: 4
105+
enabled: model.enabled ?? true
106+
contentItem: Text {
107+
text: __popupDelegate.model[control.textRole]
108+
color: __popupDelegate.enabled ? "#000" : Qt.rgba(0,0,0,0.45)
109+
font {
110+
family: "微软雅黑"
111+
pixelSize: 14
112+
weight: highlighted ? Font.DemiBold : Font.Normal
113+
}
114+
elide: Text.ElideRight
115+
verticalAlignment: Text.AlignVCenter
116+
}
117+
background: Rectangle {
118+
radius: 2
119+
color: {
120+
if (__popupDelegate.enabled)
121+
return highlighted ? "#e6f4ff" : hovered ? Qt.rgba(0,0,0,0.1) : "transparent";
122+
else
123+
return "transparent";
124+
}
125+
126+
Behavior on color { enabled: control.animationEnabled; ColorAnimation { duration: 100 } }
127+
}
128+
highlighted: control.highlightedIndex === index
129+
onClicked: {
130+
control.currentIndex = index;
131+
control.activated(index);
132+
control.popup.close();
133+
}
134+
135+
HoverHandler {
136+
cursorShape: control.hoverCursorShape
137+
}
138+
139+
Loader {
140+
y: __popupDelegate.height
141+
anchors.horizontalCenter: parent.horizontalCenter
142+
active: control.tooltipVisible
143+
sourceComponent: DelToolTip {
144+
arrowVisible: false
145+
visible: __popupDelegate.hovered
146+
text: __popupDelegate.model[control.textRole]
147+
position: DelToolTip.Position_Bottom
148+
}
149+
}
150+
}
151+
T.ScrollBar.vertical: DelScrollBar { }
152+
153+
Behavior on height { enabled: control.animationEnabled; NumberAnimation { duration: 100 } }
154+
}
155+
}
156+
157+
HoverHandler {
158+
cursorShape: control.hoverCursorShape
159+
}
160+
161+
Accessible.role: Accessible.ComboBox
162+
Accessible.name: control.displayText
163+
Accessible.description: control.contentDescription
164+
}

DelSelect/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <QGuiApplication>
2+
#include <QQmlApplicationEngine>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
7+
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
8+
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
9+
#endif
10+
11+
QGuiApplication app(argc, argv);
12+
13+
QQmlApplicationEngine engine;
14+
const QUrl url(QStringLiteral("qrc:/main.qml"));
15+
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
16+
&app, [url](QObject *obj, const QUrl &objUrl) {
17+
if (!obj && url == objUrl)
18+
QCoreApplication::exit(-1);
19+
}, Qt::QueuedConnection);
20+
engine.load(url);
21+
22+
return app.exec();
23+
}

DelSelect/main.qml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import QtQuick 2.15
2+
import QtQuick.Controls 2.15
3+
import QtQuick.Window 2.15
4+
5+
Window {
6+
width: 640
7+
height: 480
8+
visible: true
9+
title: qsTr("DelegateUI-DelSelect")
10+
11+
Row {
12+
anchors.centerIn: parent
13+
spacing: 10
14+
15+
DelSelect {
16+
width: 120
17+
height: 30
18+
tooltipVisible: true
19+
model: [
20+
{ value: 'jack', label: 'Jack' },
21+
{ value: 'lucy', label: 'Lucy' },
22+
{ value: 'yiminghe', label: 'Yimingheabcdef' },
23+
{ value: 'disabled', label: 'Disabled', enabled: false },
24+
]
25+
}
26+
27+
DelSelect {
28+
width: 120
29+
height: 30
30+
enabled: false
31+
model: [
32+
{ value: 'jack', label: 'Jack' },
33+
{ value: 'lucy', label: 'Lucy' },
34+
{ value: 'yiminghe', label: 'Yiminghe' },
35+
{ value: 'disabled', label: 'Disabled', enabled: false },
36+
]
37+
}
38+
39+
DelSelect {
40+
width: 120
41+
height: 30
42+
loading: true
43+
model: [
44+
{ value: 'jack', label: 'Jack' },
45+
{ value: 'lucy', label: 'Lucy' },
46+
{ value: 'yiminghe', label: 'Yiminghe' },
47+
{ value: 'disabled', label: 'Disabled', enabled: false },
48+
]
49+
}
50+
}
51+
}

DelSelect/qml.qrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>main.qml</file>
4+
<file>DelSelect.qml</file>
5+
<file>../DelPopup/DelPopup.qml</file>
6+
<file>../DelResizeMouseArea/DelMoveMouseArea.qml</file>
7+
<file>../DelResizeMouseArea/DelResizeMouseArea.qml</file>
8+
<file>../common/DelegateUI-Icons.ttf</file>
9+
<file>../common/DelIcon.qml</file>
10+
<file>../common/DelIconText.qml</file>
11+
<file>../DelToolTip/DelToolTip.qml</file>
12+
<file>../DelScrollBar/DelScrollBar.qml</file>
13+
</qresource>
14+
</RCC>

demonstrate/DelSelect.gif

1.77 MB
Loading

demonstrate/DelSelect.png

26.9 KB
Loading

0 commit comments

Comments
 (0)