Results 1 to 12 of 12

Thread: How to know the current tab selected

  1. #1
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default How to know the current tab selected

    I've created a qtabwidget with 4 tabs inside it, i need to know which current tab is
    currently selected by the user, the way i thought to do that was to have a call to the
    connect, something like this:

    connect( tab1pressed, SIGNAL(clicked()), this, SLOT( function_something1() ));
    connect( tab2pressed, SIGNAL(clicked()), this, SLOT( function_something2() ));
    .....

    Problem is i can't see what is the correct function to call to do the tab1pressed part,
    tabWidget->currentWidget() something? or there's a total different way to do this?
    Thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know the current tab selected

    QTabWidget offers a signal for this purpose: QTabWidget::currentChanged(int index).
    J-P Nurmi

  3. #3
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to know the current tab selected

    Quote Originally Posted by jpn View Post
    QTabWidget offers a signal for this purpose: QTabWidget::currentChanged(int index).

    Thanks, i tried to use the tabWidget->currentchanged as a signal in connect, like this:

    connect( tabWidget->currentWidget(), SIGNAL(tabWidget->currentChanged(1)), this, SLOT( function_something1() ));

    but it isn't working, maybe instead of tabWidget->currentWidget() i must use other thing?

  4. #4
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to know the current tab selected

    You must fix connect as following:
    connect( tabWidget, SIGNAL(tabWidget->currentChanged(int)), this, SLOT( function_something1() ));
    or
    connect( tabWidget, SIGNAL(tabWidget->currentChanged(int)), this, SLOT( function_something2(int) ));

    function_something1() used if you not need parameter in currentChanged(int)

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to know the current tab selected

    This won't work. This will:
    Qt Code:
    1. connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(activateTab(int)));
    2. //...
    3. void SomeClass::activateTab(int index){
    4. qDebug("%d tab selected", index);
    5. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to wysota for this useful post:

    Mrdata (6th February 2007)

  7. #6
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to know the current tab selected

    Well, actually both examples aren't working, it never enters the function_something/activatetab.


    More of my code
    Qt Code:
    1. My_tab::My_tab( QWidget *parent) : QDialog(parent)
    2. {
    3.  
    4. tabWidget = new QTabWidget;
    5. tabWidget->addTab( new Tab_1, tr("Tab_1") );
    6. tabWidget->addTab( new Tab_2, tr("Tab_2") );
    7. tabWidget->addTab( new Tab_3_Tab, tr("Tab_3") );
    8. tabWidget->addTab( new Tab_4, tr("Tab_4") );
    9.  
    10. //then i used one of connect examples
    11.  
    12.  
    13. connect( tabWidget, SIGNAL(tabWidget->currentChanged(int)), this, SLOT( function_something1() ));
    14.  
    15. //or
    16.  
    17. connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(activateTab(int)));
    18.  
    19.  
    20. mainLayout = new QVBoxLayout;
    21. mainLayout->setSizeConstraint( QLayout::SetFixedSize );
    22.  
    23. mainLayout->addWidget(tabWidget);
    24. setLayout(mainLayout);
    25. }
    26. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by Mrdata; 6th February 2007 at 18:35.

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know the current tab selected

    Please, use "[ code ]" tags around your code postings to make them readable. Have you declared the corresponding slot? Do you have the required Q_OBJECT macro?

    Qt Code:
    1. class My_tab ...
    2. {
    3. QOBJECT // <--
    4. ...
    5. private slots: // <--
    6. void activateTab(int idx); // <--
    7. ...
    8. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #8
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to know the current tab selected

    Quote Originally Posted by jpn View Post
    Please, use "[ code ]" tags around your code postings to make them readable. Have you declared the corresponding slot? Do you have the required Q_OBJECT macro?

    Qt Code:
    1. class My_tab ...
    2. {
    3. QOBJECT // <--
    4. ...
    5. private slots: // <--
    6. void activateTab(int idx); // <--
    7. ...
    8. };
    To copy to clipboard, switch view to plain text mode 
    Yes, i've the QOBJECT, and the activeTab defined as private slot.

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know the current tab selected

    So you're using Windows?
    • add "CONFIG += console" to the .pro file
    • re-run qmake & rebuild the application
    • run the application and see the command line for the reason why QObject::connect() fails
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    Mrdata (6th February 2007)

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to know the current tab selected

    Quote Originally Posted by Mrdata View Post
    connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(activateTab(int)));
    Probably it should be:
    Qt Code:
    1. connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(activateTab(int)));
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to jacek for this useful post:

    Mrdata (6th February 2007)

  14. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know the current tab selected

    It should be fine. There's an overload:
    QObject::connect ( const QObject * sender, const char * signal, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection ) const
    Equivalent to connect(sender, signal, this, method, type).
    J-P Nurmi

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to know the current tab selected

    True, somehow I always think that it's an equivalent of "connect(sender, signal, sender, slot, type)" (which obviously doesn't have much sense).

Similar Threads

  1. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 12:49
  2. Highlighting selected section on QDateTimeEdit
    By Yorma in forum Qt Programming
    Replies: 4
    Last Post: 3rd May 2013, 12:03
  3. Zooming a paricular selected region
    By Kapil in forum Newbie
    Replies: 8
    Last Post: 9th May 2006, 14:41
  4. How to get current row(column) index in the QTextTable?
    By denny.t in forum Qt Programming
    Replies: 3
    Last Post: 5th April 2006, 07:53
  5. Replies: 1
    Last Post: 23rd March 2006, 06:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.