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
-
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
-
did you try
tabwidget->tabBar()->move(from,to);
https://doc.qt.io/qt-5/qtabbar.html? -
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.@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.
-
@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 ?