Reputation: 529
Is it possible to create tag/token input in QT5/pyqt5 ?
I am creating gui app where user can store image/photo and add tags. Specific tags are already in a database, user should be able to choose from existing ones (or create new ones if needed). Lets say: Image 1 Tags: (Clouds),(Tree),(mountain),road
User has chosen first 2 tags from database and added "road" which was not in database.
Currently I can create and populate QComboBox with database data which would just append to QLineEdit, later on i can process it back by formatting text. But it would be way more convenient to have something like tag/token input (exactly like in attached image):
Does QT has something like that out of the box, or should i create graphical icon for each text in custom widget?
Upvotes: 6
Views: 1888
Reputation: 1222
Yes it is possible. I have a naive implementation of such widget here. It is not finished yet, but the key functionality is implemented.
So, the implementation is based on QTextLayout
class. Сompleted tokens are kept as an class member and are painted in paintEvent
method. Сurrently editing token is painted with QTextLayout
object. A cursor is painted with QTextLayout
object too. User key presses are handled in keyPressEvent
(typing, navigation). The widget also has a completer. This is the core idea, for more details, observe the code.
There is much work to do: customizable looking, tokenizing policies, removing by click on cross, etc. But it could be used for your own purposes or as an starting point for your own widget.
The implementation of QLineEdit
was very helpful for me.
Upvotes: 2