PDA

View Full Version : Separating GUI into classes



ShamusVW
3rd February 2010, 14:14
I am wanting to neaten up my classes, and need some advise how to proceed.
Currently in my main file I have


{
QApplication app(argc, argv);
Client client;
client.show();
return client.exec();
}

In my Client constructor I have


QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(inputOptionsGroup(), 1, 0, 1, 2);
mainLayout->addWidget(resultsGroup(), 2, 0, 1, 2);
setLayout(mainLayout);

and then in resultsGroup (similar to inputOptionsGroup)


QGroupBox *Client::resultsGroup()
{
eccentricityLabel = new QLabel(tr("<b>Eccentricity</b>"));
diameterLabel = new QLabel(tr("<b>Diameter</b>"));
eccentricityLineEdit = new QLineEdit("0");
eccentricityLineEdit->setReadOnly(true);
diameterLineEdit = new QLineEdit("0");
diameterLineEdit->setReadOnly(true);
connectionStateLabel = new QLabel("<font color='red'><b>State : Not connected to camera</b></font>");
dbConnectionLabel = new QLabel("<font color='purple'><b>Connecting to MySQL DB...</b></font>");
countLabelPass = new QLabel("<font color='green'><b>Pass : 0</b></font>");
countLabelFail = new QLabel("<font color='red'><b>Fail (Con/Dia/Total) : 0/0/0</b></font>");

queryLineEdit = new QLineEdit;

QGridLayout *vbox = new QGridLayout;
vbox->addWidget(eccentricityLabel, 0, 0, 1, 1);
vbox->addWidget(eccentricityLineEdit, 0, 1, 1, 1);
vbox->addWidget(diameterLabel, 1, 0, 1, 1);
vbox->addWidget(diameterLineEdit, 1, 1, 1, 1);
vbox->addWidget(connectionStateLabel, 2, 0, 1, 2);
vbox->addWidget(dbConnectionLabel, 3, 0, 1, 2);
vbox->addWidget(queryLineEdit, 4, 0, 1, 2);
vbox->addWidget(countLabelPass,5, 0, 1, 2);
vbox->addWidget(countLabelFail,6, 0, 1, 2);

QGroupBox *groupBox = new QGroupBox(tr("Results"));
groupBox->setLayout(vbox);

return groupBox;
}

Essentially I am splitting up the various pieces of the GUI setup into separate functions. I would instead like to move the whole GUI setup to a separate class. I know this is probably simple to some, but I don't know how to do it, and I have been searching.

My fear is that I am going to fall into problems trying to access various components on the GUI, but I would assumedly get round this using slots/signals.

Thanks.

aamer4yu
3rd February 2010, 15:05
You can have a look at how Qt generates code out of .ui files.
You can have something similar if it fits you.