Skip to content

Commit 7a0c67c

Browse files
committed
[Qt] Add Selection widget.
1 parent e9ede3d commit 7a0c67c

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

changes/3914.feature.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The Selection widget is now supported in the Qt backend.

docs/en/reference/data/apis_by_platform.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ General widgets:
131131
- web
132132
unsupported:
133133
- textual
134-
- qt
135134

136135
Slider:
137136
description: A widget for selecting a value within a range. The range is shown as a horizontal line, and the selected value is shown as a draggable marker.
1.43 KB
Loading

qt/src/toga_qt/factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .widgets.button import Button
1717
from .widgets.imageview import ImageView
1818
from .widgets.label import Label
19+
from .widgets.selection import Selection
1920
from .widgets.switch import Switch
2021
from .widgets.textinput import TextInput
2122
from .window import MainWindow, Window
@@ -45,6 +46,7 @@
4546
"Container",
4647
"Box",
4748
"Label",
49+
"Selection",
4850
"Switch",
4951
"TextInput",
5052
"ImageView",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from contextlib import contextmanager
2+
3+
from PySide6.QtWidgets import QComboBox
4+
from travertino.size import at_least
5+
6+
from .base import Widget
7+
8+
9+
class Selection(Widget):
10+
def create(self):
11+
self.native = QComboBox()
12+
self.native.setSizeAdjustPolicy(QComboBox.SizeAdjustPolicy.AdjustToContents)
13+
self.native.currentTextChanged.connect(self.qt_on_current_text_changed)
14+
self._send_notifications = True
15+
16+
@contextmanager
17+
def suspend_notifications(self):
18+
self._send_notifications = False
19+
yield
20+
self._send_notifications = True
21+
22+
def qt_on_current_text_changed(self, text):
23+
if self._send_notifications:
24+
self.interface.on_change()
25+
26+
def clear(self):
27+
self.native.clear()
28+
29+
def insert(self, index, item):
30+
self.native.insertItem(index, self.interface._title_for_item(item))
31+
32+
def change(self, item):
33+
index = self.interface._items.index(item)
34+
with self.suspend_notifications():
35+
self.native.setItemText(index, self.interface._title_for_item(item))
36+
self.interface.refresh()
37+
38+
def remove(self, index, item):
39+
current_index = self.native.currentIndex()
40+
with self.suspend_notifications():
41+
self.native.removeItem(index)
42+
if index == current_index:
43+
if self.native.count() > 0:
44+
self.native.setCurrentIndex(0)
45+
else:
46+
self.interface.on_change()
47+
48+
def select_item(self, index, item):
49+
self.native.setCurrentIndex(index)
50+
51+
def get_selected_index(self):
52+
index = self.native.currentIndex()
53+
return None if index == -1 else index
54+
55+
def rehint(self):
56+
content_size = self.native.sizeHint()
57+
self.interface.intrinsic.width = at_least(content_size.width())
58+
self.interface.intrinsic.height = content_size.height()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from PySide6.QtWidgets import QComboBox
2+
3+
from .base import SimpleProbe
4+
5+
6+
class SelectionProbe(SimpleProbe):
7+
native_class = QComboBox
8+
9+
def assert_resizes_on_content_change(self):
10+
pass
11+
12+
@property
13+
def titles(self):
14+
titles = [self.native.itemText(index) for index in range(self.native.count())]
15+
return titles
16+
17+
@property
18+
def selected_title(self):
19+
if self.native.currentIndex() < 0:
20+
return None
21+
else:
22+
return self.native.currentText()
23+
24+
async def select_item(self):
25+
self.native.setCurrentIndex(1)

0 commit comments

Comments
 (0)