Statistics
2
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-04-21

AuthorMatthias Kalle Dalheimer [Dalheimer, Matthias Kalle]

Take full advantage of Qt, the powerful, easy-to-use, cross-platform GUI toolkit. Completely updated for Qt Version 3.0, Programming with Qt guides you through the steps of writing your first Qt application. It's also a reference to the what, how, and why of every GUI element in Qt. And it covers advanced topics like 2D transformations, drag-and-drop, and custom image file filters.

Tags
No tags
Publisher: O'Reilly Media
Publish Year: 2010
Language: 英文
File Format: PDF
File Size: 3.4 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Programming with Qt, 2nd Edition Table of Contents SPECIAL OFFER: Upgrade this ebook with O’Reilly A Note Regarding Supplemental Files Preface A Productive Weekend What You Should Know Organization of This Book Conventions Used in This Book Comments and Questions Acknowledgments 1. Introduction Why GUI Toolkits? Why Portability? Why Qt? Implementing Cross-Platform GUI Libraries API Layering API Emulation GUI Emulation Acquiring Qt Qt Editions How Free Is Qt? Compiling and Installing Qt Installing Qt or Qt/Embedded on Unix or MacOS X systems Compiling and Installing Qt on Windows C++ as Used by Qt Getting Help 2. First Steps in Qt Programming Hello, world! Exercises Using the Qt Reference Documentation Exercises Adding an Exit Button Exercises
Introduction to Signals and Slots The Problem of Callback Functions A New Approach Signals and Slots in Qt Another Example of Signals and Slots Running moc Exercises Event Handling and Simple Drawings with QPainter Exercises 3. Learning More About Qt Adding Menus Exercises Adding a Scrolled View Exercises Adding a Context Menu Exercise File I/O Exercises 4. A Guided Tour Through the Simple Widgets General Widget Parameters Widget Styles Buttons Push Buttons Radio Buttons and Checkboxes Selection Widgets List Boxes Combo Boxes Widgets for Bounded-Range Input Sliders Dials Spin Boxes Widgets for Entering Date and Time Scrollbars Menu-Related Widgets Arrangers Frames Group Boxes
Splitters Widget Stacks Size Grips Tab-Related Widgets Text-Entry Fields Labels Simple Labels QLCDNumber Widgets for the Office The Main Window Toolbars Status Bars Multiple Document Interface (MDI) Docking Windows Tooltips and “What’s This” Windows Progress Bars Scrolled Views List Views Icon Views Widgets for Tabular Material Widgets for Displaying Rich Text 5. A Guided Tour Through the Qt Dialog Boxes Predefined Dialog Boxes File Selection Dialog Boxes Color Dialog Boxes Font Dialog Boxes Message Boxes Input Dialog Boxes Progress Dialog Boxes Error Message Dialog Boxes Building Blocks for Your Own Dialog Boxes Tab Dialog Boxes Wizards 6. Using Layout Managers Layout Manager Basics Laying Out Widgets in Rows and Columns Nested Layout Managers
Grid Layout Implicit Geometry Management 7. Some Thoughts on GUI Design 8. Container Classes Available Container Classes Choosing a Container Class Working with Reference-Based Container Classes Basic Usage Caching Data Iterators Stacks and Queues Working with Value-Based Container Classes 9. Graphics Animations Printing Managing Colors Color Allocation Color Models Color Groups and Palettes Basic QPainter: Drawing Figures Selecting Brushes, Colors, and Pens Drawing Operations in QPainter Advanced QPainter Two-Dimensional Transformations View Transformations Clipping Double-Buffering and Other Nifty Techniques Independently Movable Objects with QCanvas Adding Different Items to the Canvas Working with Styles Loading and Saving Custom Image Formats Setting a Cursor 10. Text Processing Internationalization and Localization of On-Screen Text Internationalizing a Qt Application Localizing Your Application Validating User Input
Working with Regular Expressions Reading and Writing XML Files Processing XML with SAX Processing XML with DOM Rich Text 11. Working with Files and Directories Reading a Text File Traversing a Directory File Information Reading and Writing Configuration Data 12. Interapplication Communication Using the Clipboard Drag-and-Drop 13. Interfacing with the Operating System Working with Date and Time Values Loading Code Libraries Dynamically Spawning Child Processes Playing Sounds 14. Writing Your Own Widgets Implementing a Coordinate Selector Implementing a Browse Box 15. Focus Handling 16. Advanced Event Handling Event Filters Sending Synthetic Events 17. Advanced Signals and Slots Signals and Slots Revisited Connecting Several Buttons to One Slot Actions 18. Providing Help 19. Accessing Databases Installation of the SQL Module Connecting to a Database Simple Data Retrieval Data Retrieval with Cursors Data Display Data Manipulation
Anything Else? Metadata Transactions More Features 20. Multithreading Configuring Qt for Multithreading Using Qt’s Multithreading Classes Multithreading Pitfalls Alternatives to Multithreading 21. Debugging 22. Portability Why Portability Is Desirable How to Write Portable Programs Danger Ahead: When Even Qt Is Not Portable Building Projects Portably with qmake 23. Qt Network Programming Low-Level Socket Access Higher-Level Network Access 24. Interfacing Qt with Other Languages and Libraries OpenGL Programming with Qt Writing Netscape Plug-ins Integrating Xt Widgets Interfacing Qt with Perl 25. Using the Visual C++ IDE for Qt Programs Importing an Existing Makefile Creating Your Own Project from Scratch Using qmake to Create a Project File Using the MS Visual Studio Integration 26. Visual Design with Qt Designer Why Do You Need A GUI Designer? Creating a Simple Application with the Help of Qt Designer Becoming Familiar with Qt Designer More Widgets for Our Form Putting It All Together Adding Functionality to a Dialog Box by Subclassing Continuing Work on an Already Existing Dialog Box Adding Functionality to the Push Buttons
Initializing the State of Widgets in the Form Connecting Widgets to One Another Using Layout Management Putting a Form Together with Layouts Popular Mechanics: Working with Spacers Useful Techniques Working with Signals and Slots Changing the Tab Order All About Layout Management Integrating Qt Designer Files into Your Project Using qmake for Generating and Building Qt Designer Files Using Project Management Creating Main Windows Accessing Databases from Qt Designer Accessing the Help System Dynamically Creating Forms at Runtime Custom Widgets A. Answers to Exercises Answers to Exercises in Chapter 2 Answers to Exercises in the Section “Hello, world!” Answers to Exercises in the Section “Using the Qt Reference Documentation” Answers to Exercises in the Section “Adding an Exit Button” Answers to Exercises in the Section “Introduction to Signals and Slots” Answers to Exercises in the Section “Event Handling and Simple Drawings with QPainter” Answers to Exercises in Chapter 3 Answers to Exercises in the Section “Adding Menus” Answers to Exercises in the Section “Adding a Scrolled View” Answers to Exercises in the Section “Adding a Context Menu” Answers to Exercises in the Section “File I/O” Index SPECIAL OFFER: Upgrade this ebook with O’Reilly
Programming with Qt, 2nd Edition Matthias Kalle Dalheimer Editor Ariane Hesse Copyright © 2010 O'Reilly Media, Inc.
(This page has no text content)
SPECIAL OFFER: Upgrade this ebook with O’Reilly Click here for more information on this offer!
A Note Regarding Supplemental Files Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596000646/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices. All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, we’ve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to booktech@oreilly.com.
Preface
A Productive Weekend A few years ago, someone emailed me on a Friday. He wanted to start a small project, but did not really have the resources for it. He outlined the requirements and asked how long it would take me. The next Monday, he phoned to ask if I thought the project was feasible and how much time I would need. I said that I already had a working prototype. I had hacked it together in a few hours the day before—and because I had used Qt, I had still found enough time to play outside with my son. Needless to say, I got the contract. Does this sound too good to be true? Well, I admit, I lied a bit. It was raining cats and dogs that Sunday and we had to play inside. But the rest is true. The Qt class library makes it easy to write applications that are visually attractive, fast, and ready to run on Windows, Unix, MacOS X, and (Linux- based) embedded systems in a very short time. Had I used Motif, gtk, or another toolkit for this task, I might not have managed it in such a short time. If I had to port my program to Windows, the number of choices would have been even smaller, and for embedded systems there are hardly any alternatives. Until the first edition of this book, there was not much Qt documentation for beginners. The package itself includes a tutorial, but leaves out a lot. There are other example programs, but often you have to search through several files to find what you are looking for and sometimes you don’t find it at all. Some people think it’s hard to get started with Qt, but after they have overcome the initial hurdles, they see that Qt helps them in a lot of ways when they write their applications. That’s why I decided to write this book. It will teach you how to program with Qt from the start and it will help you make more sense of the documentation that comes with Qt.
What You Should Know Qt,[1] written and distributed by the Norwegian company Trolltech, is a C++ class library and this book requires some C++ knowledge. However, you don’t have to be a C++ expert to start programming with Qt. We have included a short overview of C++ language features that are used in Qt programs. Qt is mostly about GUI programming (although it can also help you write a web server and other non-GUI applications). Accordingly, it helps to know a bit about how GUI programs are written. If you already know what an event loop is and how GUI programs are usually structured, this knowledge will make things easier for you at the beginning. I have tried to keep this book as self-contained as possible, though, so if you have never written a GUI program before, now would be a good time to start. Qt makes GUI programming much easier—even easier than with most (if not all) other toolkits. I assume that you know how to use a text editor to enter program source code and that you know how to start your compiler and linker to generate an executable program. Unless there is something Qt-specific about these operations, I won’t talk about these topics. All programs in this book were tested with Linux 2.4.7 and the G++ compiler, and Windows 2000 and the Microsoft Visual C++ compiler. If you use another version of Unix or Windows 95/98/NT, you should have no problems. If you use OS/2,[2] however, you are out of luck. Qt has not been ported to this platform (yet). At the time of this writing, a version for MacOS X is in the works and we have tested the examples against a beta version. The Macintosh version is now also publically available. [1] Qt can either be pronounced cue-tee or cute. Both ways are considered correct. [2] You should be able to run Qt programs with XFree86 for OS/2, but I have never tried this.
(This page has no text content)
Organization of This Book This book is organized to make finding the information you need as easy as possible. If you have not programmed with Qt before, you should read the first three chapters of this book to grasp the basic concepts. The following chapters and sections are relatively independent and you should be able to jump around and read whatever you are interested in. If your application contains any custom graphics, you will probably want to read Chapter 9. Reading Chapter 4 and Chapter 5 is a very good idea because you can make better use of Qt’s features if you know what is available. Before reading the chapter on Qt Designer, it is a good idea to have at least read these two chapters so that you know about the widgets that the Designer offers you to use, as well as how dialogs are built up in general. To help you set up your own tour through the book, the following list of chapters includes short descriptions of their contents. Of course, if you decide to read through the whole book, I would be more than glad! Chapter 1 , Introduction Introduces you to basic Qt concepts. It gives you some information about why people use GUI toolkits in the first place and explains what is special about Qt. You will learn how and where you can get Qt, whether or not you have to pay for it, and where to go if you are stuck with a Qt-related problem. Chapter 2 , First Steps in Qt Programming Is a Qt programming primer. We start with a simple “Hello world” example, learn about signals and slots, and finish the chapter by creating a small painting program that lets you scribble on a virtual canvas in various colors. Chapter 3 , Learning More About Qt Continues the Qt primer. We develop the painting application into something more useful. By the end of this chapter, you will know how to use files and understand programming menus and other features of Qt. Chapter 4 , A Guided Tour Through the Simple Widgets Is a presentation of all simple widgets available in Qt. This chapter helps you pick the right widget for any given task. If an appropriate widget is not listed, you’ll know that you have to develop your own.
You will also find explanations of the most useful methods, code examples, and hints for using existing widgets. Chapter 5 , A Guided Tour Through the Qt Dialog Boxes Continues the tour through the available GUI elements in Qt by showing which dialogs are available. It also shows you how to program your own dialogs and tab dialogs. Chapter 6 , Using Layout Managers Explains how widgets are laid out automatically within their parent window so that their positions are computed at runtime according to their needs. This topic is complex, so I provide several examples to make it easier to understand. Chapter 7 , Some Thoughts on GUI Design Provides you with some hints about writing applications that users will like. This chapter includes guidelines about when to use which widget. Chapter 8 , Container Classes Explains how to use classes for building up lists, arrays, and dictionaries and explains how to traverse all elements in a container. Chapter 9 , Graphics Shows how to create custom graphics, including animations, printing, colors, two-dimensional transformations, and canvases. Chapter 10 , Text Processing Has sections on internationalization, working with regular expressions, reading and writing XML documents, checking input into text-entry fields, and working with rich text, a markup system for text with different attributes (not the same as Microsoft’s rich text format). The classes explained here can often reduce the length of your code and ease your programming efforts. Chapter 11 , Working with Files and Directories Explains how to access files and directories and maintain portability using the classes that Qt provides. These classes can shield your code from native APIs. Chapter 12 , Interapplication Communication Tells you how Qt applications can exchange data with one another, or with other applications. It describes the clipboard and drag-and-drop. Chapter 13 , Interfacing with the Operating System Provides information about interfacing with the operating system, including how to work with date and time values, how to load libraries
dynamically, and how to start child processes from Qt programs. Chapter 14 , Writing your Own Widgets Explains how to write your own widgets. Chapter 15 , Focus Handling Explains how to control the way Qt passes the keyboard focus to widgets. This information is not crucial for getting an application up and running, but it is important if you don’t want your users to hate you. Chapter 16 , Advanced Event Handling Contains information about advanced event handling, such as filtering the events that go to a widget and sending synthetic events. Chapter 17 , Advanced Signals and Slots Builds on the tutorial’s introduction to signals and slots and describes advanced usage of signals and slots. We’ll also talk about actions here that abstract from user interaction patterns. Chapter 18 , Providing Help Explains various options for providing help to users of a Qt application. Chapter 19 , Accessing Databases Shows you how to interact with relational databases from Qt programs, including retrieving, updating, and displaying the database data. Chapter 20 , Multithreading Explains what you have to observe when you want to write multithreaded programs with Qt, as well as the classes that Qt provides for creating multiple threads and synchronizing them. Chapter 21 , Debugging Offers hints about Qt that can make debugging easier. Chapter 22 , Portability Explains how you can best achieve portability for your Qt programs. Chapter 23 , Qt Network Programming Gives examples of using Qt for non-GUI tasks by presenting a little HTTP server program and an FTP downloader created with the help of the network extension in Qt. Chapter 24 , Interfacing Qt with Other Languages and Libraries Explains how to interface Qt with other languages and libraries. Its sections include using Qt with Perl, integrating Qt and Xt widgets, using OpenGL in Qt programs, and writing Netscape plug-ins with Qt. Chapter 25 , Using the Visual C++ IDE for Qt Programs
Provides hints about how to use the Visual C++ IDE (Visual Studio) from Microsoft for writing Qt programs. Appendix A , Answers to Exercises Provides complete, commented answers to all exercises in the tutorial.