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. Connect to QTabWidget's tabBar()
Forum Updated to NodeBB v4.3 + New Features

Connect to QTabWidget's tabBar()

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.6k 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.
  • U Offline
    U Offline
    uranusjr
    wrote on last edited by
    #1

    Hi,

    I am trying to build a widget containing a tabbed view, like a web browser. I thought QTabWidget would be a nice choice, and went on built one. According to the docs, if I want the tabs to be closable, I need to call setTabsClosable(true), and then connect the tab bar's tabCloseRequested(int) to somewhere, and then handle the close event.

    Now the problem: The compiler refuse to accept the connecting statement.

    @
    error: no matching function for call to 'UJ::Qelly::TabWidget::connect(QTabBar*, const char*, UJ::Qelly::TabWidget* const, const char*)'
    /usr/local/Cellar/qt/4.7.4/include/QtCore/qobject.h:209: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
    /usr/local/Cellar/qt/4.7.4/include/QtCore/qobject.h:314: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
    make: *** [TabWidget.o] Error 1
    @

    I just don't see the problem. UJ::Qelly::TabWidget is a subclass of QTabWidget, which is a subclass of QObject, is it not? And then I tried this:

    @
    connect((QObject *)tabBar(), SIGNAL(tabCloseRequested(int)),
    this, SLOT(onTabCloseRequested(int)));
    @

    And the compiler shut up. And everything worked (at least everything I tested worked). But this does not seem to be a very nice solution (not exactly comfortable with C-style type casting, you know). Am I doing something wrong here? What should I do to connect the signal correctly?

    Thanks.

    (TabWidget.h)
    @
    #ifndef TABWIDGET_H
    #define TABWIDGET_H

    #include <QTabWidget>
    class QTabBar;

    namespace UJ
    {

    namespace Qelly
    {

    class TabWidget : public QTabWidget
    {
    Q_OBJECT

    public:
    explicit TabWidget(QWidget *parent = 0);

    signals:
    void tabWillClose(QWidget *tab);

    private slots:
    void onTabCloseRequested(int index);

    };

    } // namespace Qelly

    } // namespace UJ

    #endif // TABWIDGET_H
    @

    TabWidget.cpp
    @
    #include "TabWidget.h"

    namespace UJ
    {

    namespace Qelly
    {

    TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent)
    {
    setDocumentMode(true);
    setTabsClosable(true);
    connect(tabBar(), SIGNAL(tabCloseRequested(int)),
    this, SLOT(onTabCloseRequested(int)));
    }

    void TabWidget::onTabCloseRequested(int index)
    {
    QWidget *w = widget(index);
    removeTab(index);
    emit tabWillClose(w);
    w->deleteLater();
    }

    } // namespace Qelly

    } // namespace UJ

    @

    1 Reply Last reply
    0
    • U Offline
      U Offline
      uranusjr
      wrote on last edited by
      #2

      I just found that QTabWidget also has tabCloseRequested(int), and connecting to this one (instead of QTabBar's signal) solves this particular problem. But I still don't understand why the initial code causes an error.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        You're missing

        @
        #include <QTabBar>
        @

        in your TabWidget.cpp file.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • U Offline
          U Offline
          uranusjr
          wrote on last edited by
          #4

          You are correct. I'm such an idiot. LOL

          Thanks!

          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