Results 1 to 12 of 12

Thread: how to connect an openfile signal to a widget slot?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    Hi,

    thanks to the great documentation/tutorials/fora,
    I managed to set up the menubar with QAction. Now I face this problem

    Qt Code:
    1. error: ‘my2dPlot* MyWidget::mainPlot’ is private
    To copy to clipboard, switch view to plain text mode 
    when i try to add this line to the openfile action

    Qt Code:
    1. widget->mainPlot->plotMainCurve(fileName);
    To copy to clipboard, switch view to plain text mode 

    -widget is the central widget of the mainWindow
    -mainPlot is a class that I have created to plot 2d plots
    -plotMainCurve is the method to plot the selected data file


    This is seems to be a typical hierarchy error. But how can i overcome it and call the function to plot the curve of a specific data file (filename= "data.txt")?

  2. #2
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    before it was:


    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5. private:
    6. my2dPlot *mainPlot;
    7. ....
    8. {
    To copy to clipboard, switch view to plain text mode 


    and now i moved the mainPlot member to the public:

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5. my2dPlot *mainPlot;
    6. ....
    7. {
    To copy to clipboard, switch view to plain text mode 

    I don't know if it is a real solution and/or safe thing to do. But how else could i call the plotMainCurve(filename) of the my2dPlot class form the mainwindow?

  3. #3
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect an openfile signal to a widget slot?

    Quote Originally Posted by fatecasino View Post
    I don't know if it is a real solution and/or safe thing to do. But how else could i call the plotMainCurve(filename) of the my2dPlot class form the mainwindow?
    you could write a getter method:

    Qt Code:
    1. my2dPlot* Get2DPlot() {return my2dplot;} const;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    yeah, but I don't want to receive the plot. The plot belongs to the my2dPlot class. I just want to send the datafile that I want to print. As you see the hierarchy goes:


    Qt Code:
    1. widget->mainPlot->plotMainCurve(fileName);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to connect an openfile signal to a widget slot?

    What about
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5. public slots:
    6. void plotFromFile( const QString& file ){
    7. this->mainPlot->plotMainCurve(file);
    8. }
    9. private:
    10. my2dPlot *mainPlot;
    11. ....
    12. {
    To copy to clipboard, switch view to plain text mode 
    ?

  6. The following user says thank you to stampede for this useful post:

    fatecasino (14th December 2010)

  7. #6
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    @stampede: it worked like a charm!!
    thank you very much..I always really confused with the usage of "this"
    What is the actual difference between the two lines??
    Qt Code:
    1. this->mainPlot->plotMainCurve(file);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. widget->mainPlot->plotMainCurve(fileName);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect an openfile signal to a widget slot?

    "this" is a pointer to the object which "posesses" the current method.

    the difference is that the second line "widget->mainPlot..." works only if "mainPlot" is public. That's why I recommended to create a getter. The following would have worked too:
    Qt Code:
    1. widget->Get2DPlot()->plotMainCurve(file)
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to connect an openfile signal to a widget slot?

    What is the actual difference between the two lines??
    You use "this" keyword from class methods when you refer to actualy used class instance ( the object that "calls" the method or access the class member ).
    Of course you have access to all members of the class in this case.

    Here
    widget->mainPlot->plotMainCurve(fileName);
    you use some object "widget" to call the method. You can access only public members of the "widget" object ( unless you are a friend of a class )

    This basic knowledge is mandatory in C++ programming, I really recommend some reading before going into qt programming ( you can find some useful info here: http://www.qtcentre.org/threads/29-W...ourite-C-books )

Similar Threads

  1. Replies: 2
    Last Post: 15th September 2010, 00:54
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. How to connect signal/slot in QItemEditorFactory?
    By yyalli in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2010, 14:56
  4. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 19:38
  5. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 22:55

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
  •  
Qt is a trademark of The Qt Company.