Results 1 to 9 of 9

Thread: construction of classes within classes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: construction of classes within classes

    If i use a container class directly (as far as i know) i can only give the container a name (i.e. atom) and the variables inside (i.e. QString, QList, QList) are not named this makes organizing the data more difficult.
    For this there are maps.
    Have a look at QMap.
    So you can do things like:
    Qt Code:
    1. QMap<QString,atom*> m_mapAtoms;
    2. ...
    3. atom *pAtom = new atom;
    4. ...
    5. //Storing
    6. m_mapAtoms["atom1"] = pAtom;
    7.  
    8. //using
    9. pAtom = m_mapAtoms["atom1"];
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. The following user says thank you to high_flyer for this useful post:

    mobucl (10th January 2011)

  3. #2
    Join Date
    Dec 2010
    Posts
    31
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: construction of classes within classes

    Hi high_flyer,

    Thanks again. I think i see how to use this, however using

    Qt Code:
    1. class atom
    2. {
    3. public:
    4. QString type;
    5. QList hkl
    6. Qlist IJK
    7. };
    To copy to clipboard, switch view to plain text mode 
    in the header file to create the class atom and then running the code you gave below
    i get an error:

    aggregate 'QMap<QString, atom*> m_mapAtoms' has incomplete type and cannot be defined

    Sorry but im not sure why i get this as if i just create an instance of atom (atom atom1) it works fine.

    Can you please help?

    Thanks

    Matt
    Last edited by wysota; 10th January 2011 at 14:33. Reason: missing [code] tags

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: construction of classes within classes

    Show the definition code and the code where you use the map. (not just these lines, but a bit before and after)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    mobucl (10th January 2011)

  6. #4
    Join Date
    Dec 2010
    Posts
    31
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: construction of classes within classes

    HI high_flyer.

    This is all the code from the header file (including the previously discussed attempts at creating nested classes and some function defs):

    Qt Code:
    1. #ifndef ATTEMPT1_H
    2. #define ATTEMPT1_H
    3.  
    4. //#include <QWidget>
    5. //#include <iostream>
    6. //#include <fstream>
    7. //#include <string>
    8. //#include <vector>
    9. //#include <windows.h>
    10. //#include <QTextEdit>
    11. //#include <QString>
    12. #include <QFileDialog>
    13. //using namespace std;
    14. //#include <QtCore/QFile>
    15. //#include <QtCore/QTextStream>
    16. void FileOpen(QString fileName, QVector<double>& x, QVector<QVector<double> >& y); // this is using pointers for output
    17. void FiCalc(double lambda, double TwoThetaMin, double TwoThetaMax);
    18. QList<QList<int> > Combinations(int a, int b, int c,int aa,int bb,int cc);
    19. QList<double> AtomicScattering(QString Symbol,QList<float> dSpace);
    20. namespace Ui {
    21. class attempt1;
    22. }
    23.  
    24. class attempt1 : public QWidget
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. explicit attempt1(QWidget *parent = 0);
    30. ~attempt1();
    31.  
    32. private:
    33. Ui::attempt1 *ui;
    34.  
    35. private slots:
    36. void on_save_clicked();
    37. void on_run_clicked();
    38. void on_load_clicked();
    39. };
    40.  
    41. #endif // ATTEMPT1_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. class atom
    2. {
    3. public:
    4. QString type;
    5. };
    6.  
    7.  
    8. class data
    9. {
    10. //int size;
    11. public:
    12. QString name; //string for standard name from CIF
    13. // QList < QList <QList<float> > >xyz; //3d array for XYZ values from CIF
    14. // data(int);
    15. int size;
    16. double aSide;
    17. double bSide;
    18. double cSide;
    19. double alpha;
    20. double beta;
    21. double gamma;
    22. double volume;
    23. QList<float> dSpace; //use a float as we only want to compare to 8 significant figures
    24. QList<QList<int> > hkl;
    25. QList<float> TwoTheta;
    26. QList<float> I;
    27. atom atom1;
    28. atom atom2;
    29.  
    30. };
    To copy to clipboard, switch view to plain text mode 
    The code im using in the cpp file id just what you wrote below:

    Qt Code:
    1. QMap<QString,atom*> m_mapAtoms;
    2. atom *pAtom = new atom;
    3. //Storing
    4. m_mapAtoms["atom1"] = pAtom;
    5. //using
    6. pAtom = m_mapAtoms["atom1"];
    To copy to clipboard, switch view to plain text mode 

    Is this the information you need?

    thanks

    Matt


    Added after 19 minutes:


    Sorry high_flyer I just realised i didnt include <QMap> (im still getting used to including things as in matlab you don't do this!).

    I will have a look at QMap now - in your opinion is this the best way to organize data sets each containing lots of different data types?

    Thanks for your help again!

    Matt
    Last edited by wysota; 10th January 2011 at 14:34. Reason: missing [code] tags

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

    Default Re: construction of classes within classes

    please use "code"-tags.

    do you include <QMap>?

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

    mobucl (10th January 2011)

  9. #6
    Join Date
    Dec 2010
    Posts
    31
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: construction of classes within classes

    sorry will do in future

    yes include was the problem - i did notice and updated the post above about 20 mins after!

    Thanks

    Matt

Similar Threads

  1. QT WMI Classes
    By justatiq in forum Qt Programming
    Replies: 31
    Last Post: 10th April 2011, 15:44
  2. Using WMI CLasses
    By newb in forum Newbie
    Replies: 1
    Last Post: 4th June 2010, 19:19
  3. Relationship b/w different classes
    By salmanmanekia in forum Newbie
    Replies: 0
    Last Post: 2nd June 2010, 18:24
  4. fax classes in qt
    By dyams in forum Qt Programming
    Replies: 5
    Last Post: 7th September 2007, 09:14

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.