PDA

View Full Version : how to access class member from other class



sector
14th July 2006, 16:09
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



#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
QStringList myAppList;

private:
Ui::myAppDLG ui;

};

#endif


qmytreewidget.cpp



#include <QtGui>
#include "qmytreewidget.h"
#include "myApp.h"


QMyTreeWidget::QMyTreeWidget(QWidget *parent)
: QTreeWidget(parent)
{

}


bool QMyTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
{
QTreeWidgetItem *item;
QList <QUrl> urlList;

urlList = data->urls();

foreach(QUrl url, urlList)
{
item = new QTreeWidgetItem(this);
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;
}


QStringList QMyTreeWidget::mimeTypes () const {

QStringList qstrList;
qstrList.append("text/uri-list");
return qstrList;
}

jacek
14th July 2006, 16:43
You can use signals & slots mechanism for this:

class QMyTreeWidget : ...
{
Q_OBJECT
public:
...

signals:
void fileDropped( const QString& );
...
};

bool QMyTreeWidget::dropMimeData(...)
{
...
foreach(QUrl url, urlList)
{
item = new QTreeWidgetItem(this);
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_fileDroppe d( const QString& fileName )
{
// do something with fileName
}

sector
14th July 2006, 17:02
Many thanks for your help.

Have a nice day.

sector
14th July 2006, 22:10
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 ?



bool QMyTreeWidget::dropMimeData(...)
{
QString string="abcdef";
QString retStr;
...
// 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?

jpn
14th July 2006, 22:30
A method must be declared as static to be able to call it without instantiating an object:



// header
class Util
{
public:
static QString doSomeTrick();
};




// implementation
QString Util::doSomeTrick()
{
return QString("blaa");
}


Now you can use it like this:


#include "util.h"
QString trick = Util::doSomeTrick();

jacek
14th July 2006, 22:42
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.


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.


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.