Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set the QTabWidget background image when there is not any widget page
Forum Updated to NodeBB v4.3 + New Features

How to set the QTabWidget background image when there is not any widget page

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 7 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ JonB

    @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"?

    Axel SpoerlA Offline
    Axel SpoerlA Offline
    Axel Spoerl
    Moderators
    wrote on last edited by
    #8

    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.

    Software Engineer
    The Qt Company, Oslo

    JonBJ 1 Reply Last reply
    0
    • Axel SpoerlA Axel Spoerl

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #9

      @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 the QTabWidget occupies, it may not even be the red frame area shown in the pic?

      C 1 Reply Last reply
      1
      • JonBJ 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 the QTabWidget occupies, it may not even be the red frame area shown in the pic?

        C Offline
        C Offline
        CPPUIX
        wrote on last edited by
        #10

        @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:

        qtabwidgetback.gif

        @Hanson could you take a look at it, and answer the previous questions to clarify what you need?

        HansonH 1 Reply Last reply
        2
        • HansonH Hanson

          @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 this QTabWidget show first time and without any page.
          8e3eed4c-8e85-4bb9-8a73-ba7bcbd706f2-image.png

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #11

          @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 ) );

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shottercleve
            wrote on last edited by
            #12
            This post is deleted!
            1 Reply Last reply
            0
            • JonBJ JonB

              @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"?

              HansonH Offline
              HansonH Offline
              Hanson
              wrote on last edited by
              #13

              @JonB Yes!I just want to set the QTabWidget itself's background instead of that for any "pages".

              “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
              —— Martin Golding

              1 Reply Last reply
              0
              • C CPPUIX

                @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:

                qtabwidgetback.gif

                @Hanson could you take a look at it, and answer the previous questions to clarify what you need?

                HansonH Offline
                HansonH Offline
                Hanson
                wrote on last edited by
                #14

                @Abderrahmene_Rayene Yes.This is what I want.But can you set the background image when the QTabWidget show first without any pages.

                “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                —— Martin Golding

                1 Reply Last reply
                0
                • HansonH Offline
                  HansonH Offline
                  Hanson
                  wrote on last edited by Hanson
                  #15

                  Thanks to everyone who answered.You really help me a lot.And Finally I found a solution what I want.
                  I found that the empty QTabWidget contains a QStackedWidget.

                  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.

                  “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                  —— Martin Golding

                  1 Reply Last reply
                  0
                  • HansonH Hanson has marked this topic as solved on
                  • HansonH Hanson has marked this topic as unsolved on
                  • HansonH Hanson has marked this topic as solved on
                  • HansonH Hanson has marked this topic as unsolved on
                  • HansonH Offline
                    HansonH Offline
                    Hanson
                    wrote on last edited by
                    #16

                    Oh no!I found the qss also affect all the pages in the QTabWidget :(

                    “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                    —— Martin Golding

                    B 1 Reply Last reply
                    0
                    • HansonH Hanson

                      Oh no!I found the qss also affect all the pages in the QTabWidget :(

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by Bonnie
                      #17

                      @Hanson One way to solve this: for every widget added to the tabwidget, set autoFillBackground to true.
                      Or you can just dynamically set the qss, when there's no widget, clear the qss, when there's any, set the qss.

                      HansonH 1 Reply Last reply
                      1
                      • B Bonnie

                        @Hanson One way to solve this: for every widget added to the tabwidget, set autoFillBackground to true.
                        Or you can just dynamically set the qss, when there's no widget, clear the qss, when there's any, set the qss.

                        HansonH Offline
                        HansonH Offline
                        Hanson
                        wrote on last edited by
                        #18

                        @Bonnie Nice!It works.Thank you very much!

                        “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                        —— Martin Golding

                        1 Reply Last reply
                        0
                        • HansonH Hanson has marked this topic as solved on
                        • JonBJ JonB referenced this topic on

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved