Book of Qt 4 : the Art of Building Qt Applications
Total Page:16
File Type:pdf, Size:1020Kb
The Book of Qt 4 Daniel Molkentin TheBook of Qt 4 TheArt of BuildingQtApplications ® Munich SanFrancisco The Book of Qt 4: The ArtofBuildingQtApplications. Copyright " c 2007OpenSource PressGmbH Allrightsreserved.Nopartofthisworkmaybe reproducedortransmittedinanyform or byanymeans, electronic or mechanical, includingphotocopying, recording, or byanyinformationstorageorretrieval system,without theprior writtenpermission of thecopyright ownerand thepublisher. Printedonrecycled paperinthe United States of America. 12345678910—10090807 No Starch Pressand theNoStarch Presslogo areregisteredtrademarksofNoStarch Press, Inc. Otherproduct and companynamesmentioned herein maybe thetrademarksoftheir respectiveowners.Ratherthanuse atrademark symbol witheveryoccurrence of atrademarked name,weare usingthe namesonlyin an editorialfashionand to the benefit of thetrademark owner, withnointention of infringement of thetrademark. Publisher: William Pollock Cover Design:Octopod Studios U.S. editionpublishedbyNo Starch Press, Inc. 555 De HaroStreet,Suite 250,San Francisco, CA 94107 phone: 415.863.9900;fax:415.863.9950;[email protected];http://www.nostarch.com Original edition " c 2006OpenSource PressGmbH PublishedbyOpenSource PressGmbH, Munich, Germany Publisher: Dr.Markus Wirtz Original ISBN 978-3-937514-12-3 For informationontranslations, pleasecontact OpenSource PressGmbH, Amalienstr.45Rg, 80799 M unche¨ n, Germany phone+49.89.28755562; fax+49.89.28755563; [email protected];http://www.opensourcepress.de Theinformationinthisbook is distributed on an “AsIs” basis, without warranty.While everyprecaution hasbeen takeninthe preparationofthiswork, neitherthe author norOpenSource PressGmbHnor No Starch Press, Inc. shall haveanyliabilityto anyperson or entitywithrespect to anyloss or damage caused or allegedtobecaused directly or indirectlybytheinformationcontained in it. LibraryofCongressCataloging-in-Publication Data Molkentin, Daniel [Qt4, Einf¨uhrung in die Applikationsentwicklung.English] The book of Qt4: the art of building Qtapplications/byDaniel Molkentin.-- 1st ed. p. cm. Includesindex. ISBN-13978-1-59327-147-3 ISBN-101-59327-147-6 1.Qt(Electronic resource)2.Graphicaluserinterfaces(Computersystems) 3. Application software--Development. I.Title.QA76.9.U83M62132007 005.4’37--dc22 2007013181 Contents Introduction 19 Preparations23 1Basics, Tools, andFirst Code 25 1.1Our FirstQtProgram .......................... 25 1.1.1CompilingaQtProgram ................... 27 1.2Layouts,Object Hierarchy,and MemoryManagement ........ 29 1.2.1Howto ArrangeWidgets Automatically........... 29 1.2.2MemoryManagement in Object Hierarchies........ 31 1.2.3Other LayoutTypes...................... 33 1.3Signals andSlots ............................ 35 1.3.1The SimplestCase: ASlotResponds to aSignal...... 35 1.3.2Signals CarryingAdditional Information andHowThey AreProcessed. ........................ 36 1.4BaseClassesinQt........................... 39 1.4.1ClassesDerived from QObject ................ 39 1.4.2QString andOther Classesnot Derived from QObject ... 40 1.4.3The Qt InheritanceHierarchy................ 41 1.5QtataGlance ............................. 42 1.5.1The Qt Libraries........................ 42 1.5.2Toolsand Utilities...................... 47 1.5.3Examples andDemos ..................... 58 1.6Howto Use theDocumentation................... 59 5 Contents 2The ToolsNeeded to Create Dialogs61 2.1What’sthe DifferenceBetween Dialogsand Widgets?....... 62 2.1.1Inheriting from QObject ................... 64 2.1.2MoreComplexLayouts .................... 65 2.1.3IncreasingUsability..................... 68 2.1.4ImplementingSlots ..................... 70 2.2SeparationofGUI andProcessing Logic............... 74 2.2.1AlternativeDesign...................... 74 2.2.2Declaring andSending OutSignals ............. 76 2.2.3Using Your OwnSignals ................... 79 3GUI Design Using theQtDesigner81 3.1Dialogs “ByMouseClick” ....................... 81 3.1.1Making Layouts With theDesigner............. 84 3.1.2The PropertyEditor. ..................... 85 3.1.3The Preview.......................... 88 3.1.4Signal/SlotConnections ................... 88 3.1.5The TabSequence...................... 89 3.1.6Shortcuts andBuddies .................... 90 3.2Integrating Designer-generated FilesintoYourQtProject ..... 91 3.2.1Using Designer-generated ClassesasHelperClasses.... 92 3.2.2AlwaysHavingDesigner-generated Widgets Available ... 94 3.2.3MultipleInheritance..................... 95 3.3Automatic Signal/SlotConnections .................. 97 3.4Including Derived Classesinthe Designer .............. 99 3.5The Resource Editor .......................... 99 4Developing aGUI Application BasedonaMain Window 101 4.1The Anatomyof theMainWindow.................. 101 4.2DerivingfromQMainWindow..................... 103 4.3CreatingaMain Windowwiththe Qt Designer........... 106 4.3.1Adding Menu Bars ...................... 107 4.3.2RecyclingActions in theToolbar.............. 108 6 Contents 4.3.3Integrating theMainWindowwithYourSourceCode ... 110 4.4Making theMostofthe Status Bar.................. 118 4.4.1TemporaryMessages..................... 120 4.4.2NormalMessages. ...................... 120 4.4.3Permanent Messages. .................... 121 4.5Toolbars ................................ 125 4.6HowDo ActionsWork? ........................ 126 4.6.1Howto InstantiateQAction Manually............ 127 4.6.2Selectable Actions...................... 128 4.6.3Grouped Actions....................... 128 4.7DockWindows............................. 130 4.7.1Positioning Dock Windows. ................. 131 4.7.2ADock Windowfor OurEditor............... 133 4.8SavingPreferences ........................... 136 4.8.1Extending CuteEdit. ..................... 139 5Laying OutWidgets 141 5.1ManualLayout............................. 141 5.2Automatic Layout........................... 143 5.2.1Horizontaland Vertical Layout............... 144 5.2.2GridLayout.......................... 148 5.2.3Nested Layouts ........................ 149 5.3Splitter ................................. 150 5.3.1BehaviorDuringSizeChanges ................ 150 5.3.2SavingSplitter Positionsand Determining theWidgetSize151 5.3.3Defining RelativeSizes .................... 152 5.3.4CustomizingHandles ..................... 153 5.3.5Layoutfor LanguagesWritten from RighttoLeft..... 156 5.4StackedLayouts ............................ 157 5.4.1The Alternative: Stacked Widgets .............. 157 5.4.2WhentoUse Stacked Layouts andWidgets ......... 157 7 Contents 6Dialogs161 6.1Modal Dialogs............................. 161 6.2Non-modal Dialogs.......................... 163 6.2.1UsabilityProblems ...................... 163 6.3Semi-modal Dialogs.......................... 164 6.4Avoiding Bloated Dialogs....................... 164 6.5Ready-made DialogsinQt...................... 166 6.5.1Message Dialogs....................... 166 6.5.2Error MessagesThatAre OnlyVisible Once ......... 174 6.5.3File SelectionDialogs ..................... 175 6.5.4Input Dialogs......................... 179 6.5.5FontSelection Dialog .................... 182 6.5.6Color Selectionand Printing Dialog ............. 183 7Events, Drag andDrop, andthe Clipboard185 7.1EventLoop andEventHandler .................... 185 7.2HandlingEvents ............................ 186 7.2.1Using Specialized EventHandlers.............. 186 7.2.2Using theGeneral EventHandler .............. 189 7.3Using EventFilters........................... 190 7.4Dragand Drop ............................. 194 7.4.1MIMETypes.......................... 194 7.4.2The Drag Side......................... 196 7.4.3The Drop Side. ........................ 198 7.5The Clipboard............................. 201 8Displaying Data Using “Interview”207 8.1UnderlyingConcepts......................... 208 8.1.1The ViewClasses....................... 210 8.1.2The Model Classes...................... 211 8.2DisplayingDirectoryHierarchies................... 212 8.2.1Using ViewClassesinthe Designer ............. 214 8.2.2Implementingthe Functionalityof theFile SelectionDialog216 8 Contents 8.3The String ListsModel......................... 221 8.4ImplementingYourOwnModels ................... 222 8.4.1AnAddressBook Model................... 222 8.4.2Making Your OwnModels Writable ............. 227 8.5Sorting andFilteringDatawithProxy Models............ 231 8.5.1Adjustments to theUserInterface............. 232 8.6Making EntriesSelectable withCheckboxes ............. 234 8.7Designing Your OwnProxy Models.................. 237 8.8ImplementingDragand Drop in Models............... 241 8.9YourOwnDelegates .......................... 245 8.10WithoutYourOwnDataSource: TheStandardModel. ....... 249 8.11 Element-basedViewsWithout Model Access ............ 251 8.11.1Items ............................. 251 8.11.2The List View......................... 251 8.11.3The Tree View......................... 252 8.11.4The Table View........................ 253 8.11.5Cloning Items. ........................ 254 9The QtSqlModule 257 9.1Structure of theQtSql Module .................... 257 9.2Selecting theAppropriate Driver ................... 258 9.3Making aConnection......................... 260 9.4Making Queries............................ 261 9.5Transactions .............................. 264 9.6EmbeddedDatabases ......................... 264 9.7Using SQLModelClasseswithInterview............... 265 9.7.1DisplayingSQL TablesWithout ForeignKeysinTable and Tree Views. .......................... 265 9.7.2ResolvingForeign KeyRelations............... 266 9.7.3DisplayingQueryResults.