PDA

View Full Version : signal not getting communicated to slot



quickNitin
1st June 2006, 09:48
hi
i am currently doing a dialog in which i created 2 check-box, Ok and Cancel button. On press of Ok button 3 signals are emitted communicating state of choices on dialog as shoown below(class is named CockGui). Through this signal i want to choose whether to put my pic on canvas on not. My problem is slot to which signal is connected is not getting executed while there is no reporting of error.


/*in cockgui.cpp*/
void CockGui::pbnOK_clicked()
{
hide();
emit enableCock(cBoxAllow->isChecked());
emit enableCockAgain(cBoxAgain->isChecked());
emit needToRefresh();
done(1);
}


These are connected to slots of different class Cock which uses CockGui.



void Cock::run()
{
CockGui *myPluginGui=new CockGui(mQGisApp, QgisGui::ModalDialogFlags);
//listen for when the layer has been made so we can draw it
myPluginGui->setBox(mEnable);
connect(myPluginGui, SIGNAL(drawRasterLayer(QString)), this, SLOT(drawRasterLayer(QString)));
connect(myPluginGui, SIGNAL(drawVectorLayer(QString,QString,QString)), this, SLOT(drawVectorLayer(QString,QString,QString)));
connect(myPluginGui,SIGNAL(enableCock(bool)),this, SLOT(setEnabled(bool)));
connect(myPluginGui, SIGNAL(needToRefresh()), this, SLOT(refreshCanvas()));
connect(myPluginGui,SIGNAL(enableCockAgain(bool)), this,SLOT(setEnabled(bool)));
// refreshCanvas();
myPluginGui->show();
}


void Cock::setEnabled(bool theBool)
{
mEnable=theBool;
QgsProject::instance()->writeEntry("Cock","/Enabled", mEnable );
if(mEnable)
cout<<"\nEnable is enabled";
else
cout<<"\n not enabled";
}

Here none of the cout statement are being displayed on terminal. I traced down to it. Slot setEnabled is not being called so whatever was inital state of mEnable remains.
Code which will render output on canvas is...

void Cock::renderCock(QPainter *theQPainter)
{
if (mEnable)
{
QPixmap myQPixmap; //to store the north arrow image in
QString myFileNameQString= "/home1/qgis/images/cock/cock.png";
if (myQPixmap.load(myFileNameQString))
{

double centerXDouble = myQPixmap.width()/2;
double centerYDouble = myQPixmap.height()/2;
theQPainter->save();

int myHeight = theQPainter->device()->height();
int myWidth = theQPainter->device()->width();

#ifdef QGISDEBUG
std::cout << "Rendering n-arrow at " << mPlacementLabels.at(mPlacementIndex).toLocal8Bit() .data() << std::endl;

theQPainter->setRenderHint(QPainter::SmoothPixmapTransform);
theQPainter->drawPixmap(25,25,myQPixmap);
//unrotate the canvas again
theQPainter->restore();
cout<<"\n rendered";
}
else
{
QFont myQFont("time", 32, QFont::Bold);
theQPainter->setFont(myQFont);
theQPainter->setPen(Qt::black);
theQPainter->drawText(10, 30, QString(tr("Pixmap1 Not Found")));
}
}

else
cout<<"\nCock Not enabled";

}

Everytime i refresh i gets "Cock Not enabled"

i couldn't where problem lies. Please help me in it

zlatko
1st June 2006, 10:19
Have you declare Q_OBJECT in header file ?

quickNitin
1st June 2006, 10:32
yes i had done that, if you want i can put header file code also

munna
1st June 2006, 10:44
In the header file did you put the signals after

signals:

and the slots after

private slots:

or

public slots:

quickNitin
1st June 2006, 10:52
Here is my header file for Cock class

#ifndef Cock_H
#define Cock_H

//QT4 includes
#include <QObject>
#include<QPainter>
//QGIS includes
#include <qgisapp.h>
#include "../qgisplugin.h"


class QToolBar;

class Cock:public QObject, public QgisPlugin
{
Q_OBJECT
public:
Cock(QgisApp * theApplication, QgisIface * theInterface);
//! Destructor
virtual ~Cock();
public slots:
virtual void initGui();
void run();
void unload();
void help();
void renderCock(QPainter *thePainter);
void setEnabled(bool);
void refreshCanvas();
void projectRead();
void drawRasterLayer(QString);
void drawVectorLayer(QString,QString,QString);

private:

bool mEnable;
int mPluginType;
//! Pointer to our toolbar
QToolBar *mToolBarPointer;
//! Pionter to QGIS main application object
QgisApp *mQGisApp;
//! Pointer to the QGIS interface object
QgisIface *mQGisIface;
//!pointer to the qaction for this plugin
QAction * mQActionPointer;


};

