Results 1 to 4 of 4

Thread: Some question in example

  1. #1
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Some question in example

    Hi everyone,

    I have a question in "c++ gui programming with qt 4" chapter 5-4 example(Plotter):

    [plotter.h]
    Qt Code:
    1. #ifndef PLOTTER_H
    2. #define PLOTTER_H
    3.  
    4. //QMap store (key, value) pairs and provides fast lookup of the value associated with a key.
    5. #include <QMap>
    6. #include <QPixmap>
    7. #include <QVector>
    8. #include <QWidget>
    9.  
    10. class PlotSettings;
    11.  
    12. class Plotter : public QWidget {
    13. Q_OBJECT
    14.  
    15. public:
    16. Plotter(QWidget *parent = 0);
    17.  
    18. void setPlotSettings(const PlotSettings &settings);
    19. void setCurveData(int id, const QVector<QPointF> &data);
    20. void clearCurve(int id);
    21. QSize minimumSizeHint() const;
    22. QSize sizeHint() const;
    23.  
    24. protected:
    25. void paintEvent(QPaintEvent *event);
    26. void resizeEvent(QResizeEvent *event);
    27. void mousePressEvent(QMouseEvent *event);
    28. void mouseMoveEvent(QMouseEvent *event);
    29. void mouseReleaseEvent(QMouseEvent *event);
    30. void KeyPressEvent(QkeyEvent *event);
    31. void wheelEvent(QWheelEvent *event);
    32.  
    33. private:
    34. void updateRubberBandRegion();
    35. void refreshPixmap();
    36. void drawGrid(QPainter *painter);
    37. void drawCurves(QPainter *painter);
    38.  
    39. enum { Margin = 50 };
    40.  
    41. QToolButton *zoomInButton;
    42. QToolButton *zoomOutButton;
    43. QMap<int, QVector<QPointF> > curveMap;
    44. QVector<PlotSettings> zoomStack;
    45. int curZoom;
    46. bool rubberBandIsShown;
    47. QRect rubberBandRect;
    48. QPixmap pixmap;
    49. };
    50.  
    51. class PlotSettings {
    52.  
    53. public:
    54. PlotSettings();
    55.  
    56. void scroll(int dx, int dy);
    57. void adjust();
    58. double spanX() const {
    59. return maxX - minX;
    60. }
    61. double spanY() const {
    62. return maxY - minY;
    63. }
    64.  
    65. double maxX;
    66. double minX;
    67. int numXTicks;
    68. double maxY;
    69. double minY;
    70. int numYTicks;
    71.  
    72. private:
    73. static void adjustAxis(double &min, double &max, int &numTicks);
    74. };
    75.  
    76. #endif
    To copy to clipboard, switch view to plain text mode 

    [plotter.cpp]
    Qt Code:
    1. #include <QtGui>
    2. #include <cmath>
    3.  
    4. #include "plotter.h"
    5.  
    6. Plotter::Plotter(QWidget *parent) : QWidget(parent) {
    7. setBackgroundRole(QPalette::Dark);
    8. setAutoFillBackground(true);
    9. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    10. setFocusPolicy(Qt::StrongFocus);
    11. rubberBandIsShown = false;
    12.  
    13. zoomInButton = new QToolButton(this);
    14. zoomInButton->setIcon(QIcon(":/images/zoomIn.png"));
    15. zoomInButton->adjustSize();
    16. connect(zoomInButton, SIGNAL(clicked()), this, SLOT(zoomIn());
    17.  
    18. zoomOutButton = new QToolButton(this);
    19. zoomOutButton->setIcon(QIcon(":/images/zoomOut.png"));
    20. zoomOutButton->adjustSize();
    21. connect(zoomOutButton, SIGNAL(clicked), this, SLOT(zoomOut));
    22.  
    23. setPlotSettings(PlotSettings());
    24. }
    25.  
    26. ......
    To copy to clipboard, switch view to plain text mode 

    and i have question in plotter.cpp,
    i never include QToolButton class,
    why i can use its constructor and function.

    Can someone tell me how to solve the problem?
    thanks !

  2. #2
    Join Date
    Aug 2012
    Location
    Loughborough, UK
    Posts
    29
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Some question in example

    #include <QtGui> includes the things you're talking about in qt4.

  3. The following user says thank you to rockdemon for this useful post:

    jhowliu (9th August 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Some question in example

    As rockdemon already said this is the working of the #include <QtGui>

    To be a bit more precise, <QtGui> it a so-called module include, it is basically a header file that in turn includes all the headers for a certain Qt module.
    In case of QtGui all headers of the the Qt GUI module, which in Qt4 means all widgets.

    There is a similar module include for QtCore, QtNetwork and so on.

    Those monster includes obviously come with a price, they expand to all class declarations of a module when the source file is run through the compiler, making it parse tons of classes that it then doesn't need.

    So while the module includes are very convenient for small test programs or prototypes, it is usually considered better pratice in real world projects to include all used classes individually.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    jhowliu (9th August 2013)

  6. #4
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some question in example

    Thanks for your reply.

    and thanks anda_skoa very much for your explanation.
    I will do more practice, thanks.

Similar Threads

  1. MVC question
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 17th June 2009, 12:11
  2. Question
    By Dumbledore in forum Qt Programming
    Replies: 1
    Last Post: 20th October 2007, 23:12
  3. GUi or Not GUI - that is the question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2007, 18:19
  4. .Net & C# question
    By mickey in forum General Programming
    Replies: 1
    Last Post: 20th November 2006, 14:25
  5. map question
    By mickey in forum General Programming
    Replies: 12
    Last Post: 10th June 2006, 18:40

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.