Results 1 to 9 of 9

Thread: Blonde Moment

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Blonde Moment

    I'm having a blonde moment and I can't figure out something that I know is simple.

    I have a standard private slot:

    void function : : on_PushButton_clicked() //ps I used spaces here because otherwise it shows up as smileys on the forum.
    {
    QString address = ui->LineEdit->text();
    }

    and I have created a class called fileManager which has a function fileOpen.

    How do I get this to work?

    fileManager data;
    data.fileOpen(address);

    At the moment, I've got a permission error due to the clicked slot being private.

    Can I use pointers? I'm not sure how to use them 100% quite yet so after trying this:

    fileManager data
    data.fileOpen(*address)

    and then

    void fileManager::fileOpen(QString &fileDirectory)
    {
    QDir directory(fileDirectory);
    QMessageBox currentlFileDirectory;
    currentlFileDirectory.setInformativeText(fileDirec tory);
    int ret = currentlFileDirectory.exec();
    }

    This doesn't seem to work, so maybe I'm not declaring something correctly in respective header files?
    Last edited by Atomic_Sheep; 28th January 2012 at 06:07.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Blonde Moment

    Your slot declares a local variable that goes out of scope immediately so you can't use it in other methods. Declare the address variable as a member of the class and skip the asterisk when accessing it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Blonde Moment

    I don't understand why, but what you suggested, worked... no surprises there, thanks very much. Now I just gotta figure out why it works.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Blonde Moment

    Consider this:

    Qt Code:
    1. class A {
    2. public:
    3. void method1() { QString x = "one"; }
    4. void method2() { QString x = "two"; }
    5. QString getter() const { return x; }
    6. };
    To copy to clipboard, switch view to plain text mode 

    What do you think, what would the value returned by calling getter() be and why?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Blonde Moment

    Well, if you do this:

    const A AClass;
    AClass.getter();

    Then you should get back whatever x was assigned to in the definitions part of the class which is impossible to determine from the above information because you didn't provide the assgined value to x?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Blonde Moment

    What definition part of the class? "x" is not defined anywhere in the class. Now look at your original code, you have exactly the same situation. Did your code even compile?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Blonde Moment

    Well, I still don't quite understand your question as I'm still not fully grasping the idea of const quite yet, however I did understand why what you suggested worked...

    Basically, when I call a function from another class from within the first class:

    fileManager data;
    data.fileOpen(address);

    when the fileOpen(QString fileAddress function starts), all that happens is a new object is created called fileAddress and the QString address = ui->LineEdit->text() value is placed into that variable.

    So really basic, I just got confused when seeing more complex paths and variables.

    However getting back to your example, even though as I said... I'm not too sure, I think the value you will get when you call getter() will be 'x' as getter is a QString and I think all the const does is that the caller will not be able to alter the string name from 'x'.

  8. #8
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Blonde Moment

    What happens is that you define a new 'x' every time you call any of those two methods. That x is only available in that method and it is destroyed when it finishes.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Blonde Moment

    Quote Originally Posted by Atomic_Sheep View Post
    However getting back to your example, even though as I said... I'm not too sure, I think the value you will get when you call getter() will be 'x' as getter is a QString and I think all the const does is that the caller will not be able to alter the string name from 'x'.
    The point is this example will not even compile, it's just semantically incorrect. In C++ the fact that you give things the same name doesn't mean they are related in any way. I can create hundreds of variables called x that will have different values. See the code below:

    Qt Code:
    1. int x;
    2.  
    3. class A {
    4. public:
    5. A() { int x; x = 8; this->x = 7; }
    6. void setX(int x) { this->x = x; }
    7. void getX() const { return x; }
    8. void method() {
    9. int x;
    10. {
    11. double x = 7.8;
    12. {
    13. int x = 5;
    14. }
    15. }
    16. }
    17. private:
    18. int x;
    19. };
    20.  
    21. class B {
    22. public:
    23. B() { x = 7; }
    24. void method() { int x = 8; }
    25. private:
    26. int x;
    27. };
    28.  
    29. int func() { int x = 2; return x; }
    To copy to clipboard, switch view to plain text mode 

    I suggest you analyze it until you fully understand it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Getting the moment when resize is finished
    By m_e in forum Qt Programming
    Replies: 4
    Last Post: 7th November 2007, 21:14

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.