开发者

Is there an elegant way to activate a tab in which a focused widget is located?

开发者 https://www.devze.com 2023-02-04 04:36 出处:网络
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())

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()));
0

精彩评论

暂无评论...
验证码 换一张
取 消