Results 1 to 8 of 8

Thread: Qt4/C++ - Connect signals & slots with a control aray index

  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt4/C++ - Connect signals & slots with a control aray index

    Hello,

    Linux - QT4

    I'm trying to get to grips with signals and slots.
    I am creating an array of subclassed QLineEdits
    What I would like to do is connect the array index to my slot, so I no longer need to search thru' the QObject sender to find the array index.
    I've done this in the past, but for the life of me can't remember how and can't find my notes, nor does several internet searches help.
    Any suggestions will be appreciated.

    Regards


    Qt Code:
    1. settings::settings(const QString &path, QWidget *parent) : QDialog(parent)
    2. {
    3. for (int i = 0; i < 9; i++) {
    4. e1[i] = new myLineEdit(g1);
    5. e1[i]->setFont(font);
    6. e1[i]->setGeometry(l1[i]->x() + l1[i]->width() + 4, l1[i]->y() -5, wDim[i], 25);
    7.  
    8. if (i > 4 && i < 9) QObject::connect(e1[i],SIGNAL(mySignal0(const int &)), this, SLOT(gpioType(int)));
    9. //only connect to 5, 6, 7 and 8
    10. }
    11. ...
    12. ...
    13. for (int i = 5; i < 9; i++) e1[i]->installEventFilter(this);
    14.  
    15. this->show();
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void settings::gpioType(int i)
    2. {
    3. QString test;
    4. int num;
    5. bool ok;
    6.  
    7. QObject *senderObj = sender();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. myLineEdit::myLineEdit(QWidget *parent) : QLineEdit(parent)
    2. {
    3. }
    4. ...
    5. ...
    6. void myLineEdit::focusOutEvent(QFocusEvent *e)
    7. {
    8. QLineEdit::focusOutEvent(e);
    9. emit(focussed(false));
    10. mySignal0(0);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MYLINEDIT_H
    2. #define MYLINEDIT_H
    3.  
    4. #include <QLineEdit>
    5.  
    6. class myLineEdit : public QLineEdit
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. myLineEdit(QWidget *parent = 0);
    12. ~myLineEdit();
    13.  
    14. signals:
    15. void focussed(bool hasFocus);
    16. void mySignal0(const int &i);
    17.  
    18. protected:
    19. virtual void focusOutEvent(QFocusEvent *e);
    20. };
    21.  
    22. #endif // MYLINEDIT_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt4/C++ - Connect signals & slots with a control aray index

    My suggestion would be to either include the pointer to the line edit as a signal argument, or set the index as a kind of "id" on the line edit and emit that id as another signal argument.

    Cheers,
    _

  3. #3
    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: Qt4/C++ - Connect signals & slots with a control aray index

    Or use QSignalMapper which will handle resolving the sender object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt4/C++ - Connect signals & slots with a control aray index

    Hello,

    Thanks to you both for your replies.
    In the end I went for setting an object name, seems easiest.
    anda_skoa pointed me in the right direction.
    wysota In this case Signal Mapping looks to be a little bit overkill, plus I don't know anything about it.
    If you think what I am doing is bad or wrong, please comment.

    Regards
    Qt Code:
    1. for (int i = 0; i < 9; i++) {
    2. e1[i] = new myLineEdit(g1);
    3. e1[i]->setFont(font);
    4. e1[i]->setGeometry(l1[i]->x() + l1[i]->width() + 4, l1[i]->y() -5, wDim[i], 25);
    5. if (i > 4 && i < 9) {
    6. QObject::connect(e1[i], SIGNAL(mySignal0(const int &)), this, SLOT(gpioType(int)));
    7. e1[i]->setObjectName(QString::number(i));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void settings::gpioType(int i)
    2. {
    3. QString test;
    4. int num;
    5.  
    6. num = (QObject::sender()->objectName()).toInt();
    7. qDebug() << "index - " << num;
    8. test = e1[num]->text();
    9. qDebug() << test;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Typical output =
    2. index - 8
    3. "me"
    4. Index - 6
    5. "you"
    To copy to clipboard, switch view to plain text mode 

  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: Qt4/C++ - Connect signals & slots with a control aray index

    Quote Originally Posted by jimbo View Post
    wysota In this case Signal Mapping looks to be a little bit overkill, plus I don't know anything about it.
    It does essentially the same thing you are doing (more or less) in a self-encapsulated way.

    Regarding your code, instead of these two lines:

    Qt Code:
    1. e1[i]->setObjectName(QString::number(i));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. num = (QObject::sender()->objectName()).toInt();
    To copy to clipboard, switch view to plain text mode 

    you can do this:

    Qt Code:
    1. e1[i]->setProperty("id", i);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. num = sender()->property("id").toInt();
    To copy to clipboard, switch view to plain text mode 

    which is a bit faster and doesn't interfere with the existing property.

    However in my opinion anda_skoa's advice about emitting a pointer to the widget is a more straightforward solution.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt4/C++ - Connect signals & slots with a control aray index

    How does your current approach differ from your original? You are still using QObject::sender(), which I thought you wanted to avoid?

    Cheers,
    _

  7. #7
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt4/C++ - Connect signals & slots with a control aray index

    Hello,

    if (i > 4 && i < 9) {
    QObject::connect(e1[i], SIGNAL(mySignal0(const int &)), this, SLOT(gpioType(int)));
    e1[i]->setProperty("id", i);
    }
    Adding 'setPProperty' and checking on the "ID" makes things easier for me.
    I just wanted to avoid having to iterate through every 'myLineEdit, sorry if I wasn't clear.
    I use quite a lot of 'myLineEdit' throughout the program.

    Thanks to you both.

    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt4/C++ - Connect signals & slots with a control aray index

    Ok, just in case:

    I guess you need the array index for a lookup into a different structure than the one holding the line edits.

    Because if you just wanted the line edit that sent the signal, the easiest way would be to cast the sender
    Qt Code:
    1. MyLineEdit *le = qobject_cast<MyLineEdit*>(QObject::sender());
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Difficult connect of signals and slots
    By Ozzy in forum Qt Programming
    Replies: 13
    Last Post: 5th February 2014, 06:38
  2. Replies: 2
    Last Post: 18th April 2013, 12:15
  3. Replies: 1
    Last Post: 28th January 2012, 12:35
  4. Replies: 16
    Last Post: 16th February 2010, 13:17
  5. Replies: 12
    Last Post: 23rd June 2008, 08:05

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.