How to set the QTabWidget background image when there is not any widget page
-
wrote on 29 May 2023, 08:20 last edited by
-
wrote on 30 May 2023, 01:43 last edited by Hanson
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.
-
Hi,guys!I want to set the background image when first show
QTabWidget
that there is no widgets.
Any help is appreciated!
wrote on 29 May 2023, 08:27 last edited by@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)? -
@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)?wrote on 29 May 2023, 08:47 last edited by@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.
-
@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.
wrote on 29 May 2023, 08:54 last edited by@Hanson
So just put a slot on signal void QTabWidget::currentChanged(int index) which looks whether the new current tab has any child widgets/page or not and set the right-hand frame's background image on or off accordingly. -
@Hanson
So just put a slot on signal void QTabWidget::currentChanged(int index) which looks whether the new current tab has any child widgets/page or not and set the right-hand frame's background image on or off accordingly. -
@JonB Emm, I think you misunderstood my thought.The right widget is the
QTabWidget
that has no any page,and I want to show the background image when thisQTabWidget
show first time and without any page.
-
wrote on 29 May 2023, 11:20 last edited by
@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"? -
@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. -
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.wrote on 29 May 2023, 12:04 last edited by JonB@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? -
@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?wrote on 29 May 2023, 13:07 last edited by@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?
-
@JonB Emm, I think you misunderstood my thought.The right widget is the
QTabWidget
that has no any page,and I want to show the background image when thisQTabWidget
show first time and without any page.
wrote on 29 May 2023, 14:15 last edited by JoeCFD@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 ) ); -
wrote on 29 May 2023, 15:23 last edited byThis post is deleted!
-
@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"? -
@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?
wrote on 30 May 2023, 01:01 last edited by@Abderrahmene_Rayene Yes.This is what I want.But can you set the background image when the QTabWidget show first without any pages.
-
wrote on 30 May 2023, 01:43 last edited by Hanson
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.
-
-
-
-
-
wrote on 30 May 2023, 02:17 last edited by
Oh no!I found the qss also affect all the pages in the
QTabWidget
:( -
@Hanson One way to solve this: for every widget added to the tabwidget, set
autoFillBackground
totrue
.
Or you can just dynamically set the qss, when there's no widget, clear the qss, when there's any, set the qss. -
-
7/18