如何按标签文本/名称调整QTabWidget的标签顺序?
Hey there! Let's clear up a couple of things first because it looks like you might have mixed up two different Qt concepts here.
First off: setTabOrder() is for setting the keyboard focus traversal order between widgets, not the order of tabs displayed in the QTabWidget's tab bar. If you want to reorder how the tabs appear in the bar, you'll need to use moveTab() or insertTab() instead.
Also, there's no public QTab class in Qt—those tabs in the tab bar are managed by QTabBar, but we don't interact with them directly when reordering tabs. Instead, we work with the actual widget that each tab is attached to (the one you added with addTab() or in Qt Designer).
Reordering Tabs by Object Name (like "tab_1", "tab_2")
If you want to rearrange tabs based on their widget's object name (the one you set in Qt Designer or via setObjectName()), here's how to do it:
- Grab references to the tab widgets using
findChild<QWidget*>(). - Get their current indices in the QTabWidget.
- Use
moveTab()to shift them to your desired position.
Example code:
// Get the tab widgets by their object names QWidget* tab1 = ui->tabWidget->findChild<QWidget*>("tab_1"); QWidget* tab2 = ui->tabWidget->findChild<QWidget*>("tab_2"); // Make sure we actually found the widgets before proceeding if (tab1 && tab2) { // Get their current positions in the tab bar int idx1 = ui->tabWidget->indexOf(tab1); int idx2 = ui->tabWidget->indexOf(tab2); // Move tab2 to the position of tab1 (tab1 will shift right automatically) ui->tabWidget->moveTab(idx2, idx1); }
Reordering Tabs by Tab Text (like "helloTab", "hiTab")
If you want to reorder based on the visible text on each tab, you can loop through the tabs to find matches, then move them to your desired order:
// Define the order you want the tab texts to appear in QStringList desiredTabOrder = {"helloTab", "hiTab", "settingsTab"}; for (int targetPosition = 0; targetPosition < desiredTabOrder.size(); ++targetPosition) { const QString& targetText = desiredTabOrder[targetPosition]; // Find the index of the tab with this text int currentIndex = -1; for (int i = 0; i < ui->tabWidget->count(); ++i) { if (ui->tabWidget->tabText(i) == targetText) { currentIndex = i; break; } } // If we found the tab and it's not already in the right spot, move it if (currentIndex != -1 && currentIndex != targetPosition) { ui->tabWidget->moveTab(currentIndex, targetPosition); } }
If You Actually Wanted Focus Traversal Order
If your goal was to set the order that keyboard focus jumps between tabs when pressing Tab, then setTabOrder() is the right tool—but you need to pass the tab widgets themselves, not some non-existent QTab object:
QWidget* tab1 = ui->tabWidget->findChild<QWidget*>("tab_1"); QWidget* tab2 = ui->tabWidget->findChild<QWidget*>("tab_2"); if (tab1 && tab2) { // Set focus to move from tab1 to tab2 when Tab is pressed ui->tabWidget->setTabOrder(tab1, tab2); }
The key mistake in your original code was trying to find a QTab object—Qt doesn't expose that publicly. All tab-related operations are done through the QTabWidget itself, using the widgets attached to each tab.
内容的提问来源于stack exchange,提问作者user8849164

