PDA

View Full Version : How to know the current tab selected



Mrdata
6th February 2007, 14:41
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.

jpn
6th February 2007, 14:52
QTabWidget offers a signal for this purpose: QTabWidget::currentChanged(int index).

Mrdata
6th February 2007, 15:08
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?

vql
6th February 2007, 15:36
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)

wysota
6th February 2007, 15:58
This won't work. This will:

connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(activateTab(int)));
//...
void SomeClass::activateTab(int index){
qDebug("%d tab selected", index);
}

Mrdata
6th February 2007, 18:23
Well, actually both examples aren't working, it never enters the function_something/activatetab.


More of my code


My_tab::My_tab( QWidget *parent) : QDialog(parent)
{

tabWidget = new QTabWidget;
tabWidget->addTab( new Tab_1, tr("Tab_1") );
tabWidget->addTab( new Tab_2, tr("Tab_2") );
tabWidget->addTab( new Tab_3_Tab, tr("Tab_3") );
tabWidget->addTab( new Tab_4, tr("Tab_4") );

//then i used one of connect examples


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

//or

connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(activateTab(int)));


mainLayout = new QVBoxLayout;
mainLayout->setSizeConstraint( QLayout::SetFixedSize );

mainLayout->addWidget(tabWidget);
setLayout(mainLayout);
}
...

jpn
6th February 2007, 18:32
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?



class My_tab ...
{
QOBJECT // <--
...
private slots: // <--
void activateTab(int idx); // <--
...
};

Mrdata
6th February 2007, 18:40
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?



class My_tab ...
{
QOBJECT // <--
...
private slots: // <--
void activateTab(int idx); // <--
...
};


Yes, i've the QOBJECT, and the activeTab defined as private slot. :confused:

jpn
6th February 2007, 18:52
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

jacek
6th February 2007, 19:25
connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(activateTab(int)));
Probably it should be:
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(activateTab(int)));

jpn
6th February 2007, 19:41
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 (http://doc.trolltech.com/4.2/qobject.html#connect-2)


Equivalent to connect(sender, signal, this, method, type).

jacek
6th February 2007, 19:46
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 (http://doc.trolltech.com/4.2/qobject.html#connect-2)
True, somehow I always think that it's an equivalent of "connect(sender, signal, sender, slot, type)" (which obviously doesn't have much sense).