C++ GUI Programming with Qt 3 ppt

464 501 1
C++ GUI Programming with Qt 3 ppt

Đ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

C ++ GUI Programming with Qt 3 B RUCE P ERENS ’ O PEN S OURCE S ERIES ◆ C++ GUI Programming with Qt 3 Jasmin Blanchette, Mark Summerfield ◆ Managing Linux Systems with Webmin: System Administration and Module Development Jamie Cameron ◆ Understanding the Linux Virtual Memory Manager Mel Gorman ◆ Implementing CIFS: The Common Internet File System Christopher R. Hertel ◆ Embedded Software Development with eCos Anthony J. Massa ◆ Rapid Application Development with Mozilla Nigel McFarlane ◆ The Linux Development Platform: Configuring, Using, and Maintaining a Complete Programming Environment Rafeeq Ur Rehman, Christopher Paul ◆ Intrusion Detection Systems with Snort: Advanced IDS Techniques with Snort, Apache, MySQL, PHP, and ACID Rafeeq Ur Rehman ◆ The Official Samba-3 HOWTO and Reference Guide John H. Terpstra, Jelmer R. Vernooij, Editors C ++ GUI Programming with Qt 3 Jasmin Blanchette Mark Summerfield Prentice Hall in association with Trolltech Press Library of Congress Cataloging-in-Publication Data A CIP catalog record for this book can be obtained from the Library of Congress Editorial/Production Supervision: Kathleen M. Caren Cover Design Director: Jerry Votta Art Director: Gail Cocker-Bogusz Manufacturing Buyer: Maura Zaldivar Acquisitions Editor: Jill Harry Editorial Assistant: Brenda Mulligan Marketing Manager: Dan Depasquale Copyright  2004 Trolltech AS Published by Pearson Education, Inc. Publishing as Prentice Hall Professional Technical Reference Upper Saddle River, New Jersey 07458 This material may only be distributed subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is available at http://www.open- content.org/openpub/ ). Prentice Hall PTR offers excellent discounts on this book when ordered in quanti- ty for bulk purchases or special sales. For more information, please contact: U.S. Corporate and Government Sales, 1-800-382-3419, corpsales@pearsontechgroup. com. For sales outside of the U.S., please contact: International Sales, 1-317-581- 3793, international@pearsontechgroup.com. Trolltech  ,Qt  , and the Trolltech logo are registered trademarks of Trolltech. OpenGL  is a trademark of Silicon Graphics, Inc. in the United States and other countries. All other company and product names mentioned herein are the trademarks or registered trademarks of their respective owners. The authors,copyright holder, and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. The information in this book is furnished for informational use only, is subject to change without notice, and does not represent a commitment on the part of the copyright holder or the publisher. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The software described in this book is furnished under a license agreement or non-disclosure agreement. The software may be used or copied only in accordance with the terms of the agreement. Printed in the United States of America First Printing ISBN 0-13-124072-2 Pearson Education Ltd. Pearson Education Australia Pty., Limited Pearson Education Singapore, Pte. Ltd. Pearson Education North Asia Ltd. Pearson Education Canada, Ltd. Pearson Educación de Mexico, S.A. de C.V. Pearson Education-Japan Pearson Education Malaysia, Pte. Ltd. Contents Foreword ix Preface xi Acknowledgments xiii A Brief History of Qt xv Part I: Basic Qt 1 . Getting Started 3 Hello Qt 3 Making Connections 5 Using the Reference Documentation 8 2 . Creating Dialogs 11 Subclassing QDialog 11 Signals and Slots in Depth 18 Rapid Dialog Design 21 Shape-Changing Dialogs 28 Dynamic Dialogs 33 Built-in Widget and Dialog Classes 33 3 . Creating Main Windows 39 Subclassing QMainWindow 40 Creating Menus and Toolbars 44 Implementing the File Menu 49 Setting Up the Status Bar 56 Using Dialogs 58 Storing Settings 63 Multiple Documents 64 Splash Screens 67 v 4 . Implementing Application Functionality 69 The Central Widget 69 Subclassing QTable 70 Loading and Saving 77 Implementing the Edit Menu 80 Implementing the Other Menus 84 Subclassing QTableItem 88 5 . Creating Custom Widgets 97 Customizing Qt Widgets 97 Subclassing QWidget 99 Integrating Custom Widgets with Qt Designer 108 Double Buffering 112 Part II: Intermediate Qt 6 . Layout Management 135 Basic Layouts 135 Splitters 140 Widget Stacks 144 Scroll Views 145 Dock Windows 150 Multiple Document Interface 152 7 . Event Processing 163 Reimplementing Event Handlers 163 Installing Event Filters 168 Staying Responsive During Intensive Processing 171 8 . 2D and 3D Graphics 175 Painting with QPainter 175 Graphics with QCanvas 185 Printing 198 Graphics with OpenGL 209 9 . Drag and Drop 215 Enabling Drag and Drop 215 Supporting Custom Drag Types 220 Advanced Clipboard Handling 224 vi 10 . Input/Output 227 Reading and Writing Binary Data 227 Reading and Writing Text 234 Handling Files and Directories 237 Inter-Process Communication 239 11 . Container Classes 243 Vectors 243 Lists 247 Maps 249 Pointer-Based Containers 251 QString and QVariant 254 12 . Databases 261 Connecting and Querying 261 Presenting Data in Tabular Form 266 Creating Data-Aware Forms 275 13 . Networking 283 Using QFtp 283 Using QHttp 289 TCP Networking with QSocket 291 UDP Networking with QSocketDevice 301 14 . XML 307 Reading XML with SAX 307 Reading XML with DOM 312 Writing XML 316 15 . Internationalization 319 Working with Unicode 319 Making Applications Translation-Aware 323 Dynamic Language Switching 329 Translating Applications 334 16 . Providing Online Help 339 Tooltips, Status Tips, and “What’s This?” Help 339 Using QTextBrowser as a Simple Help Engine 342 Using Qt Assistant for Powerful Online Help 346 vii 17 . Multithreading 349 Working with Threads 349 Communicating with the GUI Thread 359 Using Qt’s Classes in Non-GUI Threads 363 18 . Platform-Specific Features 367 Interfacing with Native APIs 367 Using ActiveX 371 Session Management 384 Appendices A . Installing Qt 393 A Note on Licensing 393 Installing Qt/Windows 394 Installing Qt/Mac 395 Installing Qt/X11 397 B . Qt’s Class Hierarchy 399 Index 403 viii Foreword Why Qt? Why do programmers like us choose Qt? Sure, there are the obvious answers: Qt’s single-source compatibility, its feature richness, its C ++ perfor- mance, the availability of the source code, its documentation, the high-quality technical support, and all the other items mentioned in Trolltech’s glossy mar- keting materials. This is all very well, but it misses the most important point: Qt is successful because programmers like it. How come programmers like one technology, but dislike another? Personally, I believe software engineers enjoy technology that feels right, but dislike ev- erything that doesn’t. How else can we explain that some of the brightest pro- grammers need help to program a VCR, or that most engineers seem to have trouble operating the company’s phone system? I for one am perfectly capa- ble of memorizing sequences of random numbers and commands, but if these are required to control my answering machine, I’d prefer not to have one. At Trolltech, our phone system forces us to hold the ‘ ∗ ’ key pressed down for two seconds before we are allowed to type in the other person’s extension number. If you forget to do this but start typing the extension immediately, you have to dial the entire number again. Why ‘ ∗ ’? Why not ‘#’, or ‘1’, or ‘5’, or any of the other twenty keys on the phone? Why two seconds and not one, or three, or one and a half? Why anything at all? I find the phone so irritating that I avoid using it whenever I can. Nobody likes having to do random things, espe- cially when those random things apparently depend on some equally random context you wish you didn’t have to know about in the first place. Programming can be a lot like using our phone system, only worse. And this is where Qt comes to the rescue. Qt is different. For one thing, Qt makes sense. And for another, Qt is fun. Qt lets you concentrate on your tasks. When Qt’s original architects faced a problem, they didn’t just look for a good solution, or a quick solution, or the simplest solution. They looked for the right solution, and then they documented it. Granted they made mistakes,and granted some of their design decisions didn’t pass the test of time, but they still got a lot of things right, and what wasn’t right could and can be corrected. You can see this by the fact that a system originally designed to bridge Windows 95 and Unix/Motif now unifies modern desktop systems as diverse as Windows XP, Mac OS X, and GNU/Linux with KDE. Long before Qt became so popular and so widely used, the dedication of Qt’s developers to finding the right solutions made Qt special. That dedication is just as strong today and affects everyone who maintains and develops Qt. For us, working on Qt is a responsibility and a privilege. We are proud of helping to make your professional and open source lives easier and more enjoyable. ix One of the things that makes Qt a pleasure to use is its online documentation. But the documentation’s focus is primarily on individual classes, with little said about how to build sophisticated real-world applications. This excellent book fills that gap. It shows you what Qt has to offer, how to program Qt the “Qt way”, and how to get the best from Qt. The book will teach a C ++ programmer how to program Qt, and provides enough advanced material to satisfy experienced Qt programmers. The book is packed with good examples, advice, and explanations, and will be the text that we use to induct all new programmers who join Trolltech. Nowadays, there are a vast number of commercial and free Qt applications available for purchase or download. Some are specialized for particular vertical markets, while others are aimed at the mass-market. Seeing so many applications built with Qt fills us with pride and inspires us to make Qt even better. And with the help of this book, there will be more and higher quality Qt applications than ever before. Matthias Ettrich Oslo, Norway November 2003 x [...]... topLeftLayout->addWidget(lineEdit); 029 030 031 032 QVBoxLayout *leftLayout = new QVBoxLayout; leftLayout->addLayout(topLeftLayout); leftLayout->addWidget(caseCheckBox); leftLayout->addWidget(backwardCheckBox); 033 034 035 036 QVBoxLayout *rightLayout = new QVBoxLayout; rightLayout->addWidget(findButton); rightLayout->addWidget(closeButton); rightLayout->addStretch(1); 037 038 QHBoxLayout *mainLayout = new... explaining good idiomatic Qt 3 programming techniques rather than simply rehashing or summarizing Qt s extensive online documentation And because we are involved in the development of Qt 4, we have tried to ensure that most of what we teach here will still be valid and sensible for Qt 4 It is assumed that you have a basic knowledge of C++ The code examples use a subset of C++, avoiding many C++ features that... 1.2 was released in April 1997 Matthias Ettrich’s decision to use Qt to build KDE helped Qt become the de-facto standard for C++ GUI development on Linux Qt 1 .3 was released in September 1997 Matthias joined Trolltech in 1998, and the last major Qt 1 release, 1.40, was made in September of that year Qt 2.0 was released in June 1999 Qt 2 had many major architectural changes and was a much stronger and... setValue(int) slot of the other widget is called with the new value Line 20 sets the spin box value to 35 When this happens, the QSpinBox emits the valueChanged(int) signal with an int argument of 35 This argument is # If you get a compiler error on the QHBox constructor, it means that you are using an older version of Qt Make sure that you are using Qt 3. 2.0 or a later Qt 3 release 8 1 Getting Started passed... of this book is to teach you how to write GUI programs using Qt 3 The book starts with “Hello Qt and quickly moves on to more advanced topics, such as creating custom widgets and providing drag and drop The text is complemented by a CD that contains the source code of the example programs The CD also provides Qt and Borland C++ for Windows, Qt for Unix, and Qt for Mac OS X Appendix A explains how to... www.trolltech.com/products/3rdparty/ for information on Qt add-ons Qt also has a well-established and thriving user community that uses the qt- interest mailing list; see http://lists.trolltech.com/ for details The book is divided into two parts Part I covers all the concepts and practices necessary for programming GUI applications using Qt Knowledge of this part alone is sufficient to write useful GUI applications... value to 35 The slider then emits the valueChanged(int) signal, because its own value changed, triggering the spin box’s setValue(int) slot But at this point, setValue(int) doesn’t emit any signal, since the spin box value is already 35 This prevents infinite recursion Figure 1.6 summarizes the situation 1 00 ¤ S SSSSSSSSSSSSS setValue (35 ) 2 35 ¤ S SSSSSSSSSSSSS valueChanged (35 ) setValue (35 ) 3 35 ¤ SSSS... you can use a non-commercial Qt edition (The editions of Qt on the CD are non-commercial.) Qt is the foundation on which the K Desktop Environment (KDE) and the many open source applications that go with it are built xi In addition to Qt s hundreds of classes, there are add-ons that extend Qt s scope and power Some of these products, like the Qt/ Motif integration module and Qt Script for Applications... multiplatform GUI toolkit they would soon go on to build In 1991, Haavard started writing the classes that eventually became Qt, collaborating with Eirik on the design The following year, Eirik came up the idea for “signals and slots”, a simple but powerful GUI programming paradigm Haavard took the idea and produced a hand-coded implementation By 19 93, Haavard and Eirik had developed Qt s first graphics...Preface The Qt toolkit is a C++ class library and a set of tools for building multiplatform GUI programs using a “write once, compile anywhere” approach Qt lets programmers use a single source tree for applications that will run on Windows 95 to XP, Mac OS X, Linux, Solaris, HP-UX, and many other versions of Unix with X11 A version of Qt is also available for Embedded Linux, with the same API . 38 4 Appendices A . Installing Qt 39 3 A Note on Licensing 39 3 Installing Qt/ Windows 39 4 Installing Qt/ Mac 39 5 Installing Qt/ X11 39 7 B . Qt s Class Hierarchy 39 9 Index 4 03 viii Foreword Why. C ++ GUI Programming with Qt 3 B RUCE P ERENS ’ O PEN S OURCE S ERIES ◆ C++ GUI Programming with Qt 3 Jasmin Blanchette, Mark Summerfield ◆ Managing

Ngày đăng: 23/03/2014, 22:20

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan