Qt Designer vs Handcoding

后端 未结 6 1141
再見小時候
再見小時候 2021-01-30 04:21

Every time I start a project with some graphical toolkit, one of the first conflicts happen with the decision of how to deal with the visual design and the widget layout: A grap

6条回答
  •  孤城傲影
    2021-01-30 04:51

    My answer is based on two years developing biochemistry applications using PyQt4 (Python bindings to Qt 4) and OpenGL. I have not done C++ Qt, because we only used C++ for performance-critical algorithms. That said, the PyQt4 API greatly resembles Qt4, so much here still applies.

    Qt Designer

    • Good
      • Exploration. Discover what widgets are available, the names for those widgets, what properties you can set for each, etc.
      • Enforces separation of UI logic from application logic.
    • Bad
      • If you need to add or remove widgets at run-time, you have to have that logic in code. I think it's a bad idea to put your UI logic in two places.
      • Making changes to nested layouts. When a layout has no widgets in it, it collapses, and it can be really hard to drag and drop a widget in to the location you want.

    Hand coding

    • Good

      • Fast if you are very familiar with Qt.
      • Best choice if you need to add or remove widgets at run-time.
      • Easier than Qt Designer if you have your own custom widgets.
      • With discipline, you can still separate UI layout from behavior. Just put your code to create and layout widgets in one place, and your code to set signals and slots in another place.
    • Bad

      • Slow if you are new to Qt.
      • Does not enforce separation of layout from behavior.

    Tips

    • Don't just jump into creating your windows. Start by quickly sketching several possible designs, either on paper or using a tool like Balsamiq Mockups. Though you could do this in Qt Designer, I think it is too tempting to spend a lot of time trying to get your windows to look just right before you've even decided if it is the best design.

    • If you use Qt Designer for PyQt, you have the extra step of running pyuic4 to compile your *.ui files to Python source files. I found it easy to forget this step and scratch my head for a second why my changes didn't work.

    • If you code your UI by hand, I suggest putting your layout code in one place and your signals and slots in another place. Doing this makes it easier to change the way your widgets are arranged on a window without affecting any of your application logic. Or you can change some behavior without having to wade through all the layout code.

    Enjoy Qt! Now that I am using Java Swing for work, I miss it.

提交回复
热议问题