You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

law

Unregistered

1

Friday, July 9th 2004, 11:39pm

QTimer and slot parameter

I have a (silly) question : is it possible to use QTimer method to call a slot with parameters, or is this feature only usable with no paramater slots?

here is an example:

QTimer *timer = new QTimer( this );
connect( timer, SIGNAL(timeout()), this, SLOT(playAudio( int 3 )) );
timer->start( j*1000, TRUE );

void MilAlp::playAudio( int test )
{

}

wont work :-(

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

2

Saturday, July 10th 2004, 10:07am

RE: QTimer and slot parameter

You should do it somewhat differently:
In class definition:

Source code

1
2
3
4
5
6
class MilAlp
{
	// standard stuff
public slots:
    void playAudio( int what = 0 ) ;
} ;


Implementation

Source code

1
2
3
4
5
6
7
8
9
10
11
12
void MilAlp::someRoutine() 
{
	// some code
    QTimer *timer = new QTimer( this, "MilTimer" ) ;
    connect( timer, SIGNAL(timeout() ), this, SLOT( playAudio() ) ) ;
    timer->start( j*1000, TRUE ) ;
}
 
 void MilAlp::playAudio( int what )
 {
     // start player with something ;-)
 }


A signal without parameter can call a slot with parameters when those have default values.

If your ausio player can be called from various places and act differently, you could also have:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
 void MilAlp::playAudio( int what )
 {
    int shouldPlay = what ;
    if ( shouldPlay == 0 ) {
	// called from a signal without parms.
	QString widname = QObject::sender()->name() ;
	if ( widname == "MilTimer" ) 
	    shouldPlay = 3 ;
	else if ( widname == "someOtherName" )
	    shouldPlay = 4 ;
    	// start player with something ;-)
 }
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

law

Unregistered

3

Tuesday, July 13th 2004, 7:28pm

thanks for your help.

but i have some droubles with your code i always get this error if i push the play button.

QObject::connect: No such slot MilAlp::playAudio()
QObject::connect: (sender name: 'MilTimer')
QObject::connect: (receiver name: 'MilAlpBase')
QObject::connect: No such slot MilAlp::playAudio()
QObject::connect: (sender name: 'MilTimer')
QObject::connect: (receiver name: 'MilAlpBase')
QObject::connect: No such slot MilAlp::playAudio()
QObject::connect: (sender name: 'MilTimer')
QObject::connect: (receiver name: 'MilAlpBase')
QObject::connect: No such slot MilAlp::playAudio()
QObject::connect: (sender name: 'MilTimer')
QObject::connect: (receiver name: 'MilAlpBase')

and here is my code:

my header file:
....
private slots:
void playAudio( int what = 0 ) ;
....
my cpp file:
.....
void MilAlp::cutString()
{
const QString inputMSG = LineEdit2->text();

unsigned int i;
unsigned int j = 0;

MultiLineEdit1->setText("");

for(i=0; i<inputMSG.length(); i++)
{
// 1 minute
QTimer *timer = new QTimer( this, "MilTimer" );
connect( timer, SIGNAL(timeout() ), this, SLOT( playAudio()) );
timer->start( j*1000, TRUE );

j = j + 10;
}

}

void MilAlp::playAudio( int what )
{
int shouldPlay = what ;
if ( shouldPlay == 0 )
{
QString widname = QObject::sender()->name() ;
if ( widname == "A" )
{
MultiLineEdit1->insert("hehe");
}
else
{
MultiLineEdit1->insert("nooo");
}
}
}
....

ist it also possible do get the value of the inputMSG[i] variable do the playaudio function?

thanks for you help