PDA

View Full Version : wrong connection? program crashes altough it compiles



cbarmpar
30th September 2008, 02:24
I am trying to display a lineedit for 3 seconds and then make it disappear. The problem is that altough the code compiles properly the program crasses when i run it and sends the following error:


Object::connect: No such slot kentriko::diagrafi(grami2)
Object::connect: (receiver name: 'kentrikoClass')

the code:


#ifndef KENTRIKO_H
#define KENTRIKO_H

#include <QtGui/QMainWindow>
#include "ui_kentriko.h"

class kentriko : public QMainWindow
{
Q_OBJECT

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

public slots:
void anixe();
void diagrafi(QLineEdit* grami2);

private:
Ui::kentrikoClass ui;
};

#endif // KENTRIKO_H


and the implementation:
#include "kentriko.h"
#include <QTimer>
kentriko::kentriko(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.grami,SIGNAL(textChanged ( const QString &)),this,SLOT(anixe()));
//
}

void kentriko::anixe(){
QLineEdit* grami2 = new QLineEdit(this);
grami2->setObjectName(QString::fromUtf8("grami2"));
grami2->setGeometry(QRect(60, 80, 180, 25));
grami2->show();
QTimer::singleShot(3000, this, SLOT(diagrafi(grami2 )));
ui.label->setText("kalispera");
}

void kentriko::diagrafi(QLineEdit* grami2){
grami2->hide();
}


Any ideas about why is the program crasing when the timer expires?

Many thanks in advance.
Regards.

QbelcorT
30th September 2008, 03:12
Hi,
I have had this happen many times. The QTcore cannot find the signal/slot that is requested. It cannot tell this during compile time, so it will warn you later during the debug of what went wrong. It is telling you that it cannot find the slot in your class (diagrafi).
Object::connect: No such slot kentriko::diagrafi(grami2)
Object::connect: (receiver name: 'kentrikoClass')
Is there another object that is using this slot? maybe the other object cannot see this slot?

caduel
30th September 2008, 09:03
If QTimer emitted a signal with a QLineEdit* paran, then (and only then)

wrong

QTimer::singleShot(3000, this, SLOT(diagrafi(grami2 )));

right (not really right, because of big if)

QTimer::singleShot(3000, this, SLOT(diagrafi(QLineEdit*)));


Alas, it does not emit a signal with such a param.
Therefore:
right:

QTimer::singleShot(3000, this, SLOT(diagrafi()));

void kentriko::anixe()
{
grami2 = new QLineEdit(this); // provide a member variable grami2
grami2->setObjectName(QString::fromUtf8("grami2"));
grami2->setGeometry(QRect(60, 80, 180, 25));
grami2->show();
QTimer::singleShot(3000, this, SLOT(diagrafi()));
ui.label->setText("kalispera");
}
void kentriko::diagrafi()
{
grami2->hide();
}



HTH

lyuts
30th September 2008, 11:38
Your connect is incorrect. The documentation says
"The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)"

caduel
30th September 2008, 11:46
exactly. and what is the signature of QTimer::timeout()....?
I do not see a QLineEdit there... (and shorter than no arguments at all is usually a bit of a problem, too).

cbarmpar
30th September 2008, 13:08
caduel and lyuts many thanks for the reply.

It appears that you are right caduel. The only way to do that is to make qlineedit object visible to the entire scope of the class and then hide it without passing anyting from the slot.

But really if one cant pass arguments through the signal slot mechanism then this is a big mistake. I am suprised that it hasnt been mentioned before.

Are we sure that this is hot it works. From my reading we are right.

Regards.

caduel
30th September 2008, 13:27
You can pass arguments... when invoking a signal: the signal has to emit the arguments.
You can not define values (for a slot's arguments) when creating the connection.

(If you want to do that: you can use Boost.Signals and 'bind' the constants to your function. See www.boost.org; Boost.Signals and Qt signals are a similar concept and may be used in parallel. You have to deacivate Qt keyword with CONFIG += no_keywords in your .pro file, otherwise the keyword "signals" and the Boost signals headers are in conflict.)

cbarmpar
30th September 2008, 13:48
thank for the reply.

I just had a look on the boost library.
Is it a cross platform library?
For my case how can i use the bind function to pass arguments to the slot?
Can i use both the connect and the boost::bind function on the same code?

Can you please modify the code in order to use the boos::bind insted of the connect?

Many thanks in advance.