TabOrder children



  • Hello,
    look at this window example: http://pbrd.co/QEHdDP
    !http://pasteboard.s3.amazonaws.com/images/1348661304914253.png(layout example)!
    It contains two custom widgets (which, for simplicity, contain not more than two QLineEdits) and a QPushButton, and everything inside a QVBoxLayout.
    I want the tab order (you press the tab key to move the focus to another widget) to be like this:

    LineEdit top left

    LineEdit top right

    PushButton bottom

    LineEdit center left

    LineEdit center right

    The problem is, that you can't just go into the implementation of the custom widget. You have no access to it's children. So all you can do is this:

    custom widget top

    PushButton bottom

    custom widget center

    I tried this using the setTabOrder function, but
    @// it does not work
    setTabOrder(custom1, button);
    setTabOrder(button, custom2);@

    The custom widgets never actually get the focus, just their underlying LineEdits do. Is there a possibility to do this?



  • The tab order inside the custom widget, is the business of the custom widget. I don't really get what the actual problem is here.



  • Probably you have to reimplement the QWidget::focusNextPrevChild method...



  • Andre:
    Ok, let me try to explain it again.
    This is the tab order I want to have: http://pbrd.co/OUVqRa
    However, if I use the code I showed in my start post or just specify no tab order at all, this is the tab order I get: http://pbrd.co/QaGNEw

    broadpeak:
    Wouldn't that be the same as setTabOrder? If I rewrite that method, I could do something like @custom2->setFocus(Qt::OtherFocusReason)@ but afaik you cannot just give the focus to those custom widgets. Only the LineEdits can have their focus set like this.

    [Edit: Removed extraneous @ tags which were messing up formatting. -- mlong]



  • So, what happens if you set the tab order in this form to be TopCustomWidget -> Button -> CenterCustomWidget, and then inside the custom widgets, to leftLineEdit -> rightLineEdit ?



  • That is what I did and which gives me the unwanted result.

    @CustomWidget::CustomWidget(QWidget *parent)
    : QWidget(parent)
    {
    lineedit1 = new QLineEdit;
    lineedit2 = new QLineEdit;
    layout = new QHBoxLayout;
    layout->addWidget(lineedit1);
    layout->addWidget(lineedit2);
    setLayout(layout);
    setTabOrder(lineedit1, lineedit2); // here
    }@

    @qtTEST::qtTEST(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    QVBoxLayout *layout = new QVBoxLayout;
    QWidget *wid = new QWidget;
    setCentralWidget(wid);
    wid->setLayout(layout);

    CustomWidget *top = new CustomWidget;
    CustomWidget *bot = new CustomWidget;
    layout->addWidget(top);
    layout->addWidget(bot);
    QPushButton *button = new QPushButton;
    layout->addWidget(button);

    wid->setTabOrder(top, button); // here
    wid->setTabOrder(button, bot); // and here
    }@



  • I made it work. The key is to call
    @setFocusProxy(x); // x is your first child widget
    setFocusPolicy(Qt::TabFocus);@
    inside the custom widget.

    It is important that you cannot simply use setTabOrder inside the custom widget to give its children a special order. You have to create those widgets in the wanted order.
    Example, if you want the right QLineEdit be focused first, then the left one:
    @lineedit2 = new QLineEdit(this);
    lineedit1 = new QLineEdit(this);@

    Maybe this might help someone, who has the same problem in the future.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.