[Interest] Writing custom Qt Widgets is overly complicated

Jonathan Purol contact at folling.de
Tue Apr 28 12:04:03 CEST 2020


Hello everyone,

I'm at a bit of a rough point right now. Up until this point Qt has
never disappointed me - everything I wanted to do was possible to do in
a seamless and understandable way.

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".
You can find an example of this when asking a question on stack-overflow
at the very bottom.
Here's a screenshot:

It's relatively common in web-dev applications, and there are tons of
premade libraries for it out there.

For Qt I only found one, which was a rather shoddy implementation and
tried to do everything from scratch, so I had my go at it.

Here are the three approaches I tried:
1. Subclass LineEdit
2. Write a widget that is completely separate
3. Fiddle around with using `setTextMargins` in QLineEdit, and make the
"tag-items" widgets which use the line-edit as the parent

All three of those approaches failed for me and at this point I'm rather
frustrated since it just seems like I'm missing something obvious.

First of all, I should say that it is obviously possible to do #2 in a
sophisticated manner.
If I write the complete widget from scratch I can achieve exactly what I
want. But doing so would require around a thousand lines of code, if not
more. I was hoping for something that could largely make use of existing
functionality.

I will now go over the reason these things failed for me:

Subclassing the line-edit didn't work for me, because most of the
functionality I would need to not drift off into approach #2 just wasn't
accessible from inside the subclass. Almost no function in QLineEdit is
virtual, and since the configuration flags don't allow for what I want
to do (as far as I can tell - if they did I wouldn't need to make a
class of my own in the first place :P) it seemed like a dead end.

Writing a widget completely separate didn't really fail, it was more
that I didn't find enough "common ground" functionality. From what I saw
I would have to implement text-maneuvering, the context-menu,
right->left functionality, alignment, style-sheets, and all those
shenenigans myself since there
aren't any helper functions for it. Again, this is possible, but not
what I was looking for.
Worse still, I would have to manually calculate the positions for every
tag, to see if the "x" was pressed which seems like a waste of
resources, given that Qt has plenty of functionality around "buttons".


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.
But there was an issue, and that issue was maths.
I found no way to concisely figure out how to vertically center the
widgets in the textbox. Almost all of the dozen or so "width" "height"
or whatever functions returned wrong results. Even the ones that seemed
like they should do what I want `contentsRect` comes to mind.
That's when I opted for the HBoxLayout, which did manage to vertically
center things. But with the HBoxLayout I couldn't find a way to properly
calculate the offset for `setTextMargins`.
Now I couldn't use the width of the widgets - those would return wrong
results as well, and neither HBoxLayout nor any of its parent classes
provide any access to see how much space the widgets occupy. There's
`contentsRect` but that didn't work, neither did `geometry`. I believe
I've tried every single things. `totalSizeHint` was the closest thing
that *almost* worked. But at the end it failed as well when new tags
were added.

I believe that what I'm trying to do *could* be possible with HTML and
richtext, but I wouldn't know how to get notified when the buttons are
pressed at that point, without manually calculating the positions.
Besides, I doubt it'd look particularly natural.

Pardon the lengthy email, but I'm really at a loss here.

Any help is appreciated!

Sincerely,
Jonathan Purol






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200428/6d72bb70/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dclappmgjmgijnfe.png
Type: image/png
Size: 14928 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200428/6d72bb70/attachment.png>


More information about the Interest mailing list