Access tab created in runtime - Qt C++
-
Hello, I'm using Qt 6.2.0 with MinGW compiler on Windows 10 and I would ask you a thing.
I have this code that creates a tab with a custon TextEdit in it:ui->tabWidget->addTab(new TextEdit(), QString("New Document " + QString::number(ui->tabWidget->count() + 1)));
How can I access that tab and change the background color of it?
I tried this code but it doesn't work:ui->tabWidget->currentWidget()->setAutoFillBackground(true); QPalette temp{ui->tabWidget->currentWidget()->palette()}; temp.setColor(QPalette::Window, QColor(54,54,54)); ui->tabWidget->currentWidget()->setPalette(temp)
Any suggestions?
-
Hello, I'm using Qt 6.2.0 with MinGW compiler on Windows 10 and I would ask you a thing.
I have this code that creates a tab with a custon TextEdit in it:ui->tabWidget->addTab(new TextEdit(), QString("New Document " + QString::number(ui->tabWidget->count() + 1)));
How can I access that tab and change the background color of it?
I tried this code but it doesn't work:ui->tabWidget->currentWidget()->setAutoFillBackground(true); QPalette temp{ui->tabWidget->currentWidget()->palette()}; temp.setColor(QPalette::Window, QColor(54,54,54)); ui->tabWidget->currentWidget()->setPalette(temp)
Any suggestions?
@HenkCoder
Your code assumes that afterQTabWidget::addTab(QWidget *page, const QString &label)
the added page becomes thecurrentWidget()
("page currently being displayed by the tab dialog."). Did you verify that is the case?Try setting the background color via stylesheet instead of palette code. Does that work?
If so, I don't know but https://stackoverflow.com/questions/23709091/changing-the-background-color-of-qtabwidget claims
In order to make subclass of QWidget works on background color, you need override this function
def paintEvent(self, event):
....since your
TextEdit
is presumably a subclass ofQTextEdit
. -
@HenkCoder
Your code assumes that afterQTabWidget::addTab(QWidget *page, const QString &label)
the added page becomes thecurrentWidget()
("page currently being displayed by the tab dialog."). Did you verify that is the case?Try setting the background color via stylesheet instead of palette code. Does that work?
If so, I don't know but https://stackoverflow.com/questions/23709091/changing-the-background-color-of-qtabwidget claims
In order to make subclass of QWidget works on background color, you need override this function
def paintEvent(self, event):
....since your
TextEdit
is presumably a subclass ofQTextEdit
. -
@HenkCoder
Your code assumes that afterQTabWidget::addTab(QWidget *page, const QString &label)
the added page becomes thecurrentWidget()
("page currently being displayed by the tab dialog."). Did you verify that is the case?Try setting the background color via stylesheet instead of palette code. Does that work?
If so, I don't know but https://stackoverflow.com/questions/23709091/changing-the-background-color-of-qtabwidget claims
In order to make subclass of QWidget works on background color, you need override this function
def paintEvent(self, event):
....since your
TextEdit
is presumably a subclass ofQTextEdit
. -
@JonB Uhh no, how do I do that? Like a
if(ui-tabWidget->currentWidget()->metaObject()->className == "QTab")
?@HenkCoder said in Access tab created in runtime - Qt C++:
@JonB Uhh no, how do I do that? Like a if(ui-tabWidget->currentWidget()->metaObject()->className == "QTab")?
I meant e.g.
TextEdit *te = new TextEdit(); ui->tabWidget->addTab(te, QString("New Document " + QString::number(ui->tabWidget->count() + 1))); Q_ASSERT(ui->tabWidget->currentWidget() == te);
-
@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.
-
@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. -
@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. -
@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 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); ...
-
@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); ...
@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);
-
@JonB
Okay, I'll explain better, I need to access the tab:
behind the textEdit, not where the tab name is.@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.