Solved How to change the order of tabs in a QTabWidget
-
Good Morning all
I want to change the order of my tabs according to the sequence they had when I left the program last time.
I have already saved the names in the correct order in a string, separated by ";":Now I want to bring the tabs into this exact sequence when startung the program. Here is my routine so far:
tab_sequenz = "definition;digitalisierung;dig2;qhi;anlage;drehmoment;medien;pumpentypauswahl;npsh;isobar;isochor;alt_medium;bypass;drossel;parallel;simulation;tco;einstellungen" void MainWindow::restore_tab_seq(QString tab_sequenz){ QTabWidget *w; QWidget t; qint32 i = 0; qint32 to; foreach (QString v, tab_sequenz.split(";")) { cout << v.toStdString() << endl; w = ui->my_tabwidget->findChild<QTabWidget *>(v); //get the necessary widget to = i; if (w != NULL){ cout << w->currentIndex() << endl; } } }
w appears to be NULL at all times. How can I :
- Find the tab with the name saved in tab_sequenz[i]?
- send the tab to the position i?
Thomas
-
@Tomax63 Why don't you create the tabs in the correct order? Then there is no need to sort them.
-
Because each customer shall be able to save the order of tabs individually.
th order is saved in the string tab_sequenz when leaving the program and this order can be different from the order in my original ui. -
did you try
tabwidget->tabBar()->move(from,to);
https://doc.qt.io/qt-5/qtabbar.html? -
Thanks for the tip. But to try this, i first have to find the "from", and I just have the name of the tab.
This is why I try to find its index.After I found the actuel position, i will move it by using the move() - instruction.
-
@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 ?