Results 1 to 4 of 4

Thread: want to use the resultat of a function in another function

  1. #1
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default want to use the resultat of a function in another function

    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 MainWindowublic 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
    }

  2. #2
    Join Date
    Jul 2015
    Location
    Sweden
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: want to use the resultat of a function in another function

    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...

    ...
    Qt Code:
    1. private:
    2. QMenu *menuFile;
    3. QAction *Dateipath;
    4. QString mydirectory; // Add this
    To copy to clipboard, switch view to plain text mode 
    ...
    Qt Code:
    1. void MainWindow::selectdirectory()
    2. {
    3. // don't declare a local variable here, assign the value to the member variable
    4. mydirectory = "home\users\........"
    5. }
    6.  
    7. QString MainWindow::getDirName() const
    8. {
    9. return mydirectory; // and there we have it
    10. }
    To copy to clipboard, switch view to plain text mode 
    ...

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

  3. #3
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: want to use the resultat of a function in another function

    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;
    }

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: want to use the resultat of a function in another function

    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,
    _

Similar Threads

  1. Replies: 2
    Last Post: 2nd December 2013, 04:43
  2. pass member function as argument int template function
    By bibhukalyana in forum General Programming
    Replies: 1
    Last Post: 12th March 2013, 07:05
  3. Replies: 11
    Last Post: 5th September 2012, 20:47
  4. Replies: 4
    Last Post: 2nd August 2012, 07:42
  5. Replies: 0
    Last Post: 10th March 2010, 08:13

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.