Solved How to change the order of tabs in a QTabWidget
-
@Tomax63 said in How to change the order of tabs in a QTabWidget:
Because each customer shall be able to save the order of tabs individually.
That's clear. But how do you create the tabs? I mean, you could create them dynamically when the app starts and do it in the order which was saved.
-
I create them in QTDesigner
How can I create them again during app start?
-
- Let them be created initially in the order defined in Qt Creator. this is good anyway for first time, when there is no saved order.
- Read the saved order, if any.
- Move them around accordingly.
What is wrong/the actual problem with this?
-
Your point 3 is the problem. I dont know how to do it.
-
-
Yes, i want to find the "from" value.
-
@Tomax63 I guess you need to iterate over all these widgets until you find the one with given text?
-
@Tomax63
So you'd like us to look at your original code and figure why it can't find the tab, right?I'll look in a sec. Meanwhile:
qint32 i = 0; ... to = i;
How is
to
ever going to be anything other than0
?Although @VRonin wrote
tabwidget->tabBar()->move(from,to);
, I only actually seevoid QTabBar::moveTab(int from, int to)
(https://doc.qt.io/qt-5/qtabbar.html#moveTab). from & to are integer positions, so do yourself a favor and start by replacing yourforeach (QString v, tab_sequenz.split(";"))
with an old-fashionedfor
loop counter so that it's easy to get number positions.... -
@Tomax63
So I think you are saying yourw = ui->my_tabwidget->findChild<QTabWidget *>(v); //get the necessary widget
is not finding the tab by name. That would imply one of:
- The name is not right.
- The matching tab is not currently in the widget.
So do a little digging. What tabs are actually there? What are their names and what are you searching for? If necessary, just do the iteration in your own code to see what's going on. In any case, you're going to need the tab's index to pass to
move()
, so fromfindChild()
you're going to have re-look up the result in the tabs, you might be best off doing it yourself retuning an index number in the first place.... -
I made it.
void MainWindow::restore_tab_seq(QString tab_sequenz){ QStringList v = tab_sequenz.split(";"); qint32 i = 0, j = 0, k = v.count(); for(i=0; i<k; i++){ for (j=0; j<ui->registerkarten->tabBar()->count(); j++){ if (ui->registerkarten->tabBar()->tabText(j).toStdString() == v[i].toStdString()){ ui->registerkarten->tabBar()->moveTab(j, i); } } } } Thanks for your help, mates.
-
Hi,
Why are you using
toStdString
to compare two QStrings ?