1. Trang chủ
  2. » Công Nghệ Thông Tin

The book of qt 4 the art of building qt applications - phần 1 ppt

45 388 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 45
Dung lượng 1,97 MB

Nội dung

Cross-platform development is a kind of holy grail, and Trolltech’s Qt toolkit may well be the most promising solution yet to this development challenge. Qt is widely used for the development of GUI applications as well as console tools and servers, and it’s especially appeal- ing to programmers who need to write cross-platform applications to run on Linux/Unix, Mac, and Windows machines without having to rewrite for each platform. The Book of ™ Qt 4 offers an in-depth explanation of Qt 4 that goes beyond the common focus on C++ program- ming. Author Daniel Molkentin uses practical examples to explain features like the signal/slot concept and the event system, as he guides you through developing applications with and without Qt’s graphical GUI builder, Qt Designer. And as a core KDE developer, Molkentin’s head is full of real-world problems and solutions that he peppers liberally throughout The Book of Qt 4, making it a resource that you’ll consult time and again. You’ll find coverage of: • Tools for creating dialogs • GUI design with the Qt Designer • Widget layout and dialog construction www.nostarch.com THE FINEST IN GEEK ENTERTAINMENT ™ SHELVE IN: PROGRAMMING $54.95 ($65.95 CDN) • Data visualization using Qt’s model/view concept • The QtSql module and the graphics library Arthur • File handling, XML, processes, and network connections • Databases and threading • Event handling and using drag and drop • Internationalization and debugging As well as lots of useful hints on how to use Qt’s data- types, containers, and algorithms and how to develop user-friendly applications. Whether you’re already a Qt developer or you’re just considering Qt for cross-platform development, you’ll find The Book of Qt 4 to be indispensable. ABOUT THE AUTHOR Daniel “danimo” Molkentin has been a core developer with the KDE project since 2000, and he’s the co-author of the PIM application Kontact, among other applications. He was actively involved in the development of Qt 4 as a beta tester, and he worked with the initial port of KDE to the new Qt version. Qt 4. FROM THE BASICS THROUGH APPLICATION DESIGN. Qt 4. FROM THE BASICS THROUGH APPLICATION DESIGN. “I LAY FLAT.” This book uses RepKover— a durable binding that won’t snap shut. Printed on recycled paper Qt 4 Qt 4 THE ART OF BUILDING Qt APPLICATIONS DANIEL MOLKENTIN THE BOOK of THE BOOK of Qt 4 THE BOOK of THE BOOK of Qt 4 ® MOLKENTIN The Book of Qt 4 Daniel Molkentin TheBook of Qt 4 TheArt of BuildingQtApplications ® Munich SanFrancisco The Book of Qt 4: The ArtofBui ldingQtApplications. 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 Am erica. 12345678910—10090807 No Starch Pressand theNoStarch Presslogo areregisteredtrademarksofNoStarch Press, Inc. Otherproduct and companynamesmentioned herein maybe thetrademarksoftheir respectiveowners .Ratherthanuse atra demark symbol witheveryoccurrence of atrademarked name,weare usingthe na mesonlyin 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;info@nostarch.com;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 ¨ unchen, Germany phone+49.89.28755562; fax+49.89.28755563; info@opensourcepress.de;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 Sim plestCase: 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 La youts 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 Fu nctionalityof theFile SelectionDialog216 8 [...]... 2 41 8.9 Your Own Delegates 245 8 .10 Without Your Own Data Source: The Standard Model 249 8 .11 Element-based Views Without Model Access 2 51 8 .11 .1 Items 2 51 8 .11 .2 The List View 2 51 8 .11 .3 The Tree View 252 8 .11 .4 The Table View 253 8 .11 .5 Cloning... 307 10 .10 .2 Painter Paths 309 10 .10 .3 Composition Modes 310 11 Input/Output Interfaces 317 11 .1 The QIODevice Class Hierarchy 317 11 .1. 1 Derived Classes 318 11 .1. 2 Opening I/O Devices 319 10 Contents 11 .2 Access to Local Files 320 11 .3 Serializing Objects... 322 11 .3 .1 Defining Serialization Operators 325 11 .3.2 Saving Serialized Data to a File and Reading from It 326 11 .4 Starting and Controlling Processes 328 11 .4. 1 Synchronous Use of QProcess 328 11 .4. 2 Asynchronous Use of QProcess 330 11 .5 Communication in the Network 332 11 .5 .1 Name Resolution with... 13 .2 The DOM API 366 13 .2 .1 Reading in and Processing XML Files 367 13 .2.2 Searching for Specific Elements 370 13 .2.3 Manipulating the DOM Tree 3 71 13.2 .4 The DOM Tree as XML Output 372 11 Contents 14 Internationalization 375 14 . 1 Translating Applications into Other Languages 375 14 . 1. 1 Preparing the. .. 376 14 . 1. 2 Processing Translation Sources with Linguist 377 14 . 1. 3 Using Translations in the Program 378 14 . 1 .4 Adding Notes for the Translation 380 14 . 1. 5 Specifying the Translation Context 380 14 . 1. 6 Internationalizing Strings Outside Qt Classes 3 81 Appendixes 383 A Debugging Help 385 A .1 Debugging Functions 385 A .1. 1... 40 3 Queues (QQueue) 40 4 Associative Arrays 40 4 B .4. 1 12 Dictionaries (QMap) 40 4 Contents B .4. 2 B .4. 3 Hash Tables with QHash 40 9 B .4. 4 B.5 Allowing Several Identical Keys (QMultiMap) 40 7 Hash-based Amounts with QSet 41 1 Algorithms 41 2 B.5 .1 The foreach... 333 11 .5.2 Using QTcpServer and QTcpSocket 333 12 Threading with QThread 337 12 .1 Using Threads 338 12 .2 Synchronizing Threads 3 41 12 .2 .1 The Consumer/Producer Pattern 342 12 .3 Thread-dependent Data Structures 345 12 .4 Using Signals and Slots Between Threads 347 12 .5 Your Own Event... 41 2 B.5.2 Sorting 41 3 B.5.3 Sorting in Unsorted Containers 41 4 B.5 .4 Copying Container Areas 41 5 B.5.5 Binary Search in Sorted Containers 41 6 B.5.6 Counting the Number of Occurences of Equal Elements 41 8 B.5.7 Deleting Pointers in Lists 41 8 B.5.8 Checking that Data Structures Have Identical Elements 41 9... it Qt has developed into an exceptionally beautiful piece of software Today Matthias Ettrich (KDE founder) and Lars Knoll (of KHTML fame) lead the team of developers responsible for keeping Qt the kick-ass product you expect from Trolltech 15 Foreword to the English Edition Qt 4 is more or less a total rewrite of Qt And I know one of the parts the Trolltech developers are especially proud of is the. .. 12 .5 .1 Communication via Events Without a Thread-based Event Loop 352 13 Handling XML with QtXml 353 13 .1 The SAX2 API 3 54 13 .1. 1 How It Works 3 54 13 .1. 2 Reimplementing a Default Handler to Read RSS Feeds 355 13 .1. 3 Digression: Equipping the RSS Reader with a GUI and Network Capability 3 61 13.2 . paper Qt 4 Qt 4 THE ART OF BUILDING Qt APPLICATIONS DANIEL MOLKENTIN THE BOOK of THE BOOK of Qt 4 THE BOOK of THE BOOK of Qt 4 ® MOLKENTIN The Book of Qt 4 Daniel Molkentin TheBook of Qt 4 TheArt. 2 51 8 .11 .1Items 2 51 8 .11 . 2The List View 2 51 8 .11 . 3The Tree View 252 8 .11 . 4The Table View 253 8 .11 .5Cloning Items. 2 54 9The QtSqlModule 257 9.1Structure of theQtSql Module 257 9.2Selecting theAppropriate. Windowwiththe Qt Designer 10 6 4. 3.1Adding Menu Bars 10 7 4. 3.2RecyclingActions in theToolbar 10 8 6 Contents 4. 3.3Integrating theMainWindowwithYourSourceCode 11 0 4. 4Making theMostofthe Status Bar 11 8 4. 4.1TemporaryMessages

Ngày đăng: 13/08/2014, 08:21

TỪ KHÓA LIÊN QUAN