PDA

View Full Version : accessing current index in QComboBox



uz_qt
27th August 2013, 18:24
Hi all,

I would like to have access to the index of the item selected from the QComboBox.

I have written the following line:

mComboBoxSigSelect = new QComboBox(this);
connect(mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)), this, SIGNAL(CurrentIndex()));

The above lines are in a file code1.cpp and I need to have access to the index of the item chosen by the user in another file Code2.cpp.

I have mentioned the following lines code1.h (header file for code1.cpp):

class My_Signals : public QWidget
{
Q_OBJECT

public:

signals:
int CurrentIndex();

public slots:

private:

};

In Code2.cpp, i have the following lines:
My_constructor::My_constructor
{
.
.
.
int k=CurrentIndex(); // Want to save the index value from Code1.cpp into the variable 'k'
}

int SignalTimeDomainPlot::CurrentIndex()
{
// code ??
}



And in Code2.h (header file for Code2.cpp), I have written:

class My_Code : public QWidget
{
Q_OBJECT

public:

signals:

public slots:

int CurrentIndex();

private:

};


Please help! Thanks

ChrisW67
27th August 2013, 22:02
The signal emitted by the combo box carries the integer index of the current item ( or -1 ) to your slot when it changes. Your slot must be declared with a parameter to receive it... Yours is not. There are other problems with your code but that is a place to start.

wagmare
28th August 2013, 05:33
in ur code


signals:
int CurrentIndex();
it should be
private slots:
void CurrentIndex(int)


so in the function

void SignalTimeDomainPlot::CurrentIndex(int index)
{
}

the index will return u the comboBox index every time u change the combobox value.
use the index value .

uz_qt
28th August 2013, 07:24
@wagmare:

Thank you.

Since I wanted to have access to the index in another file (code2.cpp), i have written this line in code1.cpp:


connect(mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)), this, SIGNAL(CurrentIndex(int)));

and mentioned

signals:
int CurrentIndex(int);

in its header so that I can emit the signal from code1.cpp.
Is it not right?

As you said, if its in the same file, then it would be written as:


connect(mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)), this, SLOT(CurrentIndex(int)));
and in its header file-

private slots:
void currentIndex(int);

Please clarify, thanks

wagmare
28th August 2013, 09:38
Since I wanted to have access to the index in another file (code2.cpp), i have written this line in code1.cpp:
Qt Code:
Switch view

connect(mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)), this, SIGNAL(CurrentIndex(int)));

To copy to clipboard, switch view to plain text mode

and mentioned

signals:
int CurrentIndex(int);

in its header so that I can emit the signal from code1.cpp.
Is it not right?
right and should work
but how ur integrating two objects code1 and code2
My_Signals and My_Class how u are calling it.
if ur calling the two classes in a seperate class then
u can do

connect(My_Signals->mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)),My_Class , SLOT(CurrentIndex(int)));

if u r calling M_Signals object in M_Class then

connect(My_Signals->mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)),this , SLOT(CurrentIndex(int)))

is it clear to u ..?

Added after 26 minutes:

signal code

#include <QWidget>
#include <QComboBox>

class M_Signal : public QWidget
{
Q_OBJECT
public:
explicit M_Signal(QWidget *parent = 0);
QComboBox *mComboBoxSigSelect;
signals:
void IndexChanged(int);

public slots:

};
its cpp

#include "m_signal.h"

M_Signal::M_Signal(QWidget *parent) :
QWidget(parent)
{
mComboBoxSigSelect = new QComboBox(this);
mComboBoxSigSelect->addItem("Hai");
mComboBoxSigSelect->addItem("Hello");
mComboBoxSigSelect->addItem("Way");
mComboBoxSigSelect->addItem("good");
connect(mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)), this, SIGNAL(IndexChanged(int)));
}

M_Class Object

#include "m_signal.h"

class M_Class : public QWidget
{
Q_OBJECT

public:
M_Class(QWidget *parent =0);

private slots:
void currentIndexChanged(int);

private:
M_Signal *m_signal;
int mCurrentIndex;
};



#include "m_class.h"
#include <QDebug>

M_Class::M_Class(QWidget *parent) : QWidget(parent)
{
m_signal = new M_Signal(this);
/* 0ne way*/
connect(m_signal->mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)),this , SLOT(currentIndexChanged(int)));
/*Another way */
connect(m_signal, SIGNAL(IndexChanged(int)),this , SLOT(currentIndexChanged(int)));
}

void
M_Class::currentIndexChanged(int index)
{
qDebug()<<"Index value returns:"<<index;
}


this way im calling M_Signal inside my M_Class
if u want a seperate Object to connect this both say M_MainWindow
then
connect(My_Signals, SIGNAL(IndexChanged(int)),My_Class , SLOT(CurrentIndex(int)));

uz_qt
29th August 2013, 16:05
@wagmare: Thanks a lot for your example. It was really helpful! :-)