How can set QTabWidget tabs in a certain order?
-
Hi betments,
If you want to avoid this slot to be called when you add Tabs, you can add following statements before and after you operation.
@
ui->tabw_main->blockSignals(true);
@and
@
ui->tabw_main->blockSignals(false);
@ -
hi batnaens,
if you want solved this problem then you have to add statement before and after you operation.
thank you:) -
Well, QTabWidget provides access to it's embedded QTabBar instance via tabBar(), and [[doc:QTabBar]] has a method moveTab(int from, int to). If you don't want to subclass your QTabWidget, you can use QObject::findChildren<QTabBar*>() on your QTabWidget to get a pointer to the QTabBar.
-
Thanks Andre.
@QStringList tb_main_list;
Project::Project(QWidget *parent) : QMainWindow(parent), ui(new Ui::Project) {
tb_main = ui->tabwMain->findChild<QTabBar *>("qt_tabwidget_tabbar");
int n = tb_main->count();
for (int i = 0; i < n; ++i) tb_main_list << QString::number(i);//("0", "1", "2", "3") save as default tab order. Needed if programmatically change tab count.
connect(tb_main, SIGNAL(tabMoved(int, int)), this, SLOT(tab_order(int, int)));
}
//when tab is moved, change also tb_main_list order
void Project::tab_order(int to, int from){
QString t1 = tb_main_list[from], t2 = tb_main_list[to];
tb_main_list[from] = t2;
tb_main_list[to] = t1;
}@
But here is mathematical problem for me :)
for example I have saved tab order ("3", "0", "2", "1")
How can I change with moveTab(int from, int to) ("0", "1", "2", "3") to ("3", "0", "2", "1")?
With for (int i = 0; i < n; ++i) list << tb_main_list.indexOf(i) I can get also ("1", "3", "2", "0"). That will be helpful if moveTab(0, 3) changed from ("0", "1", "2", "3") to ("3", "1", "2", "0"), but result is ("1", "2", "3", "0") -
moveTab is not switchTab, of course. The move of the tab seems clear to me. The tab at index 0 ("0") is taken from the array, and then re-inserted at postition 3 (the end of the sequence). When taking "0" from the array, all other items move one position to the left, hence your result.
-
OK, here are the moves needed to reorder your tabs:
You start with the order "0", "1", "2", "3", and your goal is "3", "0", "2", "1". I think it is easiest to order them simply starting at the front of the rows of tabs. So, starting with position 0, each time you move the tab you want at that position to that index. The tabs you already ordered will not be affected, as they are already at the right place so only the tabs behind it will change index.
@
start "0", "1", "2", "3"
move 0: "3", "0", "1", "2" // move from 3 to 0
move 1: "3", "0", "1", "2" // move from 1 to 1: no move needed
move 2: "3", "0", "2", "1" // move from 3 to 2
@Note that the "to" index is equal to the move number, and equal to the index in the row of tabs we're fixing. Also note that the "from" index is always >= to the "to" index, thus we're never messing up the work we already did.
-
It's impossible :)
@QStringList list_default, list_user;
int i, n, from, to;
list_default<<"0"<<"1"<<"2"<<"3";
list_user<<"2"<<"0"<<"3"<<"1";
n = list.count(); //or list.count() - 1
for (i = 0; i < n; ++i) {
from = ???;
to = ???;
tb_main->moveTab(from, to);
}
@
Seems I tried everything in ??? -
[quote author="betmens" date="1333654853"]It's impossible :)
[/quote]Yes, it's possible! ;)
You must iterate through list from the end to the begining.
Here is the example (maybe will be useful for somebody, if it's to late for you):@
QStringList list_default, list_user;
int i, n, from, to;
list_default<<"0"<<"1"<<"2"<<"3";
list_user<<"2"<<"0"<<"3"<<"1";
n = list_user.count() - 1
for (i = n; i > -1; i--) {
list_element = list_user[i];
to = i;
from = list_default.index(list_element);list_default.insert(to, list_default.pop(from)); // keep default list consistent
tb_main->moveTab(from, to);
}
@Sorry for the possible program syntax errors (line 11 maybe), in above example - most time I programming in PyQt, so I am not much familiar with C++ & Qt...