[Qt-interest] Surprising tab order with focus proxy on QSpinBox
andrew.m.goth at l-3com.com
andrew.m.goth at l-3com.com
Thu Jan 22 04:42:06 CET 2009
I have a QWidget subclass called Bug that sets its focus proxy to be a
child QSpinBox. I have four Bugs in a QWindow. When I create the Bug
instances in a sequence that doesn't match the programmatically defined
tab order, the actual tab order is incorrect.
With the following code linked against Qt 4.4.3, the actual tab order is
bug1, bug2, bug4, bug3, whereas it should be bug1, bug2, bug3, bug4.
========[bug.cc]========
#include <QApplication>
#include <QVBoxLayout>
#include "bug.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget* window = new QWidget();
QVBoxLayout* vbox = new QVBoxLayout(window);
Bug* bug1 = new Bug(window);
Bug* bug3 = new Bug(window);
Bug* bug4 = new Bug(window);
Bug* bug2 = new Bug(window);
vbox->addWidget(bug1);
vbox->addWidget(bug2);
vbox->addWidget(bug3);
vbox->addWidget(bug4);
QWidget::setTabOrder(bug1, bug2);
QWidget::setTabOrder(bug2, bug3);
QWidget::setTabOrder(bug3, bug4);
bug1->setFocus(Qt::OtherFocusReason);
window->show();
int status = app.exec();
delete window;
return status;
}
========[bug.h]========
#include <QSpinBox>
#include <QVBoxLayout>
#include <QWidget>
class Bug: public QWidget {
Q_OBJECT
public:
Bug(QWidget* parent = 0): QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
QSpinBox* spinbox = new QSpinBox(this);
layout->addWidget(spinbox);
setFocusProxy(spinbox);
setFocusPolicy(Qt::TabFocus);
}
};
=======================
My guess is that the problem is in how QWidget::setTabOrder handles
focus proxies. If the first argument has a proxy, it substitutes the
last child of first that accepts focus. If the second argument has a
proxy, it substitutes the proxy. When passed a Bug* as its first
argument, it substitutes the QLineEdit embedded in the QSpinBox inside
the Bug. But when passed a Bug* as its second argument, it substitutes
the QSpinBox. The result is a corrupted (or at least confusing) focus
traversal linked list.
This a problem for me because the dialog code for my application is
generated by Qt Designer, which doesn't let me control the order in
which the widgets are instantiated.
So, is this a Qt bug, or am I just misunderstanding something? What can
I do to work around or fix this problem? If it's a Qt bug, is it a
known issue (perhaps fixed in Qt 4.5) or a new problem? Maybe it was
fixed as a part of 217395 "QSpinBox ignores focus policy".
--
Andy Goth
<amgoth at link.com>
More information about the Qt-interest-old
mailing list