Results 1 to 6 of 6

Thread: accessing current index in QComboBox

  1. #1
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default accessing current index in QComboBox

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: accessing current index in QComboBox

    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing current index in QComboBox

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


    so in the function
    Qt Code:
    1. void SignalTimeDomainPlot::CurrentIndex(int index)
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 

    the index will return u the comboBox index every time u change the combobox value.
    use the index value .
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: accessing current index in QComboBox

    @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:
    Qt Code:
    1. 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?

    As you said, if its in the same file, then it would be written as:
    Qt Code:
    1. connect(mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)), this, SLOT(CurrentIndex(int)));
    To copy to clipboard, switch view to plain text mode 
    and in its header file-

    private slots:
    void currentIndex(int);


    Please clarify, thanks

  5. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing current index in QComboBox

    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
    Qt Code:
    1. #include <QWidget>
    2. #include <QComboBox>
    3.  
    4. class M_Signal : public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. explicit M_Signal(QWidget *parent = 0);
    9. QComboBox *mComboBoxSigSelect;
    10. signals:
    11. void IndexChanged(int);
    12.  
    13. public slots:
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 
    its cpp
    Qt Code:
    1. #include "m_signal.h"
    2.  
    3. M_Signal::M_Signal(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. mComboBoxSigSelect = new QComboBox(this);
    7. mComboBoxSigSelect->addItem("Hai");
    8. mComboBoxSigSelect->addItem("Hello");
    9. mComboBoxSigSelect->addItem("Way");
    10. mComboBoxSigSelect->addItem("good");
    11. connect(mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)), this, SIGNAL(IndexChanged(int)));
    12. }
    To copy to clipboard, switch view to plain text mode 

    M_Class Object
    Qt Code:
    1. #include "m_signal.h"
    2.  
    3. class M_Class : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. M_Class(QWidget *parent =0);
    9.  
    10. private slots:
    11. void currentIndexChanged(int);
    12.  
    13. private:
    14. M_Signal *m_signal;
    15. int mCurrentIndex;
    16. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "m_class.h"
    2. #include <QDebug>
    3.  
    4. M_Class::M_Class(QWidget *parent) : QWidget(parent)
    5. {
    6. m_signal = new M_Signal(this);
    7. /* 0ne way*/
    8. connect(m_signal->mComboBoxSigSelect, SIGNAL(currentIndexChanged(int)),this , SLOT(currentIndexChanged(int)));
    9. /*Another way */
    10. connect(m_signal, SIGNAL(IndexChanged(int)),this , SLOT(currentIndexChanged(int)));
    11. }
    12.  
    13. void
    14. M_Class::currentIndexChanged(int index)
    15. {
    16. qDebug()<<"Index value returns:"<<index;
    17. }
    To copy to clipboard, switch view to plain text mode 

    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)));
    Last edited by wagmare; 28th August 2013 at 09:38.
    "Behind every great fortune lies a crime" - Balzac

  6. #6
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: accessing current index in QComboBox

    @wagmare: Thanks a lot for your example. It was really helpful! :-)

Similar Threads

  1. How to get Current index value in QlistView
    By boopathi33 in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2013, 09:08
  2. Replies: 4
    Last Post: 1st March 2013, 13:00
  3. QListView current row index?
    By noborder in forum Newbie
    Replies: 1
    Last Post: 3rd February 2013, 19:21
  4. How to get current focus index in QML ???
    By duc_bkav in forum Qt Programming
    Replies: 0
    Last Post: 25th November 2011, 05:28
  5. How to get current row(column) index in the QTextTable?
    By denny.t in forum Qt Programming
    Replies: 3
    Last Post: 5th April 2006, 08:53

Tags for this Thread

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.