How to set the QTabWidget background image when there is not any widget page
-
Thanks to everyone who answered.You really help me a lot.And Finally I found a solution what I want.
I found that the emptyQTabWidget
contains aQStackedWidget
.QList<QWidget*> widgets = ui->tabWidget_2->findChildren<QWidget*>(); for (auto wid : widgets) { auto meta = wid->metaObject(); qDebug() << meta->className(); qDebug() << wid->objectName(); qDebug() << "=============="; }
And there is the output
QStackedWidget "qt_tabwidget_stackedwidget" ============== QTabBar "qt_tabwidget_tabbar" ============== QToolButton "" ============== QToolButton "" ==============
So I write a qss like this, and it really works.
QTabWidget>QStackedWidget#qt_tabwidget_stackedwidget { background-image: url(:/img/background.png); }
With @Bonnie suggestion, it works perfectly.
@Bonnie said in How to set the QTabWidget background image when there is not any widget page:
for every widget added to the tabwidget, set autoFillBackground to true.
-
@Hanson
"No widgets" where? Do you mean that each Tab is supposed to show widgets on itself? And if there are none then the frame to the right should show an image (bit not if the current tab has any widgets)? Or, do you mean something about that right-hand frame should show widgets (from where)? -
@JonB said in How to set the QTabWidget background image when there is not any widget page:
And if there are none then the frame to the right should show an image (bit not if the current tab has any widgets)?
Yes!There none and the tabwidget show an image.
-
@Hanson said in How to set the QTabWidget background image when there is not any widget page:
@JonB And the left side QTabWidget has nothing to do with this question.
That's really helpful to say now, it's as clear as mud! I previously asked for clarification.
So, I don't know, did you try setting the
QTabWidget
itself's background instead of that for any "pages"? -
An empty tab doesn’t show anything, neither does an empty widget, as @JonB said.
If you want to show an image to represent an empty default, populate the empty tab with a label that shows the desired image. -
@Axel-Spoerl said in How to set the QTabWidget background image when there is not any widget page:
populate the empty tab
My impression is that OP is saying the
QTabWidget
has 0 tabs.
In that situation I'm not even sure what area theQTabWidget
occupies, it may not even be the red frame area shown in the pic? -
@JonB I thought the same thing, so I made this MRE based on what I guess OP needs:
//this is a container widget QWidget *w = new QWidget(); QVBoxLayout *l = new QVBoxLayout(w); QPushButton *b = new QPushButton("add",w); QPushButton *b1 = new QPushButton("remove",w); QTabWidget *t = new QTabWidget(w); //here I'm setting QTableWidget background image before it is displayed t->setStyleSheet("background-image: url(:/green.png);"); l->addWidget(b); l->addWidget(b1); l->addWidget(t); //here I'm making it detect when all tabs are removed so It can set a background image when it's empty t->connect(t,&QTabWidget::currentChanged, [=]() { if(t->currentIndex()==-1) t->setStyleSheet("background-image: url(:/green.png);"); else t->setStyleSheet(""); }); //these are just buttons I used to add and remove tabs b->connect(b,&QPushButton::clicked, [=]() { t->addTab(new QWidget(),"Tab"); }); b1->connect(b1,&QPushButton::clicked, [=]() { t->removeTab(t->count()-1); }); w->show();
Here's how it functions:
@Hanson could you take a look at it, and answer the previous questions to clarify what you need?
-
@Hanson I guess Tab1 and Tab2 are QTabWidget.
auto page2 = new QWidget( this );
tabWidget->addTab( page2, QString( "Tab2" ) );
You create a widget and set it as Tab2.If you have created page2,
auto page2 = tabWidget->widget( 1 );Then set a picture with stylesheet for icon_name.
page2->setStyleSheet(QStringLiteral("border-image: url(:/resources/images/%1)").arg( icon_name ) ); -
This post is deleted!
-
Thanks to everyone who answered.You really help me a lot.And Finally I found a solution what I want.
I found that the emptyQTabWidget
contains aQStackedWidget
.QList<QWidget*> widgets = ui->tabWidget_2->findChildren<QWidget*>(); for (auto wid : widgets) { auto meta = wid->metaObject(); qDebug() << meta->className(); qDebug() << wid->objectName(); qDebug() << "=============="; }
And there is the output
QStackedWidget "qt_tabwidget_stackedwidget" ============== QTabBar "qt_tabwidget_tabbar" ============== QToolButton "" ============== QToolButton "" ==============
So I write a qss like this, and it really works.
QTabWidget>QStackedWidget#qt_tabwidget_stackedwidget { background-image: url(:/img/background.png); }
With @Bonnie suggestion, it works perfectly.
@Bonnie said in How to set the QTabWidget background image when there is not any widget page:
for every widget added to the tabwidget, set autoFillBackground to true.
-
-
-
-
-
-