问题
I have designed a user interface by using Qt Designer, and I have set the tab order using the "edit tab order" mode.
Now what I'd like to know (for an other reason, not so important) is how to get the tab order of a specific QWidget in the ui?
I mean if I have several widgets, and say the tab order has been set, is there a way to do something like :
int nb = widget1->getTabOrder();
回答1:
There is no way to get the tab order as an integer.
If you look into the C++ code that the uic tool creates from your ui file, it will call QWidget::setTabOrder()
a few times, and that method just takes two QWidget
pointers. Thus, Qt internally doesn't even store the tab order as an integer, but rather as a chained list of QWidget
pointers.
You can query that chained list with QWidget::nextInFocusChain()
and QWidget::previousInFocusChain()
. This gives you the whole focus chain of the widget, containing all child widgets inside it, in the right order. Then you can get the real tab order list by checking their focusPolicy, enabled state and visible state, just like the inside implementation of the QWidget::focusNextPrevChild()
function. If you really need an integer index here, you need to devise an algorithm yourself that calculates indices from that obtained tab order list.
来源:https://stackoverflow.com/questions/20154635/get-focus-or-tab-order