Important: Please read the Qt Code of Conduct -

creating layout in function

  • i have a widget subclass on which i'm setting a layout. in this layout i have many other layouts. i'm creating all these layouts in my subclass ctor, adding them to main layout and setting it to the widget.

    however, one of the layouts is created in a function. i do it like this: i have a member widget (another one). i init this widget in ctor, then, in layout creating function (the separate one), when the layout is created, i set the layout to the member widget.

    then, in ctor, this widget gets added to the main layout. however, the tab order between widgets is now broken. if i take the code from function and paste it in ctor, everything works fine. so what's the problem with function version?

    and the layout that was created in function gets focus AFTER the layout that's created after it in ctor

  • Lifetime Qt Champion


    You can use QWidget::setTabOrder.

  • i know about it. i've tried that and it doesn't work

  • Lifetime Qt Champion

    Also as a note.
    The only real difference between ctor and a function is that in function,
    the widgets have been showed first time and all layouts been calculated and
    also tabOrder been established.

    When in ctor, some calculations are are
    delayed until first shown as some widget could be hidden etc.

    But if you call the function from the ctor. it should do the same.
    Its not clear how you call function but I assume you did not call it
    from ctor but say from a button ?

  • it's a call from a ctor, returning void.

    i also tried returning the layout from the function and setting it to my member widget but that didn't work either.

    only when i copy paste function code in ctor, it works

  • Lifetime Qt Champion

    That is a bit odd as calling a function from ctor should by all means be the same as code
    directly there.
    There must have been a slight difference in what it did. function vs code directly.

  • @mrjj

    say i have 3 hlayouts in main vlayout:

    1st hlayout - created in ctir
    2nd hlayout - created in function
    3rd hlayout - created in ctor
    all layouts added to main vlayout
    set vlayout as widget's layout

    in this scenario, say i have a line edit in 1st layout. the tab then goes to 3rd layout, traverses the items, then goes to 2nd layout.

  • Lifetime Qt Champion

    well it sounds like 2nd layout is considered added last.
    Are you sure function does 100% the same as when u paste the code ?
    Is that same code completely ?

  • @mrjj

    yes it's the same code.

    btw the layout (2nd) is a member, and i create it in the function.

  • @SGaist said in creating layout in function:


    You can use QWidget::setTabOrder.

    @user4592357 said in creating layout in function:

    i know about it. i've tried that and it doesn't work

    If you still wish to address your issue, I find it hard to believe/surprising that you say QWidget::setTabOrder "doesn't work". Where did you put the calls (e.g. before or after adding them to layouts, before/after adding layouts to layouts --- I would suggest calling it as late as possible, after everything has been created & set up) against which widgets? Who are the parents of the widgets, etc.? Since you say the situation is so simple, you can presumably recreate it in a simple standalone case, without worrying about anything else your existing code might be doing?

Log in to reply