1 Attachment(s)
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
Code:
#ifndef MYAPP_H
#define MYAPP_H
#include "ui_form.h"
class myApp
: public QWidget,
private Ui
::myAppDLG{
Q_OBJECT
public:
myApp();
// i want to access this from qmytreewidget.cpp, line 26
private:
Ui::myAppDLG ui;
};
#endif
qmytreewidget.cpp
Code:
#include <QtGui>
#include "qmytreewidget.h"
#include "myApp.h"
QMyTreeWidget
::QMyTreeWidget(QWidget *parent
){
}
{
urlList = data->urls();
foreach
(QUrl url, urlList
) {
item->setText(0, url.toLocalFile());
// i want to access myAppList, defined in myApp.h
// what is the best way to do it ?
myAppList.append(url.toLocalFile()); // this of course doesn't work, just to clear what a want to do
}
return true;
}
qstrList.append("text/uri-list");
return qstrList;
}
Re: how to access class member from other class
You can use signals & slots mechanism for this:
Code:
class QMyTreeWidget : ...
{
Q_OBJECT
public:
...
signals:
void fileDropped( const QString& );
...
};
bool QMyTreeWidget::dropMimeData(...)
{
...
foreach(QUrl url, urlList
) {
item->setText(0, url.toLocalFile());
emit fileDropped( url.toLocalFile() );
}
...
}
...
class myApp
: public QWidget,
private Ui
::myAppDLG {
Q_OBJECT
...
public slots:
void on_treeWidgetOrWhateverItsCalled_fileDropped( const QString& fileName );
...
};
void myApp::on_treeWidgetOrWhateverItsCalled_fileDropped( const QString& fileName )
{
// do something with fileName
}
Re: how to access class member from other class
Many thanks for your help.
Have a nice day.
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 ?
Code:
bool QMyTreeWidget::dropMimeData(...)
{
...
// I want sometihing like retStr = myApp::doSomeStringTricks(string)
...
}
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?
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:
Code:
// header
class Util
{
public:
};
Code:
// implementation
{
}
Now you can use it like this:
Code:
#include "util.h"
QString trick
= Util
::doSomeTrick();
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.