Results 1 to 4 of 4

Thread: Problem with pointers and methods

  1. #1
    Join Date
    Dec 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Problem with pointers and methods

    Hello. I am making a program, for a team at my high school, I am close to finishing it but each time I think I'm done with this source file some pointers kill all momentum at the moment I am trying to add a QFormLayout to a QWidget(the main window) using setLayout() but it seems I can't do that and use the .addRow(...) method. Here is my code:
    Qt Code:
    1. #include <QtGui>
    2. #include "QtCore"
    3.  
    4. void create(int argc, char *argv[]){
    5. QLineEdit *haveauto;
    6. QLineEdit *uprighted;
    7. QLineEdit *parked;
    8. QLineEdit *bowlball;
    9.  
    10. QLineEdit *lowgoal;
    11. QLineEdit *crategoal;
    12. QLineEdit *cratestack;
    13. QLineEdit *holdcrate;
    14. QLineEdit *magball;
    15. QLineEdit *bowlend;
    16.  
    17. QApplication app(argc, argv);
    18. QWidget window;
    19. window.resize(300, 300);
    20.  
    21. window.setWindowTitle(QApplication::translate("toplevel", "Main Recon Window"));
    22.  
    23. QLabel *autoLabel = new QLabel(QApplication::translate("auto", "AUTONOMOUS"));
    24.  
    25. haveauto = new QLineEdit();
    26. uprighted = new QLineEdit();
    27. parked = new QLineEdit();
    28. bowlball = new QLineEdit();
    29.  
    30. QFormLayout *auton;
    31. auton.addRow(QObject::tr(""), autoLabel);
    32. auton.addRow(QObject::tr("Do they have an Auto?"), haveauto);
    33. auton.addRow(QObject::tr("How many crates did they upright?"), uprighted);
    34. auton.addRow(QObject::tr("Where did they park?"), parked);
    35. auton.addRow(QObject::tr("Where did they park the bowling ball?"), bowlball);
    36.  
    37. lowgoal = new QLineEdit();
    38. crategoal = new QLineEdit();
    39. cratestack = new QLineEdit();
    40. holdcrate = new QLineEdit();
    41. magball = new QLineEdit();
    42. bowlend = new QLineEdit();
    43.  
    44. auton.addRow(QObject::tr("How many balls are in their low goal?"), lowgoal);
    45. auton.addRow(QObject::tr("How many balls did they drop in crates?"), crategoal);
    46. auton.addRow(QObject::tr("How many crates did they stack? Use either number circled."), cratestack);
    47. auton.addRow(QObject::tr("Can they hold crates?"), holdcrate);
    48. auton.addRow(QObject::tr("Can they find the magnet ball?"), magball);
    49. auton.addRow(QObject::tr("Did they get the bowling ball in either goal?"), bowlend);
    50.  
    51. window.setLayout(auton);
    52. window.show();
    53. }
    To copy to clipboard, switch view to plain text mode 
    The first thing that main() does is call create(...). I have been trying to solve this for a while now but it seems from what I've experienced that pointers make doing certain tasks mutually exclusive. Any help is welcomed and appreciated.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with pointers and methods

    Firstly, all of this stuff you have in create() after the QApplication is created is typically in your main window object's constructor. That is, the main window constructs itself, it is not built from outside as you are trying to do.

    Line 30 declares auton as a pointer to a QFormLayout... it is not a QFormLayout itself, and does not allocate one to point to. Trying to use this pointer will be a Bad ThingTM. (Think of the pointer like a piece of paper that can contain the address of a house, but currently is full of random letters. You need to identify (or build a new) house and write its address on the piece of paper before you can access the house.)

    Lines 31-35, and similar ones later, try to access member functions of QFormLayout as if auton were an an instance of QFormLayout (an actual house) rather than a pointer to (address of) one. That is:
    • auton.someFunction() is used if auton is an instance of an object, and
    • auton->someFunction() is used if auton is an pointer to an instance of an object.


    In general, your code can be simplified substantially.

  3. #3
    Join Date
    Dec 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with pointers and methods

    Ok I changed all of the .addRow's to ->addRow, and it didn't give me an error this time though windows is giving me a crash report now. And can you give me any tips on how to simplify my code?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with pointers and methods

    Quote Originally Posted by LastElemtnal View Post
    Ok I changed all of the .addRow's to ->addRow,
    Yes, it probably compiles now.
    and it didn't give me an error this time though windows is giving me a crash report now.
    You still have an invalid pointer (auton): you have to create an instance of QFormLayout for the pointer to point at.
    And can you give me any tips on how to simplify my code?
    For example:
    • The declarations at lines 5-15, and the initialisations at lines 25-28 and 37-42 can be combined (like line 23). (They can disappear altogether with a little more thought)
    • The code to construct the UI should be in a custom class constructor, which makes the window self-assembling from the point of view of the application.
    • Pushing UI code into a QWidget constructor has the side-effect of shortening QObject::tr() and QApplication::tr() to just tr() and saving some other typing.

Similar Threads

  1. Problem with passing Pointers to a Slot
    By Basti300 in forum Newbie
    Replies: 2
    Last Post: 26th May 2010, 14:45
  2. problem with a list of pointers
    By maider in forum Qt Programming
    Replies: 6
    Last Post: 30th November 2009, 12:45
  3. problem using a .h file's methods
    By sincnarf in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2007, 22:04
  4. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48
  5. K&R's strcpy function - problem with pointers
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 8th January 2006, 15:16

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.