Margins with repeated packaging of QWidgets


  • Moderators

    Within Qt guis one may pack a couple of elements into a class derived from QWidget and use this new class as a new widget. One may do this even a couple of times. Apparently each time the space available for GUI elements does shrink a bit. Certainly this is important when there is also some reason to have some external distinction such as a frame, which needs of course some space.

    However, when using simply QWidget for packing only there are also apparently margins substracted each time. For larger widgets that is acceptable. When starting to pack different widgets into another and placing them into a QWidgetTable, this may become a nuisance.

    I have basically a radio button, which I have placed as QWidgetTableItem into a QWidgetTable. It looks fine as longas I am using only this radiobutton which is only in one column while all the other columns contain text.

    I have decided now to place more than simply one radiobutton there. Therefore, I have written a class derived from QWidget which basically holds several radiobuttons. I have added QHBoxLayout in order to have the radiobuttons nicely distributed.

    Suddenly the radiobuttons are no longer in the center of the field, but seem to add with each wrapping some margin. As a result the radiobutton are getting smaller. Resizing them gets them bigger, but they are definitely no longer in the center.

    Using setContentsMargins with values of 0 does not eliminate the complete "wasted" space. Is there another settings to used as well?



  • I remembered I did it and now it's weird... you can achieve this using QHBoxLayout::setMargin(int) and passing a 0. Strangely enough I can't find this method in the docs but I used it in the past and auto-complete finds it


  • Moderators

    @VRonin

    Here is the reason why you cannot find it setMargin

    However, I am not sure, it looks it might still have a marginal effect in Qt 5.4 (which I am using).


  • Lifetime Qt Champion

    Hi,

    Can you post a sample code to reproduce this ?


  • Moderators

    @VRonin

    setMargin(0) has the same effect as setContentsMargins(0, 0, 0, 0) in Qt 5.4.
    As you pointed already out the documentation is missing. Apparently, it was kicked out because it was marked as obsolete.
    Either it was still active in Qt 4.8 and the term obsolete was premature or it slipped back in.


  • Moderators

    @SGaist

    I have tried to get an example to the problem I saw apparently, but I did not succeed. For a simple radio button it is working, but the layout's margins need to be set to 0 either with setMargin or setContentsMargins.

    For the small example I have generated I had introduced also the derived class I used and it seems to work there. Therefore, there must be another problem with the QTableWidget's implementation or I simply fall into a trap.

    Therefore, I will mark as solved.