Unsolved Access tab created in runtime - Qt C++
-
@HenkCoder said in Access tab created in runtime - Qt C++:
@JonB Oh but I don't need the TextEdit, I need to access the tab.
Let's start again. You want to color the background of just the tab itself, not its page widget? I presumed from your code you meant the latter.
-
@JonB Yes, I want to set the background color of the tab itself, sorry if I explained myself badly.
-
@HenkCoder
Ah! Then accessing thecurrentWidget()
or theTextEdit
is not what you want --- that is for the page-widget itself.Most people would do this via stylesheet:
QTabBar::tab { background-color: ...; }
If you want to do it in code I suggest you Google
qtabwidget tab color
as it looks a bit involved, e.g. https://www.qtcentre.org/threads/2986-How-to-change-color-of-Tab-in-QTabWidget. -
@JonB Oh right! Let me try with stylesheets.
-
@JonB Hey, I tried but that changes the background-color of where the name of the tab is. But I need to set the background-color of the tab page, behind the custom TextEdit.
-
@HenkCoder said in Access tab created in runtime - Qt C++:
Hey, I tried but that changes the background-color of where the name of the tab is.
Yes, and that's exactly what I thought you said you want to change....
So now I'm not sure what you want. I suggest you look at e.g. https://stackoverflow.com/questions/38369015/customuzing-qtabwidget-with-style-sheets and https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qtabwidget-and-qtabbar to determine just what you want to change the color on.
-
@HenkCoder addTab() returns the index of the new tab, so this should fly:
int newTabIndex = ui->tabWidget->addTab(new TextEdit(), QString("New Document " + QString::number(ui->tabWidget->count() + 1))); QWidget *newTabWidget = ui->tabWidget->widget(newTabIndex); ...
-
@JonB
Okay, I'll explain better, I need to access the tab:
behind the textEdit, not where the tab name is. -
@ChrisW67 hey, I tried ur solution but the code doesn't work:
int tabIndex = ui->tabWidget->addTab(new TextEdit(), QString("New Document " + QString::number(ui->tabWidget->count() + 1))); ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1); QWidget *tab{ui->tabWidget->widget(tabIndex)}; QPalette temp{tab->palette()}; temp.setColor(QPalette::Window, QColor(54,54,54)); tab->setPalette(temp);
-
@HenkCoder
You do not need to explain better to me, you need to use the links I gave you to find out which stylesheet rule gives you the result you are looking for.