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. Access tab created in runtime - Qt C++
Forum Updated to NodeBB v4.3 + New Features

Access tab created in runtime - Qt C++

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 1.1k Views
  • 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

    @HenkCoder
    Your code assumes that after QTabWidget::addTab(QWidget *page, const QString &label) the added page becomes the currentWidget() ("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 of QTextEdit.

    H Offline
    H Offline
    HenkCoder
    wrote on last edited by
    #4

    @JonB Oh but I don't need the TextEdit, I need to access the tab.

    JonBJ 1 Reply Last reply
    0
    • H HenkCoder

      @JonB Uhh no, how do I do that? Like a if(ui-tabWidget->currentWidget()->metaObject()->className == "QTab")?

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

      @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);
      
      1 Reply Last reply
      0
      • H HenkCoder

        @JonB Oh but I don't need the TextEdit, I need to access the tab.

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

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

        H 1 Reply Last reply
        1
        • JonBJ JonB

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

          H Offline
          H Offline
          HenkCoder
          wrote on last edited by
          #7

          @JonB Yes, I want to set the background color of the tab itself, sorry if I explained myself badly.

          JonBJ 1 Reply Last reply
          0
          • H HenkCoder

            @JonB Yes, I want to set the background color of the tab itself, sorry if I explained myself badly.

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

            @HenkCoder
            Ah! Then accessing the currentWidget() or the TextEdit 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.

            H 2 Replies Last reply
            1
            • JonBJ JonB

              @HenkCoder
              Ah! Then accessing the currentWidget() or the TextEdit 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.

              H Offline
              H Offline
              HenkCoder
              wrote on last edited by
              #9

              @JonB Oh right! Let me try with stylesheets.

              1 Reply Last reply
              0
              • JonBJ JonB

                @HenkCoder
                Ah! Then accessing the currentWidget() or the TextEdit 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.

                H Offline
                H Offline
                HenkCoder
                wrote on last edited by
                #10

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

                JonBJ 1 Reply Last reply
                0
                • H HenkCoder

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

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

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

                  H 1 Reply Last reply
                  1
                  • C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #12

                    @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);
                    ...
                    
                    H 1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      H Offline
                      H Offline
                      HenkCoder
                      wrote on last edited by
                      #13

                      @JonB
                      Okay, I'll explain better, I need to access the tab:
                      3898c336-37c4-48e6-a292-bcb054dc79f0-image.png
                      behind the textEdit, not where the tab name is.

                      JonBJ 1 Reply Last reply
                      0
                      • C ChrisW67

                        @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);
                        ...
                        
                        H Offline
                        H Offline
                        HenkCoder
                        wrote on last edited by HenkCoder
                        #14

                        @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);
                        
                        1 Reply Last reply
                        0
                        • H HenkCoder

                          @JonB
                          Okay, I'll explain better, I need to access the tab:
                          3898c336-37c4-48e6-a292-bcb054dc79f0-image.png
                          behind the textEdit, not where the tab name is.

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

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

                          1 Reply Last reply
                          0

                          • Login

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