Results 1 to 7 of 7

Thread: class MainWindow has no member 'customPlot'

  1. #1
    Join Date
    Nov 2012
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default class MainWindow has no member 'customPlot'

    Hello,

    When I implement the code below I get the error: 'class MainWindow' has no member named 'customPlot'
    What can I do to solve this?

    Regards,
    Arend (absolute beginner)

    Qt Code:
    1. [B]mainwindow.h[/B]
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QtCore>
    6. #include <QtGui>
    7. #include "qcustomplot.h"
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private slots:
    22. void on_pushButtonSwap_clicked();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    29.  
    30. [B]mainwindow.cpp:[/B]
    31. #include "mainwindow.h"
    32. #include "ui_mainwindow.h"
    33. #include "QtCore"
    34. #include "QtGui"
    35. #include "QDebug"
    36. #include "QMessageBox"
    37. #include <QVector>
    38. #include <cmath>
    39.  
    40. MainWindow::MainWindow(QWidget *parent) :
    41. QMainWindow(parent),
    42. ui(new Ui::MainWindow)
    43. {
    44. ui->setupUi(this);
    45. }
    46.  
    47. MainWindow::~MainWindow()
    48. {
    49. delete ui;
    50. }
    51. void MainWindow::on_pushButtonSwap_clicked()
    52. {
    53. // generate some data:
    54. QVector<double> x(101), y(101);
    55. for (int i=0; i<101; ++i) {
    56. x[i] = i/50.0 - 1; // x goes from -1 to 1
    57. y[i] = x[i]*x[i]; // let's plot a quadratic function
    58. }
    59. this->customPlot->addGraph();
    60. this->customPlot->graph(0)->setData(x, y);
    61. this->customPlot->xAxis->setLabel("x");
    62. this->customPlot->yAxis->setLabel("y");
    63. this->customPlot->xAxis->setRange(-1, 1);
    64. this->customPlot->yAxis->setRange(0, 1);
    65. this->customPlot->replot();
    66. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    17
    Thanked 13 Times in 13 Posts

    Default Re: class MainWindow has no member 'customPlot'

    Try removing the keyword this before customPlot
    Qt Code:
    1. void MainWindow::on_pushButtonSwap_clicked()
    2. {
    3. // generate some data:
    4. QVector<double> x(101), y(101);
    5. for (int i=0; i<101; ++i) {
    6. x[i] = i/50.0 - 1; // x goes from -1 to 1
    7. y[i] = x[i]*x[i]; // let's plot a quadratic function
    8. }
    9. customPlot->addGraph();
    10. customPlot->graph(0)->setData(x, y);
    11. customPlot->xAxis->setLabel("x");
    12. customPlot->yAxis->setLabel("y");
    13. customPlot->xAxis->setRange(-1, 1);
    14. customPlot->yAxis->setRange(0, 1);
    15. customPlot->replot();
    16. }
    To copy to clipboard, switch view to plain text mode 
    if customPlot is defined in one of the header files, if not you need to declare an object of type QCusomPlot named customPlot in MainWindow class.

    Qt Code:
    1. this->customPlot
    To copy to clipboard, switch view to plain text mode 
    means that the class MainWindow has a member named customPlot, and in this case it does not exists.
    Last edited by Ashkan_s; 9th March 2013 at 12:44.

  3. #3
    Join Date
    Nov 2012
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: class MainWindow has no member 'customPlot'

    Thanks for your reply but when I remove this then i get:
    /Users/..../mainwindow.cpp:33: error: 'customPlot' was not declared in this scope

  4. #4
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    17
    Thanked 13 Times in 13 Posts

    Default Re: class MainWindow has no member 'customPlot'

    Declare it in MainWindow class
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8.  
    9. private slots:
    10. void on_pushButtonSwap_clicked();
    11.  
    12. private:
    13. Ui::MainWindow *ui;
    14. QCustomPlot *customPlot;
    15. };
    To copy to clipboard, switch view to plain text mode 

    And new it in MainWindow's constructor
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. customPlot = new QCustomPlot(this);
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2012
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: class MainWindow has no member 'customPlot'

    Thanks! Now it works.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: class MainWindow has no member 'customPlot'

    Quote Originally Posted by Ashkan_s View Post
    Declare it in MainWindow class
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8.  
    9. private slots:
    10. void on_pushButtonSwap_clicked();
    11.  
    12. private:
    13. Ui::MainWindow *ui;
    14. QCustomPlot *customPlot;
    15. };
    To copy to clipboard, switch view to plain text mode 

    And new it in MainWindow's constructor
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. customPlot = new QCustomPlot(this);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Why did you use pointer and not normal member? Needless use of the heap.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    17
    Thanked 13 Times in 13 Posts

    Default Re: class MainWindow has no member 'customPlot'

    Quote Originally Posted by amleto View Post
    Why did you use pointer and not normal member? Needless use of the heap.
    To be compatible with the rest of the code

Similar Threads

  1. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 17:15
  2. Replies: 4
    Last Post: 23rd August 2011, 22:53
  3. Replies: 3
    Last Post: 12th April 2011, 11:58
  4. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  5. Replies: 4
    Last Post: 29th May 2010, 13:56

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.