PDA

View Full Version : Qt4/C++ - Connect signals & slots with a control aray index



jimbo
28th April 2015, 15:33
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



settings::settings(const QString &path, QWidget *parent) : QDialog(parent)
{
for (int i = 0; i < 9; i++) {
e1[i] = new myLineEdit(g1);
e1[i]->setFont(font);
e1[i]->setGeometry(l1[i]->x() + l1[i]->width() + 4, l1[i]->y() -5, wDim[i], 25);

if (i > 4 && i < 9) QObject::connect(e1[i],SIGNAL(mySignal0(const int &)), this, SLOT(gpioType(int)));
//only connect to 5, 6, 7 and 8
}
...
...
for (int i = 5; i < 9; i++) e1[i]->installEventFilter(this);

this->show();
}

void settings::gpioType(int i)
{
QString test;
int num;
bool ok;

QObject *senderObj = sender();

}

myLineEdit::myLineEdit(QWidget *parent) : QLineEdit(parent)
{
}
...
...
void myLineEdit::focusOutEvent(QFocusEvent *e)
{
QLineEdit::focusOutEvent(e);
emit(focussed(false));
mySignal0(0);
}

#ifndef MYLINEDIT_H
#define MYLINEDIT_H

#include <QLineEdit>

class myLineEdit : public QLineEdit
{
Q_OBJECT

public:
myLineEdit(QWidget *parent = 0);
~myLineEdit();

signals:
void focussed(bool hasFocus);
void mySignal0(const int &i);

protected:
virtual void focusOutEvent(QFocusEvent *e);
};

#endif // MYLINEDIT_H

anda_skoa
28th April 2015, 17:19
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,
_

wysota
28th April 2015, 17:36
Or use QSignalMapper which will handle resolving the sender object.

jimbo
28th April 2015, 20:33
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

for (int i = 0; i < 9; i++) {
e1[i] = new myLineEdit(g1);
e1[i]->setFont(font);
e1[i]->setGeometry(l1[i]->x() + l1[i]->width() + 4, l1[i]->y() -5, wDim[i], 25);
if (i > 4 && i < 9) {
QObject::connect(e1[i], SIGNAL(mySignal0(const int &)), this, SLOT(gpioType(int)));
e1[i]->setObjectName(QString::number(i));
}
}
void settings::gpioType(int i)
{
QString test;
int num;

num = (QObject::sender()->objectName()).toInt();
qDebug() << "index - " << num;
test = e1[num]->text();
qDebug() << test;
Typical output =
index - 8
"me"
Index - 6
"you"

wysota
28th April 2015, 21:42
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:


e1[i]->setObjectName(QString::number(i));

num = (QObject::sender()->objectName()).toInt();

you can do this:


e1[i]->setProperty("id", i);

num = sender()->property("id").toInt();

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.

anda_skoa
29th April 2015, 06:32
How does your current approach differ from your original? You are still using QObject::sender(), which I thought you wanted to avoid?

Cheers,
_

jimbo
29th April 2015, 14:37
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

anda_skoa
29th April 2015, 15:48
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


MyLineEdit *le = qobject_cast<MyLineEdit*>(QObject::sender());


Cheers,
_