[Interest] Writing custom Qt Widgets is overly complicated

Tony Rietwyk tony at rightsoft.com.au
Wed Apr 29 20:32:54 CEST 2020


On 28 April 2020 at 12:05:54, Jonathan Purol (contact at folling.de 
<mailto:contact at folling.de>) wrote:
>>
>> Yesterday however I put my hands on trying to implement my first 
>> custom widget.
>>
>> The item in question is what I would describe as a "Tag Textfield".
>>
> […]
>
>> The last approach seemed rather clever, and it worked pretty well. I 
>> could put widgets into the textfield and align them at the left. 
>> First I tried to do this manually, then I opted for an HBoxLayout.
>>
> Sorry, I don’t really have a solution for Qt Widgets. But is 
> QHBoxLayout really the best pick there? I would go for a “flow” 
> layout, like the QML ‘Flow’ positioner (but you’d have to make it 
> yourself). In this way your tags can wrap correctly according to 
> widget’s width.
>
> In fact this is what I did in my QML Tag Edit widget 
> (https://github.com/fferri/QtQuick-TagsEditItem) maybe this is the 
> shoddy one you said you already found :-D
>
> I didn’t put too much effort in it, and it has some minor issues, as 
> it can be seen from the screenshot. But the implementation is very 
> simple, so one can easily pick up from that and extend it. Maybe also 
> fixing those positioning issues is a one-liner fix (if anyone wants to 
> contribute a fix… PRs are welcome).
>
> Maybe you can copy and adapt the implementation to Qt Widgets.
>
> Cheers,
>
> Federico Ferri
>
Hi Jonathon,

For some reason, I didn't receive your original post.

As a starting point, I agree with Federico and suggest using the flow 
layout given in the Qt Layout examples 
qthelp://org.qt-project.qtwidgets.5127/qtwidgets/qtwidgets-layouts-flowlayout-example.html 


Use a separate QLabel for each tag, and a single QLineEdit with 
clearButtonEnabled=true that is initially hidden.  In each label's 
onClick, hide the label and insert the edit with the same text 
immediately after it in the layout.  When the focus leaves the edit, if 
there is text in the edit, put the text back into the adjacent hidden 
label and show the label, otherwise delete the label.  Then hide the 
edit box.  Ensure there is always an empty label to click on at the end.

Regards, Tony


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200430/f24cd7e5/attachment.html>


More information about the Interest mailing list