Results 1 to 2 of 2

Thread: Is it normal to have a huge MainWindow constructor?

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Is it normal to have a huge MainWindow constructor?

    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:

    Qt Code:
    1. latticeView = new QGraphicsView(this);
    2. latticeView->setFocusPolicy(Qt::NoFocus);
    3. latticeView->setRenderHint(QPainter::Antialiasing, true);
    4. latticeView->setResizeAnchor(QGraphicsView::AnchorViewCenter);
    5. latticeView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    6. latticeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    7. latticeView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    To copy to clipboard, switch view to plain text mode 

    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.

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

    Default Re: Is it normal to have a huge MainWindow constructor?

    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:
    Qt Code:
    1. void createActions() {...}
    2. void createMenus() {...}
    3. void createToolBars() {...}
    4. void createStatusBar() {...}
    5. void createLayouts() {...}
    6. // ... you get the picture. The break down should suit your UI.
    To copy to clipboard, switch view to plain text mode 

    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.

  3. The following user says thank you to ChrisW67 for this useful post:

    wayfaerer (18th April 2012)

Similar Threads

  1. How to block the normal buttons of a QWidget
    By Momergil in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2012, 12:32
  2. Style sheets bug? Or normal behavior?
    By Syntho in forum Qt Programming
    Replies: 0
    Last Post: 7th June 2010, 09:16
  3. Replies: 1
    Last Post: 28th May 2010, 23:01
  4. Start application minimized or normal
    By matze in forum Qt Programming
    Replies: 3
    Last Post: 12th January 2010, 14:57
  5. can't new qwebview in a mainwindow constructor
    By billconan in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2009, 09:29

Tags for this Thread

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.