PDA

View Full Version : want to use the resultat of a function in another function



lucie1985
19th July 2015, 21:42
hello people :) . i need some help.
I have a mainWindow(1rst Function of .cpp) with a menubar. the menubar help to choise a directory with SiGNAL & SLOT (2nd Function). The directory is for example : home\users\...... .
My problem : I want to pass the directory-value(here a QString) to another function!!! But it is not happened.

here is my code:






mainwindow.h
class MainWindow:public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);

QString getDirName() const;

public slots:
void selectdirectory() ;

private:
QMenu *menuFile;
QAction *Dateipath;

};
#endif // MAINWINDOW_H






mainwindow.cpp

MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
menuFile = menuBar()->addMenu("&File");

Dateipath = new QAction("&Select directory", this);
menuFile -> addAction(Dateipath);
Dateipath->setShortcut(QKeySequence("ctrl+s"));

QObject::connect(Dateipath, SIGNAL(triggered()),this, SLOT(selectdirectory()));
}






void MainWindow::selectdirectory()
{

QString mydirectory= "home\users\........"

}




QString MainWindow::getDirName() const
{
I need QString mydirectory here
}

mikag
19th July 2015, 22:24
You can declare mydirectory as a member of the MainWindow class and then it will be available in all other member functions.
Not really Qt issue but more a C++/programming knowledge issue...

...

private:
QMenu *menuFile;
QAction *Dateipath;
QString mydirectory; // Add this

...

void MainWindow::selectdirectory()
{
// don't declare a local variable here, assign the value to the member variable
mydirectory = "home\users\........"
}

QString MainWindow::getDirName() const
{
return mydirectory; // and there we have it
}
...

There are many ways to do this, but this will do what you asked for...

lucie1985
21st July 2015, 23:31
thank you very much mikag. But it's not happen.
the problem is: I use the return (mydirectory) in another Cpp-Directory.
When I do like you, I get "the program has crashed". This is very confusing. May be i should use another way to transfer this Value "mydirectory"



when I try to do like this, it works: I mean, I get my result in the other cpp

void MainWindow::selectdirectory()
{
}

QString MainWindow::getDirName() const
{
QString mydirectory = "home\users\........" ;
return mydirectory;
}

anda_skoa
22nd July 2015, 07:30
If you want help with code that does not work, you will have to show the code that does not work, not code that works but does something different then what you need.

Cheers,
_