1. Trang chủ
  2. » Giáo án - Bài giảng

molkentin-the book of qt 4-the art of building qt applications

442 455 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 442
Dung lượng 6,37 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 ofQt 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 [...]... 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 new painting engine in Qt 4 It is called Arthur the paint engine, after “Thomas the Tank Engine,”1 and it really is “the paint engine that could!” Arthur has developed into a state -of- the -art painting engine that makes it possible... desktops under Linux But Qt is also used by a substantial number of companies: Google Earth is based on Qt, as is the telephony software Skype and the video editing program MainActor When Trolltech published a pre-version of Qt 4 in 2005, I started trying out several of the new functionalities and was very impressed For the first time there was a uniform licensing scheme for variations of Qt, which until then... version of the library But if the company is developing proprietary programs, then it pays for Qt license fees, thus supporting the development of the toolkit, and receives support from the manufacturer This structural level is of relevance as far as the licensing of the commercial Qt version Trolltech has three editions of Qt 4 available: Qt Console for nongraphic development, and Qt Desktop Light and Qt. .. restricted in any way in terms of size 2 The article was written by Matthias Ettrich, the founder of the KDE project 19 Introduction This book is based on the open source edition of Qt, but it can also be used without problem by those who have purchased the commercial version The embedded version of Qt (Qtopia Core) is not covered in the book, because although the API is identical, apart from a few extra classes,... evident in the hallways of our of ces Haavard and I have left the control of Qt s destiny to much more capable programmers than ourselves And I have to say they are doing an extraordinary job at 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... hard to beat Instead, the book aims to explain contexts and basic techniques by the use of examples, and to simplify getting started with programming, just as that magazine article back then helped me when I was starting to get to grips with Qt Note of Thanks It would have been impossible to read this book without a number of people giving me support with their advice In particular, Patricia Jung and... 3 Please make sure that you really do use the Qt 4 qmake, which differs significantly from the Qt 3 version Use of the latter on Qt 4 projects causes errors Many Linux distributions contain both Qt 3 and Qt 4; in Ubuntu Breezy Badger and Dapper Drake, for example, qmake is linked by default to qmake -qt3 The Qt 4 version of the tool can be run with qmake -qt4 ; if you seldom need the third edition, change... most difficult part of Qt to get right, and also the most visible, is the graphical user interface (GUI) The foundation for any GUI is the technology used to render the interface, and Qt contains its own technology for this purpose—namely “Arthur the Paint Engine.” Partly inspired by “Thomas the Tank Engine,” Arthur has come a long way in recent years Its story is a good example of how Qt has continued... into native code, not least for reasons of performance The book assumes that you have a fundamental knowledge of C++ An interested reader should be familiar with the concepts of pointers and templates The book also assumes that you know about things such as the overloading of operators Knowledge of the Standard Template Library (STL) in particular is not expected Qt provides its own classes for the most... recompiling will make it run on all platforms supported by Qt From the very first version of Qt released back in May 1995, Trolltech has been using a dual-licensing business model with a free version of Qt available for developers of free and open source software The first toe in the water back then was a binary-only version for development of free software under Linux only This was quickly followed by a . 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. 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. BuildingQtApplications ® Munich SanFrancisco The Book of Qt 4: The ArtofBui ldingQtApplications. Copyright c  2007OpenSource PressGmbH Allrightsreserved.Nopartofthisworkmaybe reproducedortransmittedinanyform

Ngày đăng: 28/04/2014, 16:56

TỪ KHÓA LIÊN QUAN