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. Work with QTabWidget
Forum Updated to NodeBB v4.3 + New Features

Work with QTabWidget

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 14.9k Views 1 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.
  • F Offline
    F Offline
    fomikadze
    wrote on last edited by
    #1

    Hi! I'm developing the simple application and use QTabWidget in it. I want to make the dynamic adding/closing of the tabs. With setTabsClosable() - I've add the close buttons on the tabs, but the don't react on clicks. How can I add signal and slot to that buttons?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      QTabWidget emits the tabCloseRequested() signal, which can be connected to to actually remove the tab using removeTab(). Be aware that the page widget itself is not deleted automatically.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fomikadze
        wrote on last edited by
        #3

        Thanks
        And is there any ways to get the number of the tab, on which close button was presse, or I need to figure it by my own?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          "QTabWidget::tabCloseRequested()":http://qt-project.org/doc/qt-4.8/qtabwidget.html#tabCloseRequested

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fomikadze
            wrote on last edited by
            #5

            I've added the tabCloseRequested() signal to the removeTab() slot, but I don't understand how it works. Because when I write
            @connect(tabWidget, SIGNAL(tabCloseRequested(index)), this, SLOT(removeTab(index)));@
            where "index" is the index of the page, it gives me the mistake "no such SIGNAL tabCloseRequested(index)", but when I live as
            @connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int)));@
            it woks. But now when I press on close button, it closes all tabs that follows the tab I'm closing.
            How does it work?

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              A signal, as well as a slot, is always identified by its signature, <code>tabCloseRequested(int)</code>. You cannot pass actual values, <code>tabCloseRequested(index)</code>.

              Although beeing ordinary methods, slots have to be marked as such, <code>public slots:</code>, which is not true for QTabWidget::removeTab() and you cannot connect to.

              The initial post might have been a bit misleading. You can connect to the tabCloseRequested() signal, but not to removeTab(), due to not beeing a slot.

              The common use case is to add a (private) slot to the embracing class, which is responsible for closing the tab and deleting the page widget.
              @
              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              MainWindow() : QMainWindow()
              {
              tabWidget_ = new QTabWidget;
              tabWidget_->addTab(new QLabel("TabA"), "TabA");
              tabWidget_->addTab(new QLabel("TabB"), "TabB");
              tabWidget_->addTab(new QLabel("TabC"), "TabC");
              tabWidget_->setTabsClosable(true);

                  connect(tabWidget_, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab_(int)));
              
                  setCentralWidget(tabWidget_);
              }
              

              private slots:
              void closeTab_(int index)
              {
              // Remove the tab using removeTab(). Be aware that the
              // page widget itself is not deleted!
              tabWidget_->removeTab(index);

                  // OR (do NOT do both)
              
                  // Delete the page widget, which automatically removes
                  // the tab as well.
                  delete tabWidget_->widget(index);
              }
              

              private:
              QTabWidget *tabWidget_;
              };
              @

              1 Reply Last reply
              1
              • F Offline
                F Offline
                fomikadze
                wrote on last edited by
                #7

                I did that, but when I delete one tab, following tabs are deleted too!
                I don't understand why such thing appears. I have almost the same code as you, but the promblome is still there. Maybe I miss some thing?

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fomikadze
                  wrote on last edited by
                  #8

                  I've found the problem! ;)
                  @tabWidget_->removeTab(index);@ - here we remove the tab from the tabWidget_, and indexes of tabs are updated. Now we have anothe tab on the plase of the removed one.
                  So, when I was trying to delete the page widget itself, in fact I was deleting the next tab. That on that moment was on the place of the removed tab.

                  Thanks for help!!!

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    Well, that's what I meant with <code>do NOT do both</code> ;-)

                    You're welcome.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      deepak.jangra
                      wrote on last edited by
                      #10

                      Hi all,

                      I am using tab widget in my application and I need to update the tab text color in order to show notification to the user. But I dint any way to do this. My requirement is to show alert to the user for the tab which is not currently selected. I have try many things to implement this behaviour but get failed every time. I have also use the qtabbar stylesheet but even that not provide any way to update the random tab style as there are only limited stated provided by the QTabbar stylesheet.
                      I also find a method setTabTextColor in QTabbar class but dont find any way to use it. Please suggest me the way to implement the desired behaviour as I have spent much time on this.

                      Thanks in advance
                      Deepak Jangra

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        QTabWidget::tabBar() (and thus QTabBar::setTabTextColor()) is protected, so you'll need to subclass QTabWidget in order to make it publicly available.
                        @
                        class TabWidget : public QTabWidget
                        {
                        public:
                        TabWidget(QWidget *parent = 0) : QTabWidget(parent) {}

                        void setTabTextColor(int index, const QColor &color)
                        {
                            tabBar()->setTabTextColor(index, color);
                        }
                        

                        };

                        ...

                        TabWidget *tabWidget = new TabWidget;
                        tabWidget->setTabTextColor(0, Qt::red);
                        @

                        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