QTabWidget set current tab by tab name?
-
I have a
QTabWidget
and I added new tabs to it by this:m_ui->myTabWidget->addTab(filterWidget, tabName);
I want to set current tab widget by tab name instead of index. Now the first thing I need to do it to find the index, but I don't know how. I found something online like this inside a custom
QTabWidget
but it doesn't work, and aqDebug
return many names except for the one I entered.foreach (QWidget* child, findChildren<QWidget*>()) { if (child->objectName() == _tabName) { qDebug() << child->objectName(); setCurrentIndex(indexOf(child)); } }
-
@lansing said in QTabWidget set current tab by tab name?:
child->objectName()
This returns object name which is completely unrelated to tabName which is a text label.
Why do you want to activate a tab using its text label which will be different when translated to another language?
You can activate using the widget you're inserting via addTab (https://doc.qt.io/qt-5/qtabwidget.html#indexOf). -
I have this
QTabWidget
tied to theQComboBox
on another dialog sharing the same tab name. If the tabwidget changed, the combobox will also changed and vise versa. I also have functions that remove and add new items to this tabwidget/combobox as well as allowing reordering of tab, so I can't connect them through the same index, I have to connect them by names.I have a slot catching the signal on the combobo change, and in the slot I have retrieved my widget through
std::find_if
with a lamda expression. But when I use it with indexof() I got the errorerror: cannot initialize a parameter of type 'QWidget *' with an lvalue of type 'QVector<FilterWidget>::iterator' (aka 'FilterWidget*')
-
@lansing Then why not use index from https://doc.qt.io/qt-5/qcombobox.html#activated to activate the tab?
"But when I use it with indexof() I got the error" - well, I guess (you don't show the code) that you forgot to dereference the iterator when calling indexOf...
-
@jsulm said in QTabWidget set current tab by tab name?:
dereference the iterator
This is my slot catching the combobox signal, I don't know how to dereference it
auto pred = [tabName](const FilterWidget &item) { return item.tabName== tabName; }; QVector<FilterWidget>::iterator it = std::find_if(std::begin(m_filterWidgetVector), std::end(m_filterWidgetVector), pred); if (it != std::end(m_filterWidgetVector)) { int foundIndex = m_ui->myTabWidget->indexOf(it); m_ui->myTabWidget->setCurrentIndex(foundIndex);
-
@lansing said in QTabWidget set current tab by tab name?:
int foundIndex = m_ui->myTabWidget->indexOf(it);
int foundIndex = m_ui->myTabWidget->indexOf(*it);
On a separate note, I don't mean to confuse you but:
QVector<FilterWidget>
Assuming your
FilterWidget
really is aQWidget
, it would be more usual to store pointers to widgets,QVector<FilterWidget *>
, but I don't know what the rest of your code for this looks like.... -
@lansing
I have already answered that in the second part of my post.
You really ought know what you're doing withQWidget
s versusQWidget *
s instead of randomly tyrying things and getting errors, basic C++.Without correcting that (which I think you should, but as I said that's a separate matter), I am not sure but you may be able to make it work from where you are now via
int foundIndex = m_ui->myTabWidget->indexOf(&*it);
-
The FilterWidget is a struct storing a few widgets and some properties, those widgets has pointers.
struct FilterWidget { ImageFilter* imageFilter; TextFilter* textFilter; QString tabName; }; QVector<FilterWidget> m_filterWidgetVector;
indexOf(&*it)
returns the errorerror: cannot initialize a parameter of type 'QWidget *' with an rvalue of type 'FilterWidget *'
-
@lansing
Then I give up. I don't know what you think you are doing. You know thatmyTabWidget->indexOf()
takes aQWidget *
as the widget to try to find, and you are passing it somestruct
(which you have unadvisedly chosen to nameFilterWidget
[and also how can you possibly make a member namedtabName
as of typeint
?]) that has nothing to do with a widget, so how do you expect it to work? Like I wrote, you're not supposed to bung random things in and see what happens, you really need to understand what is going on to write the correct code.... -
@lansing
Hi
The indexOf can find the widgets you insert to tabWidget with
m_ui->myTabWidget->addTab(filterWidget, tabName);so you can find that index again with m_ui->myTabWidget->indexof(filterWidget)
struct FilterWidget cannot be used in this context.
But i wonder how
ImageFilter* imageFilter;
TextFilter* textFilter;
relates to the filterWidget you use with addTab
is it store in imagefilter or textfilter? -
@lansing
Ok so from a FilterWidget item you can find the
tab index by
m_ui->myTabWidget->indexof(filterWidgetENTRY.imageFilter )
or
int foundIndex = m_ui->myTabWidget->indexOf(it.imageFilter ); // maybe need * in front -
This post is deleted!
1/17