the book of qt 4

442 545 0
the book of qt 4

Đ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

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 www.it-ebooks.info www.it-ebooks.info The Book of Qt 4 www.it-ebooks.info www.it-ebooks.info Daniel Molkentin TheBook of Qt 4 TheArt of BuildingQtApplications ® Munich SanFrancisco www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info [...]... 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 the corresponding link... 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 you expect from Trolltech 15 www.it-ebooks.info Foreword to the English Edition Qt 4 is more or less a total rewrite of Qt And I know one of the. .. written by Matthias Ettrich, the founder of the KDE project 19 www.it-ebooks.info 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, there are so many items to... 40 3 Queues (QQueue) 40 4 Associative Arrays 40 4 B .4. 1 Dictionaries (QMap) 40 4 12 www.it-ebooks.info 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... qmake generates only the Makefile file, which creates an executable of the program that includes debug output after make is run, unless the debug variants of the Qt libraries used by the program are missing.5 To ensure that these are installed, on Unix systems you should search for the library files with _debug.so in the name, for example, libQtCore_debug.so .4 for the debug version of the QtCore library In... use the subsequent QVBoxLayout::addWidget() calls to ensure that the QWidget object assumes parentage of each of the new labels (In fact, the GUI elements contained in a widget must be the children of the overlying widget For this reason, the QWidget object becomes the parent of the QLabel object, and not the 6 Of course, all allocated objects will go out of scope after exec() returns However, the. .. this purpose, the first two lines of code include the header files for the Qt classes that we want to use in the following code In our case these header files contain the interface descriptions for the classes QApplication and QLabel In Qt 4 there is precisely one header file for each Qt class, which is named without the otherwise standard filename extension h: Its name corresponds exactly to the class name.1... 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 Desktop as versions containing all features The open source version in each case corresponds to the desktop edition, so it is not restricted in any way in terms of size 2 The. .. the KDE project, which was developing an entire desktop based on Qt Today, together with GNOME, KDE is one of the most important 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. .. explained in Appendix B Structure of the Book The book first explains the basic structure of the Qt toolkit, together with its most important specific properties The subsequent chapters concentrate on writing your own small applications All other technologies, presented in the final chapters, are demonstrated as short, independent examples for the sake of clarity But they are arranged in such a way that . MOLKENTIN THE BOOK of THE BOOK of Qt 4 THE BOOK of THE BOOK of Qt 4 ® MOLKENTIN www.it-ebooks.info www.it-ebooks.info The Book of Qt 4 www.it-ebooks.info www.it-ebooks.info Daniel Molkentin TheBook of. 4 www.it-ebooks.info www.it-ebooks.info Daniel Molkentin TheBook of Qt 4 TheArt of BuildingQtApplications ® Munich SanFrancisco www.it-ebooks.info The Book of Qt 4: The ArtofBui ldingQtApplications. Copyright c  2007OpenSource. (QStack) 40 3 B.3.2Queues(QQueue) 40 4 B.4AssociativeArrays 40 4 B .4. 1Dictionaries (QMap) 40 4 12 www.it-ebooks.info Contents B .4. 2AllowingSeveral Identical Keys(QMultiMap) 40 7 B .4. 3HashTableswithQHash 40 9 B .4. 4Hash-basedAmountswithQSet.

Ngày đăng: 24/04/2014, 16:09

Mục lục

  • The Book of Qt 4

    • Contents

    • Introduction

    • Preparations

    • Chapter 1: Basics, Tools, and First Code

    • Chapter 2: The Tools Needed to Create Dialogs

    • Chapter 3: GUI Design Using the Qt Designer

    • Chapter 4: Developing a GUI Application Based on a Main Window

    • Chapter 5: Laying Out Widgets

    • Chapter 6: Dialogs

    • Chapter 7: Events, Drag and Drop, and the Clipboard

    • Chapter 8: Displaying Data Using "Interview"

    • Chapter 9: The QtSql Module

    • Chapter 10: The Graphics Library "Arthur"

    • Chapter 11: Input/Output Interfaces

    • Chapter 12: Threading with QThread

    • Chapter 13: Handling XML with QtXml

    • Chapter 14: Internationalization

    • Appendixes

    • A: Debugging Help

    • B: Tulip: Containers and Algorithms

Tài liệu cùng người dùng

Tài liệu liên quan