#endif //Cock_H

munna
1st June 2006, 11:14
Are you sure that you are emitting the signal in the correct manner?

Can we see that part of the code as well?

zlatko
1st June 2006, 11:22
also use CODE tags becouse we see only some abracadabra

quickNitin
1st June 2006, 11:32
This is the code where i am generating signals. It is in pbnOk_clicked() slot.



#include "cockgui.h"

//qt includes

//standard includes

CockGui::CockGui() : CockGuiBase()
{
setupUi(this);
}

CockGui::CockGui( QWidget* parent, Qt::WFlags fl )
: QDialog ( parent, fl )
{
setupUi(this);
}
CockGui::~CockGui()
{
}

void CockGui::pbnOK_clicked()
{
//
// If you have a produced a raster layer using your plugin, you can ask qgis to
// add it to the view using:
// emit drawRasterLayer(QString("layername"));
// or for a vector layer
// emit drawVectorLayer(QString("pathname"),QString("layername"),QString("provider name (either ogr or postgres"));
//
//close the dialog
hide();
emit enableCock(cBoxAllow->isChecked());
emit enableCockAgain(cBoxAgain->isChecked());
emit needToRefresh();
done(1);
}
void CockGui::pbnCancel_clicked()
{
close(1);
}
void CockGui::setBox(bool theBool)
{
cBoxAllow->setChecked(theBool);
}


i couldn't perceive the meaning of CODE tags. Do you want some explanation about what i am trying to achieve.
What i am trying to do is to design a plugin for QGIS( open source map viewer) aiming to display a cock to show direction of wind. I generated form using designer. After that i added my functionality to it. class CockGui handles interaction of logic to dialog and class Cock implements that logic.
I have similar code working there but i don't know what mistake i make here . Ijust couldn't make it out.
mEnable is a boolean flag which tells plugin to whether to display cock or not. mEnable is set by a slot setBox(bool) (code for it already there). setBox(bool) slot is connected to signal generated in accordance with state of checkboxes.

i think i have tried to explain my motive. Anything more required please instruct me.

munna
1st June 2006, 11:50
i couldn't perceive the meaning of CODE tags.

Write

"["CODE"]"

Your code. Use the tags without " "

"["/CODE"]"

is pbnOk_clicked() slot called eveytime the button is clicked ?

quickNitin
1st June 2006, 11:52
yes. I will recheck it but i am confident about it.

quickNitin
1st June 2006, 12:23
thanks man, i think u r right but i am not still sure. While creating dialog i fromed a connection between a slot accept and OK button. I am trying to rectify it now.
any advices are welcome

munna
1st June 2006, 12:43
Can we see the code where you make the connection ?

quickNitin
1st June 2006, 13:00
i am putting in constructor of CockGui code

connect(pbnOK,SIGNAL(clicked()),this,SLOT(pbnOK_cl icked()));

quickNitin
1st June 2006, 13:10
there is a script which generated basic framework for my plugin code and i followed that and added functionality to it . There is no code in my knowledge which would do job of connecting clicked() signal of ok button with function pbnOK_clicked() (ya it is declared as function); In other plugins this is working well. There is no such connection code. Is it possible? or am i missing sth?
currently in my code i had made pbnOK_clicked() as a public slot and connected it to clicked event in constructor of CockGUI class. I hope it will work



CockGui::CockGui() : CockGuiBase()
{
setupUi(this);
connect(pbnOK,SIGNAL(clicked()),this,SLOT(pbnOK_cl icked()));
}

jacek
1st June 2006, 13:15
What happens when you add:
connect( myPluginGui, SIGNAL( enableCock( bool ) ), qApp, SLOT( aboutQt() ) );
after:
connect( myPluginGui, SIGNAL( enableCock( bool ) ), this, SLOT( setEnabled( bool ) ) );?
Are there any message on the console? Do you compile your application in debug mode?

quickNitin
1st June 2006, 13:50
after adding the code one dialog appeared telling about qt similat to one which gets generated from help menu of QtDesigner.
I compiled it in debug mode ( -g option ). and there are nothing as such on terminal excepat statements which i added. One more thing setEnabled() is being called twice after that there is no call . I trying to figure out it.

jacek
1st June 2006, 14:19
after adding the code one dialog appeared telling about qt similat to one which gets generated from help menu of QtDesigner.
So that signal should be OK and probably there is some other code that invokes setEnable() or changes mEnabled.

Grep your code and check how do you access that mEnabled variable --- maybe you do something like: if( mEnabled = false ) ... ?

Then set a brakepoint inside the setEnabled() and check the stack trace and values of "sender()" and "theBool".

quickNitin
2nd June 2006, 04:56
you are right. I debugged it back. Accidently i didn't removed code which was reverting setEnabled() action.

regrads
nitin k dhiman