You are not logged in.

1

Friday, August 24th 2012, 4:19am

Making slots for an array of QWidgets

I have several arrays of QWidgets as such:

Source code

1
QLineEdit *entries; /*retrieve size from file...*/ entries = new QLineEdit[size];


The problem is I don't know how to specify which entry is signaling slots. The first thing I thought of was to have just one function that every signal connects to and just have the signal pass the index of the array to the function, but I don't know how that would be done or if there is an alternative.

Q: How can I connect signals from an array of widgets to a function that knows exactly which widget interacted with?

Thanks for the help.

PS couldn't get source code to recognize new lines.

2

Saturday, August 25th 2012, 6:40pm

Anything?

_Blueman

Intermediate

  • "_Blueman" is male

Posts: 196

Location: Poland

  • Send private message

3

Saturday, August 25th 2012, 6:52pm

I don't have a lot of experience but my first idea was to use signal and slot with arguments [ documentation ] or instead arrays of QWidgets use QStackedWidget

4

Sunday, August 26th 2012, 10:13am

use QSignalMapper

eg:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
QList<QLineEdit *> edits;
// ...
QSignalMapper mapper = new QSignalMapper( this );
for( int i = 0; i < 10; ++i )
{
    QLineEdit * edit = new QLineEdit;
    edits << edit;
    connect( edit, SIGNAL(someSignal()), mapper, SLOT(map()) );
    mapper->setMapping( edit, edit );
    // ... 
    // perhaps set in a layout 
    // ...
}
connect( mapper, SIGNAL(mapped(QWidget*)), this, SIGNAL(on_edit_someSignal(QWidget*)) );

read QSignalMapper documentation for more information

5

Sunday, August 26th 2012, 5:19pm

QSignalMapper should do the trick. I'll check that out. I'm sure QList is simple enough so I'll do it the same way you have and see if it works for me. Thanks for the replies, I was really stuck on this.

6

Wednesday, August 29th 2012, 2:32am

I have the following as a single statement:

Source code

1
connect( mapper, SIGNAL(mapped(int)), this, SIGNAL(on_editEntry_textChanged(const QString &arg1, const int &i)) );

This, however, is in a loop that goes from i=0 to i=arrayLength:

Source code

1
2
connect( &editEntry[i], SIGNAL(textEdited(const QString &)), mapper, SLOT(map()) );
mapper->setMapping(&editEntry[i], i);

I declared this function in my window's class header:

Source code

1
on_editEntry_textChanged(const QString &arg1, const int & i);

I know I'm missing something. Does all the above code look right? I do not know how to send "i" to the function I created. Also, does that function need to be a public slot?

Any help would be appreciated. I'm very new to GUI stuff and googling things hasn't been helping.

This post has been edited 1 times, last edit by "dem7w2" (Aug 29th 2012, 3:03am)


7

Wednesday, August 29th 2012, 9:52am

you're missing to read documentation
QObject model: doc/html/object.html
and more specifically
signals and slots: doc/html/signalsandslots.html

8

Thursday, August 30th 2012, 7:58pm

In all due respect, I've read those. I really just need a TextChanged example for what I'm trying to do.

9

Friday, August 31st 2012, 8:07am

Quoted from "doc/html/signalsandslots.html#signals-and-slots"

The signals and slots mechanism is type safe:
The signature of a signal must match the signature of the receiving slot.

examples:
void signal( QString ) matches void signal( QString )
void signal( QString, int ) matches void signal( QString, int )
void signal( int ) doesn't match void signal( QString, int )
...

Quoted from "doc/html/signalsandslots.html#signals-and-slots"

(In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)

examples:
void signal( QString, int ) is compatible with void signal( QString )
void signal( QString, int ) is compatible with void signal( QString, int nb = 0 )
...
I have the following as a single statement:

Source code

1
connect( mapper, SIGNAL(mapped(int)), this, SIGNAL(on_editEntry_textChanged(const QString &arg1, const int &i)) );

here, it's obvious that
void QSignalMapper::mapped( int ) doesn't match and is not compatible with void YourClass::on_editEntry_textChanged( const QString &, const int & )

argument names must not be written when using SIGNAL and SLOT macros