PDA

View Full Version : Is it normal to have a huge MainWindow constructor?



wayfaerer
18th April 2012, 01:32
My MainWindow file is huge. It used to be over 1200 lines of code, but I've since cut it down to about 600 by delegating responsibilities to other classes. Now over a third of the MainWindow file is the constructor, which is a lot of this stuff:



latticeView = new QGraphicsView(this);
latticeView->setFocusPolicy(Qt::NoFocus);
latticeView->setRenderHint(QPainter::Antialiasing, true);
latticeView->setResizeAnchor(QGraphicsView::AnchorViewCenter);
latticeView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
latticeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn );
latticeView->setViewportUpdateMode(QGraphicsView::FullViewportU pdate);


i.e., just setting up all the widgets, the actions, the menus, connecting the signals/slots, etc.

I don't know if this does or does not make sense from a design perspective. I wish it would be nicer. Should I:

(1) leave it; it works fine, anyways, and maybe a huge MainWindow constructor is normal
(2) subclass all of the widgets so that they set themselves up in their own constructors
(3) make some type of class to do all this setup stuff
(4) do something else

I'd really appreciate some advice! Note that I'm not using Qt Designer, and prefer to keep it that way.

ChrisW67
18th April 2012, 02:46
The code to construct the UI has to exist somewhere. If you use Designer it exists in the generate ui_*.h file, in your case it exists in your class.

There is no perfect answer as to how you arrange it. You might like to improve maintainability by splitting the UI construction code into several private member functions you call from the constructor:


void createActions() {...}
void createMenus() {...}
void createToolBars() {...}
void createStatusBar() {...}
void createLayouts() {...}
// ... you get the picture. The break down should suit your UI.


If you are going to create the same widget several times (esp. from other classes) then you could create a dedicated sub class or just a private factory function.