Reputation: 2418
In my project, I have a QTabWidget
with multiple tabs, each having a unique icon. I want the icon to change to a lighter version of the normal icon whenever the tab is selected.
This is simple with stylesheets QTabBar::tab:selected{image: url(image.png);}
however the problem is that the stylesheet can't differentiate between each different tab to set the correct icon. There isn't just an 'on' and 'off' icon. There is an 'on' and 'off' icon for 7 different icons.
Sticking with the stylesheet :selected
method, I'm trying to find a way to accomplish this with accessibleName. If upon tab creation I set the accessibleName for each tab to an icon type identifier (icon_1, icon_2, icon_3, etc), I could select it later in my stylesheet with QTabBar::[accessibleName=\"icon_1\"]:selected
.
However I don't see anything in the documentation that says I can set an accessibleName for each tab. Is this possible? Also, I'm aware I could listen for the currentIndexChanged signal and update the icon without stylesheets, however the actual method of figuring out which icon to use for each tab is quite a bit of work, so it's not ideal to recheck every tab each time the index has changed. Not to mention that the user can have as many tabs open at any given time. Make sense?
Is it possible to set an accessibleName onto a tab? Is there another way to select between different tabs via stylesheets? Thanks for your time.
Upvotes: 0
Views: 775
Reputation: 8311
You can achieve this without writing a stylesheet.
As a QIcon
object can hold multiple images, you can simply create a QIcon
holding both images (the normal and the lighter one) for you tab.
By default the QTabWidget
will use the image for "State = Off and Mode = Normal" for unselected tabs and the image for "State = On and Mode = Normal" for the selected tab.
Here is a sample code:
QIcon icon_for_tab1;
icon_for_tab1.addFile("tab1.png", QIcon::Off, QIcon::Normal);
icon_for_tab1.addFile("tab1_lighter.png", QIcon::On, QIcon::Normal);
QIcon icon_for_tab2;
icon_for_tab2.addFile("tab2.png", QIcon::Off, QIcon::Normal);
icon_for_tab2.addFile("tab2_lighter.png", QIcon::On, QIcon::Normal);
QTabWidget *tabWidget;
// Supposing tabWidget point to a valid QtabWidget instance with at least 2 tabs.
tabWidget->setTabIcon(0, icon_for_tab1);
tabWidget->setTabIcon(1, icon_for_tab2);
Upvotes: 2
Reputation: 53
You could lock the other tabs in order to highlight the current tab. The text and icons in the locked tabs are greyed out so the current tab is highlighted.
I used to do that with code from SO:
void MainWindow::lockTabs(int except){
for (int i=0; i < ui->tabWidget->count(); i++) {
if (i != except) ui->tabWidget->setTabEnabled(i, false);
}
}
void MainWindow::unlockTabs() {
for (int i=0; i < ui->tabWidget->count(); i++) {
ui->tabWidget->setTabEnabled(i, true);
}
}
ui->tabWidget ist my QTabWidget.
In your tabBarClicked slot you can highlight the current one:
void MainWindow::on_tabWidget_tabBarClicked(int index) {
this->unlockTabs();
this->lockTabs(index);
}
IMHO this is what a user expects for highlighting the current tab.
Upvotes: 0