I have a QLineEdit inside a widget insi开发者_如何学JAVAde a QTabWidget. Let's say that tab is at tab index 2. When currently active tab is at index 1, and I call setFocus() (and show(), and raise()) on the QLineEdit, the active tab doesn't switch to index 2 to show the focused widget.
Is there any elegant way to show the focused widget's tab when that widget gets focus?
Thanks!
Here's a more generic function to switch to the tab containing a widget:
void SwitchToTabContaining(QTabWidget * tabsW, QWidget * w)
{
for(int i=0;i<tabsW->count();++i) {
QWidget * tab = tabsW->widget(i);
if(tab->isAncestorOf(w)) {
tabsW->setCurrentWidget(tab);
break;
}
}
}
It's not tested but you should get the idea of how it works.
Assuming the QTabWidget
is named qtab:
In the QLineEdit's focusInEvent
:
QTabBar* bar = qtab->tabBar();
bar->setCurrentIndex(2);
And for your specific case, where you're sure the QLineEdit is a child of one of your tab widgets:
myTabWidget->setCurrentIndex(myTabWidget->indexOf(myLineEdit->parentWidget()));
精彩评论