PDA

View Full Version : Problem with pointers and methods



LastElemtnal
8th December 2011, 00:06
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:


#include <QtGui>
#include "QtCore"

void create(int argc, char *argv[]){
QLineEdit *haveauto;
QLineEdit *uprighted;
QLineEdit *parked;
QLineEdit *bowlball;

QLineEdit *lowgoal;
QLineEdit *crategoal;
QLineEdit *cratestack;
QLineEdit *holdcrate;
QLineEdit *magball;
QLineEdit *bowlend;

QApplication app(argc, argv);
QWidget window;
window.resize(300, 300);

window.setWindowTitle(QApplication::translate("toplevel", "Main Recon Window"));

QLabel *autoLabel = new QLabel(QApplication::translate("auto", "AUTONOMOUS"));

haveauto = new QLineEdit();
uprighted = new QLineEdit();
parked = new QLineEdit();
bowlball = new QLineEdit();

QFormLayout *auton;
auton.addRow(QObject::tr(""), autoLabel);
auton.addRow(QObject::tr("Do they have an Auto?"), haveauto);
auton.addRow(QObject::tr("How many crates did they upright?"), uprighted);
auton.addRow(QObject::tr("Where did they park?"), parked);
auton.addRow(QObject::tr("Where did they park the bowling ball?"), bowlball);

lowgoal = new QLineEdit();
crategoal = new QLineEdit();
cratestack = new QLineEdit();
holdcrate = new QLineEdit();
magball = new QLineEdit();
bowlend = new QLineEdit();

auton.addRow(QObject::tr("How many balls are in their low goal?"), lowgoal);
auton.addRow(QObject::tr("How many balls did they drop in crates?"), crategoal);
auton.addRow(QObject::tr("How many crates did they stack? Use either number circled."), cratestack);
auton.addRow(QObject::tr("Can they hold crates?"), holdcrate);
auton.addRow(QObject::tr("Can they find the magnet ball?"), magball);
auton.addRow(QObject::tr("Did they get the bowling ball in either goal?"), bowlend);

window.setLayout(auton);
window.show();
}


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.

ChrisW67
8th December 2011, 00:48
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.

LastElemtnal
8th December 2011, 00:58
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?

ChrisW67
8th December 2011, 04:21
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.