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
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