PDA

View Full Version : slots calling functions from other classes?



kja
17th November 2010, 07:13
Hi,

Is it possible to get your slots to call other functions from other classes?

I want to have my main qwidget's buttons call on a class I made that plots some QWTplots. This is what I'm working on now, but I am afraid I am going about the slot implementation the wrong way


class myPlot : public QwtPlot
{
...
myPlot(){}
void loadFile( char * fn )
{
//a function that opens a file...
}
void plot( double st, double end )
{
//loads the data and assigns it to a curve...
}
};
class myCanvas : public QWidget
{
Q_OBJECT
public:
myCanvas (QWidget * parent = 0);
public Q_SLOTS:
void enterFile();
void addFile();
void makePlot();
};




myCanvas::myCanvas(QWidget *parent):QWidget(parent)
{
....
myPlot *plot = new myPlot(); //is this even close to right?

connect(pushbutton, SIGNAL(clicked()), this, SLOT(enterFile()));
connect(pushbutton, SIGNAL(clicked()), this, SLOT(addFile()));
connect(pushbutton, SIGNAL(clicked()), this, SLOT(makePlot()));
....
}

void myCanvas::enterFile()
{
QString theText = lineedit->text(); ///this works
FileName = qstrdup( theText.toLatin1() );
}
void myCanvas::addFile()
{
plot->loadFile(FileName); //error plot undeclared identifier
}
void myCanvas::slotButtonPlots()
{
myPlot::plot(1264377600, 1288974375.51);
//error: illegal call of non-static member function
// something like this maybe?
}


Any ideas how I can call these these myPlot functions with myCanvas, or is that impossible?

Do I have to make new functions in myCanvas in order for the slots to work?

Thanks a lot!

janorcutt
17th November 2010, 07:49
have you put...

#include "myPlot.h"

class myPlot;
in your header for myCanvas?

I've had a similar problem...

it would help if i read your origional post properly

squidge
17th November 2010, 07:52
'plot' is local to the myCanvas constructor.

Put it as a member variable.

janorcutt
17th November 2010, 08:01
class myPlot : public QwtPlot
{
...
myPlot(){}
void loadFile( char * fn )
{
//a function that opens a file...
}
void plot( double st, double end )
{
//loads the data and assigns it to a curve...
}
};

class myPlot; //try this

class myCanvas : public QWidget
{
Q_OBJECT
public:
myCanvas (QWidget * parent = 0);
public Q_SLOTS:
void enterFile();
void addFile();
void makePlot();
};

i believe this should work, maybe, sort of, well it should. lol

Added after 7 minutes:

just to clarify for me, are you trying to call the plot() function from your plot object, or do you want to call the function outside of an object to get the return value?

ie

//this
myPlot plot;
plot.plot(dub1, dub2);

//or
myPlot::plot(dub1,dub2)

kja
17th November 2010, 15:28
Thanks for all the replies.


just to clarify for me, are you trying to call the plot() function from your plot object, or do you want to call the function outside of an object to get the return value?

I think I want to call the plot() from the plot object because I want all of the parameters that I set up in my functions, like loadFile. But I'm actually not sure of the difference between plot.plot(dub1, dub2); and myPlot::plot(dub1,dub2);


class myPlot; //try this

class myCanvas : public QWidget
{
Q_OBJECT

Do you mean simply write "class myPlot;" in the file with class myCanvas? I'm curious, what does that do in general?
I tried that and I still get the errors that 'plot' is an undeclared identifier.



have you put...
#include "myPlot.h"
in your header for myCanvas?
actually I have them in the same .h file

Added after 41 minutes:


'plot' is local to the myCanvas constructor.

Put it as a member variable.

This seems to be the way to go. I am now allowed to access the functions in myPlot!

thanks a lot!

janorcutt
17th November 2010, 17:29
OK

I think I want to call the plot() from the plot object because I want all of the parameters that I set up in my functions, like loadFile. But I'm actually not sure of the difference between plot.plot(dub1, dub2); and myPlot::plot(dub1,dub2);
I know what your getting at now, and i would say you need to call
plot.plot(dub1, dub2); as this performs the plot() function in the context of the object
myPlot plot;

using
myPlot::plot(dub1,dub2) would be useless because the function has no valid return type.
for example i could create a QPixmap in two ways...


QPixmap pixmap;
pixmap.loadFromData(...etc...);

//or

QPixmap pixmap2 = QPixmap::fromImage(...etc...);


as you can see this allows you to use a function without creating an extra object...
i hope that made sense roflmao