Results 1 to 6 of 6

Thread: how to access class member from other class

  1. #1
    Join Date
    Jul 2006
    Location
    Slovakia
    Posts
    17
    Thanks
    12
    Thanked 6 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to access class member from other class

    Hello,

    this may sound like a subject for pure C++ Programming forum but I ilustrate problem on qt4/ui program, so I deciced to rather put it here.

    I have form designed in designer, it contains only treeWidget which is promoted to custom widget to add drag and drop capabilty.

    My question is how to access myAppList defined in myApp.h from qmytreewidget.cpp ? I use Qt 4.2.0-tp1 opensource, Windows 2000, mingw.

    Thanks for any suggestions.

    Whole app is zipped in attachement, most interesting parts:

    myApp.h

    Qt Code:
    1. #ifndef MYAPP_H
    2. #define MYAPP_H
    3.  
    4. #include "ui_form.h"
    5.  
    6. class myApp : public QWidget, private Ui::myAppDLG
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. myApp();
    12.  
    13. // i want to access this from qmytreewidget.cpp, line 26
    14. QStringList myAppList;
    15.  
    16. private:
    17. Ui::myAppDLG ui;
    18.  
    19. };
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    qmytreewidget.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "qmytreewidget.h"
    3. #include "myApp.h"
    4.  
    5.  
    6. QMyTreeWidget::QMyTreeWidget(QWidget *parent)
    7. : QTreeWidget(parent)
    8. {
    9.  
    10. }
    11.  
    12.  
    13. bool QMyTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
    14. {
    15. QList <QUrl> urlList;
    16.  
    17. urlList = data->urls();
    18.  
    19. foreach(QUrl url, urlList)
    20. {
    21. item = new QTreeWidgetItem(this);
    22. item->setText(0, url.toLocalFile());
    23. // i want to access myAppList, defined in myApp.h
    24. // what is the best way to do it ?
    25. myAppList.append(url.toLocalFile()); // this of course doesn't work, just to clear what a want to do
    26. }
    27.  
    28. return true;
    29. }
    30.  
    31.  
    32. QStringList QMyTreeWidget::mimeTypes () const {
    33.  
    34. QStringList qstrList;
    35. qstrList.append("text/uri-list");
    36. return qstrList;
    37. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to access class member from other class

    You can use signals & slots mechanism for this:
    Qt Code:
    1. class QMyTreeWidget : ...
    2. {
    3. Q_OBJECT
    4. public:
    5. ...
    6.  
    7. signals:
    8. void fileDropped( const QString& );
    9. ...
    10. };
    11.  
    12. bool QMyTreeWidget::dropMimeData(...)
    13. {
    14. ...
    15. foreach(QUrl url, urlList)
    16. {
    17. item = new QTreeWidgetItem(this);
    18. item->setText(0, url.toLocalFile());
    19. emit fileDropped( url.toLocalFile() );
    20. }
    21. ...
    22. }
    23. ...
    24. class myApp : public QWidget, private Ui::myAppDLG
    25. {
    26. Q_OBJECT
    27. ...
    28. public slots:
    29. void on_treeWidgetOrWhateverItsCalled_fileDropped( const QString& fileName );
    30. ...
    31. };
    32.  
    33. void myApp::on_treeWidgetOrWhateverItsCalled_fileDropped( const QString& fileName )
    34. {
    35. // do something with fileName
    36. }
    To copy to clipboard, switch view to plain text mode 

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

    sector (14th July 2006)

  4. #3
    Join Date
    Jul 2006
    Location
    Slovakia
    Posts
    17
    Thanks
    12
    Thanked 6 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to access class member from other class

    Many thanks for your help.

    Have a nice day.

  5. #4
    Join Date
    Jul 2006
    Location
    Slovakia
    Posts
    17
    Thanks
    12
    Thanked 6 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to access class member from other class

    Another question come to my mind
    Lets say we have these two classes. Is it possible to call myApp class function (would by QString myApp::doSomeStringTricks(QString str)) from some of QMyTreeWidget function ?

    Qt Code:
    1. bool QMyTreeWidget::dropMimeData(...)
    2. {
    3. QString string="abcdef";
    4. QString retStr;
    5. ...
    6. // I want sometihing like retStr = myApp::doSomeStringTricks(string)
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    This actually gives me error:
    qmytreewidget.cpp:26: error: cannot call member function `QString
    myApp::doSomeStringTricks(const QString&)' without object

    I know that function doSomeStringTricks might be defined as QMyTreeWidget funcion, then it could be called with no problem from myApp (like treeWidget->doSomeStringTricks(str)).

    What is a best approach to this? What is the best place to define general purpose functions for use with application?

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to access class member from other class

    A method must be declared as static to be able to call it without instantiating an object:

    Qt Code:
    1. // header
    2. class Util
    3. {
    4. public:
    5. static QString doSomeTrick();
    6. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // implementation
    2. QString Util::doSomeTrick()
    3. {
    4. return QString("blaa");
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now you can use it like this:
    Qt Code:
    1. #include "util.h"
    2. QString trick = Util::doSomeTrick();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    sector (14th July 2006)

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to access class member from other class

    Quote Originally Posted by sector
    This actually gives me error:
    qmytreewidget.cpp:26: error: cannot call member function `QString
    myApp::doSomeStringTricks(const QString&)' without object
    That error message contains the answer. You can't invoke methods without an object --- you need a pointer, reference or the object itself to do this.

    Quote Originally Posted by sector
    treeWidget->doSomeStringTricks(str)
    This works, because you have "treeWidget" pointer. To do the same with myApp class, you have to pass a pointer or reference to it to the place where you want to invoke its methods.

    Quote Originally Posted by sector
    What is a best approach to this? What is the best place to define general purpose functions for use with application?
    All depends on the project. It would be nice if you could split your application into two layers --- a GUI, that provides interface for the user, and core, which implements your application's logic. Usually there should be also a third layer, that allows the core to access data. You can put general purpose functions in separate namespace, but make them generic --- so that you can create a collection of useful functions that you can use in different projects.

    When it comes to widgets, you should always remember about code reuse. If you need a custom widget, implement it in such way that you can use it in another application (unless it's very custom ). This means that it shouldn't know anything about your application's logic or other classes. Signals & slots mechanism is very useful in achieving this.

  9. The following user says thank you to jacek for this useful post:

    sector (15th July 2006)

Similar Threads

  1. Replies: 2
    Last Post: 4th May 2006, 19:17
  2. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 01:27
  3. const member definitions in a class ....
    By TheKedge in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 13:03

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.