PDA

View Full Version : Qt Creator - signals/slots problem



ctote
17th February 2010, 21:54
Hey everyone,

I'm using QtCreator to design my project. I need to send a datagram every 5 seconds out of a udpSocket. What i'm trying to do is this: When a user clicks the Enable button, it starts a timer - every 5 seconds the datagram is sent out the updSocket.

Here's my code -



void jaus::startBroadcasting()
{
connect(timer, SIGNAL(timeout()), this, SLOT(sendDatagram()));
ui->enableButton->setText("Disable");
ui->enableButton->setEnabled(false);
timer->start(5000);
}

void jaus::sendDatagram()
{
QByteArray message = "Hello.";
udpSocket->writeDatagram(message, QHostAddress::LocalHost, 9000);
}

I have the Enable button anchored to the startBroadcasting() slot. But when I click the button, the program blows up -- it doesn't do that when I take out the connect command and the timer->start() command.

Thanks for any help

Lykurg
17th February 2010, 22:12
have you initialised the timer correctly? and be aware that if you call startBroadcasting twice, sendDatagram will be called twice every 5 seconds. So better put the connect statement in the c-tor where you initialise your timer.

pitonyak
18th February 2010, 03:03
You may have to learn to do multiple things. For example:

Deal with a timer calling a slot
Deal with UDP messages

If you are not absolutely certain that you have the UDP thing working, see what happens if you replace your UDP code with qDebug statements. In other words, try to test one thing at a time for your initial code.