Results 1 to 7 of 7

Thread: Struct in a class

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

    Default Struct in a class

    Hi, I defined a struct in the default ui header file.

    In the clicked slot of the ui class, I assigned values to various struct members based on inputs received from the ui:

    e.g. struct.name = ui->lineedit->text();

    How do I access struct.name from a different class? I need to be able to compare the struct.name value to a string or int for example. Is it even possible to access things from a slot?

  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: Struct in a class

    This is a classic C++ problem, it has nothing to do with Qt. If you want to access object (not struct, you don't access structs but instances of it) B from A then B needs to be visible to A using any of the mechanisms provided by C++ (like passing pointers around).
    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: Struct in a class

    This is driving me insane... as I said, I created a struct within a class and assigned values to them, I then passed that struct... well the pointer to another function:

    Qt Code:
    1. //creating a new object
    2. object object;
    3.  
    4. object.fileopen(&variables);
    To copy to clipboard, switch view to plain text mode 

    in the other .cpp file which contains the function of fileopen, here's the code that I have:

    Qt Code:
    1. void FileManager::fileopen(struct properties *pointer)
    2. {
    3.  
    4. //code
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    However, I'm getting an error at compile:

    extra qualification 'File Manager' on member function 'fileopen'

    I included a function prototype in the header file for the filemanager.cpp file:

    void FileManager::fileopen(struct properties *pointer);

    What does this error mean? I checked the {} brackets, all seem to be in order. Either I'm using the pointers wrong or something else is happening which I don't understand... when I try to access the different members of my struct... they don't show up when I press ctrl+space, which suggests that the struct hasn't been passed properly...

    pointer->someKindOfVariable doesn't seem to work i.e. when I type pointer-> and press ctrl+space, no suggestions pop up, I've got a feeling the problems are linked.
    Last edited by wysota; 10th February 2012 at 09:32. Reason: missing [code] tags

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

    Default Re: Struct in a class

    Like wysota said it`s classic c++ problem. In your class where you define the struct and assign values, add some "getValue/s" methods...like:

    Qt Code:
    1. QString getTextFromLineEdit_1 (){ return this->ui->lineEdit_1->text ();}
    To copy to clipboard, switch view to plain text mode 

    then all you need to do from the other class is call:

    thisvariableneedstextfromthelineEdit = objectofyourclass.getTextFromLineEdit_1 ();

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Struct in a class

    Quote Originally Posted by Atomic_Sheep View Post
    I included a function prototype in the header file for the filemanager.cpp file:

    void FileManager::fileopen(struct properties *pointer);
    In your header file you should only have:
    Qt Code:
    1. void fileopen(struct properties *pointer);
    To copy to clipboard, switch view to plain text mode 
    The extra qualification is "FileManager::"

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

    Default Re: Struct in a class

    Quote Originally Posted by KillGabio View Post
    Like wysota said it`s classic c++ problem. In your class where you define the struct and assign values, add some "getValue/s" methods...like:

    Qt Code:
    1. QString getTextFromLineEdit_1 (){ return this->ui->lineEdit_1->text ();}
    To copy to clipboard, switch view to plain text mode 
    Doesn't that defeat the purpose of passing a struct by pointer if you have to create functions for each of the members within a struct? And method functions are for accessing certain functionality within classes? How is this related to passing a struct by pointer?

    Quote Originally Posted by norobro View Post
    In your header file you should only have:
    Qt Code:
    1. void fileopen(struct properties *pointer);
    To copy to clipboard, switch view to plain text mode 
    The extra qualification is "FileManager::"
    Thanks, changed that part.

    P.S. Now I'm getting an error of:

    within my clicked slot function, there is no matching function to call to filemanager::fileopen(otherfile::variable*, otherfile:ptions*)
    candidates are filemanager::fileopen(variable*, options*)

    The most frustrating part of programming... you always think you're right until you finally figure out where you went wrong, but it's so hard to know exactly what you are messing up/don't know/understand.
    Last edited by Atomic_Sheep; 10th February 2012 at 02:39.

  7. #7
    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: Struct in a class

    Drop the "struct" keyword in the method prototype.
    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. Replies: 4
    Last Post: 7th June 2011, 00:46
  2. Using global struct
    By David812 in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2011, 13:45
  3. Struct into a C++ class
    By franco.amato in forum General Programming
    Replies: 24
    Last Post: 30th September 2010, 16:13
  4. QDataStream class/struct & stream operators
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 19:40
  5. struct problem...
    By hiuao in forum General Programming
    Replies: 3
    Last Post: 5th April 2007, 07:48

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.