Results 1 to 6 of 6

Thread: slots calling functions from other classes?

  1. #1
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default slots calling functions from other classes?

    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

    Qt Code:
    1. class myPlot : public QwtPlot
    2. {
    3. ...
    4. myPlot(){}
    5. void loadFile( char * fn )
    6. {
    7. //a function that opens a file...
    8. }
    9. void plot( double st, double end )
    10. {
    11. //loads the data and assigns it to a curve...
    12. }
    13. };
    14. class myCanvas : public QWidget
    15. {
    16. Q_OBJECT
    17. public:
    18. myCanvas (QWidget * parent = 0);
    19. public Q_SLOTS:
    20. void enterFile();
    21. void addFile();
    22. void makePlot();
    23. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. myCanvas::myCanvas(QWidget *parent):QWidget(parent)
    2. {
    3. ....
    4. myPlot *plot = new myPlot(); //is this even close to right?
    5.  
    6. connect(pushbutton, SIGNAL(clicked()), this, SLOT(enterFile()));
    7. connect(pushbutton, SIGNAL(clicked()), this, SLOT(addFile()));
    8. connect(pushbutton, SIGNAL(clicked()), this, SLOT(makePlot()));
    9. ....
    10. }
    11.  
    12. void myCanvas::enterFile()
    13. {
    14. QString theText = lineedit->text(); ///this works
    15. FileName = qstrdup( theText.toLatin1() );
    16. }
    17. void myCanvas::addFile()
    18. {
    19. plot->loadFile(FileName); //error plot undeclared identifier
    20. }
    21. void myCanvas::slotButtonPlots()
    22. {
    23. myPlot::plot(1264377600, 1288974375.51);
    24. //error: illegal call of non-static member function
    25. // something like this maybe?
    26. }
    To copy to clipboard, switch view to plain text mode 


    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!

  2. #2
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: slots calling functions from other classes?

    have you put...
    Qt Code:
    1. #include "myPlot.h"
    2.  
    3. class myPlot;
    To copy to clipboard, switch view to plain text mode 
    in your header for myCanvas?

    I've had a similar problem...

    it would help if i read your origional post properly

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slots calling functions from other classes?

    'plot' is local to the myCanvas constructor.

    Put it as a member variable.

  4. The following user says thank you to squidge for this useful post:

    kja (17th November 2010)

  5. #4
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: slots calling functions from other classes?

    Qt Code:
    1. class myPlot : public QwtPlot
    2. {
    3. ...
    4. myPlot(){}
    5. void loadFile( char * fn )
    6. {
    7. //a function that opens a file...
    8. }
    9. void plot( double st, double end )
    10. {
    11. //loads the data and assigns it to a curve...
    12. }
    13. };
    14.  
    15. class myPlot; //try this
    16.  
    17. class myCanvas : public QWidget
    18. {
    19. Q_OBJECT
    20. public:
    21. myCanvas (QWidget * parent = 0);
    22. public Q_SLOTS:
    23. void enterFile();
    24. void addFile();
    25. void makePlot();
    26. };
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. //this
    2. myPlot plot;
    3. plot.plot(dub1, dub2);
    4.  
    5. //or
    6. myPlot::plot(dub1,dub2)
    To copy to clipboard, switch view to plain text mode 
    Last edited by janorcutt; 17th November 2010 at 08:01.

  6. #5
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slots calling functions from other classes?

    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:lot(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!
    Last edited by kja; 17th November 2010 at 15:28.

  7. #6
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: slots calling functions from other classes?

    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:lot(dub1,dub2);
    I know what your getting at now, and i would say you need to call
    Qt Code:
    1. plot.plot(dub1, dub2);
    To copy to clipboard, switch view to plain text mode 
    as this performs the plot() function in the context of the object
    Qt Code:
    1. myPlot plot;
    To copy to clipboard, switch view to plain text mode 

    using
    Qt Code:
    1. myPlot::plot(dub1,dub2)
    To copy to clipboard, switch view to plain text mode 
    would be useless because the function has no valid return type.
    for example i could create a QPixmap in two ways...
    Qt Code:
    1. QPixmap pixmap;
    2. pixmap.loadFromData(...etc...);
    3.  
    4. //or
    5.  
    6. QPixmap pixmap2 = QPixmap::fromImage(...etc...);
    To copy to clipboard, switch view to plain text mode 

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

  8. The following user says thank you to janorcutt for this useful post:

    kja (17th November 2010)

Similar Threads

  1. Qt & OpenGl, calling gl-commands within other classes
    By KnufflPuffl in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2011, 10:44
  2. Calling GUI functions from a DLL
    By inktomi in forum Qt Programming
    Replies: 0
    Last Post: 19th July 2010, 12:50
  3. Cannot call OpenCV 2.0 functions inside Qt slots
    By Asfer in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2010, 11:48
  4. Replies: 7
    Last Post: 15th January 2010, 20:45
  5. Question about functions in classes
    By cwnelatury in forum Newbie
    Replies: 1
    Last Post: 13th May 2009, 06:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.