100% found this document useful (5 votes)
38 views

KDE 2 Qt Programming Bible 1st Edition by Arthur Griffith ISBNinstant download

The document provides links to various programming books available for instant download, including titles such as 'KDE 2 Qt Programming Bible' and 'JavaScript Bible'. It includes information about the publishers, ISBNs, and formats available for each book. Additionally, it outlines the mission and background of IDG Books Worldwide, the publisher of these titles.

Uploaded by

bahebkjedir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
38 views

KDE 2 Qt Programming Bible 1st Edition by Arthur Griffith ISBNinstant download

The document provides links to various programming books available for instant download, including titles such as 'KDE 2 Qt Programming Bible' and 'JavaScript Bible'. It includes information about the publishers, ISBNs, and formats available for each book. Additionally, it outlines the mission and background of IDG Books Worldwide, the publisher of these titles.

Uploaded by

bahebkjedir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

KDE 2 Qt Programming Bible 1st Edition by Arthur

Griffith ISBN download

https://ebookball.com/product/kde-2-qt-programming-bible-1st-
edition-by-arthur-griffith-isbn-13544/

Instantly Access and Download Textbook at https://ebookball.com


Get Your Digital Files Instantly: PDF, ePub, MOBI and More
Quick Digital Downloads: PDF, ePub, MOBI and Other Formats

Spss for Dummies 1st Edition by Arthur Griffith 0470113448


978-0470113448

https://ebookball.com/product/spss-for-dummies-1st-edition-by-
arthur-griffith-0470113448-978-0470113448-14694/

IDG Books WorldWide 1st Edition by DirectX 3D Graphics Programming


Bible ISBN

https://ebookball.com/product/idg-books-worldwide-1st-edition-by-
directx-3d-graphics-programming-bible-isbn-11082/

C++ GUI Programming With Qt 4 1st edition by Jasmin Blanchette, Mark


Summerfield ISBN 0131872494 9780131872493

https://ebookball.com/product/c-gui-programming-with-qt-4-1st-
edition-by-jasmin-blanchette-mark-summerfield-
isbn-0131872494-9780131872493-12526/

Google Power Tools Bible 1st Edition by Ted CoombsandRoderico DeLeon


ISBN

https://ebookball.com/product/google-power-tools-bible-1st-
edition-by-ted-coombsandroderico-deleon-isbn-12306/
Hakin9 Bible 1st edition by Vikram Singh ASIN B08VNWH9D5

https://ebookball.com/product/hakin9-bible-1st-edition-by-vikram-
singh-asin-b08vnwh9d5-16838/

JavaScript Bible 5th Edition by Danny Goodman, Michael Morrison ISBN


9780764557439

https://ebookball.com/product/javascript-bible-5th-edition-by-
danny-goodman-michael-morrison-isbn-9780764557439-11424/

3Ds Max 4 Bible 1st Edition by Kelly L Murdock ISBN 0764535846


9780764535840

https://ebookball.com/product/3ds-max-4-bible-1st-edition-by-
kelly-l-murdock-isbn-0764535846-9780764535840-14636/

Introduction to Design Patterns in C with Qt 2nd Edition by Alan


Ezust, Paul Ezust ISBN 0132851636 9780132851633

https://ebookball.com/product/introduction-to-design-patterns-in-
c-with-qt-2nd-edition-by-alan-ezust-paul-ezust-
isbn-0132851636-9780132851633-14144/

An Introduction to Design Patterns in C with Qt 4 1st Edition by Alan


Ezust, Paul Ezust ISBN 0131879057 9780131879058

https://ebookball.com/product/an-introduction-to-design-patterns-
in-c-with-qt-4-1st-edition-by-alan-ezust-paul-ezust-
isbn-0131879057-9780131879058-12664/
4682-1 fm.f.qc 11/20/00 15:45 Page iii

KDE 2/Qt
Programming Bible
Arthur Griffith

IDG Books Worldwide, Inc.


An International Data Group Company
Foster City, CA ✦ Chicago, IL ✦ Indianapolis, IN ✦ New York, NY
4682-1 fm.f.qc 11/20/00 15:45 Page iv

KDE 2/Qt Programming Bible Service department at 800-762-2974. For reseller


Published by information, including discounts and premium sales,
IDG Books Worldwide, Inc. please call our Reseller Customer Service
An International Data Group Company department at 800-434-3422.
919 E. Hillsdale Blvd., Suite 400 For information on where to purchase IDG Books
Foster City, CA 94404 Worldwide’s books outside the U.S., please contact
www.idgbooks.com (IDG Books Worldwide our International Sales department at 317-572-3993
Web site) or fax 317-572-4002.
Copyright © 2001 IDG Books Worldwide, Inc. All rights For consumer information on foreign language
reserved. No part of this book, including interior translations, please contact our Customer Service
design, cover design, and icons, may be reproduced department at 800-434-3422, fax 317-572-4002, or
or transmitted in any form, by any means (electronic, e-mail [email protected].
photocopying, recording, or otherwise) without the For information on licensing foreign or domestic
prior written permission of the publisher. rights, please phone +1-650-653-7098.
ISBN: 0-7645-4682-1 For sales inquiries and special prices for bulk
Printed in the United States of America quantities, please contact our Order Services
10 9 8 7 6 5 4 3 2 1 department at 800-434-3422 or write to the address
above.
1B/RV/RS/QQ/FC
For information on using IDG Books Worldwide’s
Distributed in the United States by IDG Books
books in the classroom or for ordering examination
Worldwide, Inc.
copies, please contact our Educational Sales
Distributed by CDG Books Canada Inc. for Canada; department at 800-434-2086 or fax 317-572-4005.
by Transworld Publishers Limited in the United
For press review copies, author interviews, or other
Kingdom; by IDG Norge Books for Norway; by IDG
publicity information, please contact our Public
Sweden Books for Sweden; by IDG Books Australia
Relations department at 650-653-7000 or fax
Publishing Corporation Pty. Ltd. for Australia and
650-653-7500.
New Zealand; by TransQuest Publishers Pte Ltd. for
Singapore, Malaysia, Thailand, Indonesia, and Hong For authorization to photocopy items for corporate,
Kong; by Gotop Information Inc. for Taiwan; by ICG personal, or educational use, please contact
Muse, Inc. for Japan; by Intersoft for South Africa; Copyright Clearance Center, 222 Rosewood Drive,
by Eyrolles for France; by International Thomson Danvers, MA 01923, or fax 978-750-4470.
Publishing for Germany, Austria, and Switzerland;
by Distribuidora Cuspide for Argentina; by LR Library of Congress Cataloging-in-Publication Data
International for Brazil; by Galileo Libros for Chile; by Griffith, Arthur.
Ediciones ZETA S.C.R. Ltda. for Peru; by WS Computer KDE 2/Qt programming bible / Arthur Griffith.
Publishing Corporation, Inc., for the Philippines; p. cm.
by Contemporanea de Ediciones for Venezuela; by ISBN 0-7645-4682-1 (alk. paper)
Express Computer Distributors for the Caribbean 1. C++ (Computer program language)
and West Indies; by Micronesia Media Distributor, Inc. 2. Graphical user interfaces (Computer systems)
for Micronesia; by Chips Computadoras S.A. de C.V. 3. Linux I. Title.
for Mexico; by Editorial Norma de Panama S.A. for QA76.73.C153.G7426 2001
Panama; by American Bookshops for Finland. 005.13’3--dc21 00-047247
For general information on IDG Books Worldwide’s
books in the U.S., please call our Consumer Customer
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR
BEST EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS
OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS
BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS
FOR A PARTICULAR PURPOSE. THERE ARE NO WARRANTIES WHICH EXTEND BEYOND THE
DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY
SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF
THE INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT GUARANTEED OR
WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES CONTAINED
HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR
SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT
NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.
Trademarks: All brand names and product names used in this book are trade names, service marks,
trademarks, or registered trademarks of their respective owners. IDG Books Worldwide is not associated
with any product or vendor mentioned in this book.

is a registered trademark or trademark under exclusive license


to IDG Books Worldwide, Inc., from International Data Group, Inc.,
in the United States and/or other countries.
4682-1 fm.f.qc 11/20/00 15:45 Page v

Welcome to the world of IDG Books Worldwide.


IDG Books Worldwide, Inc., is a subsidiary of International Data Group, the world’s largest publisher of
computer-related information and the leading global provider of information services on information technology.
IDG was founded more than 30 years ago by Patrick J. McGovern and now employs more than 9,000 people
worldwide. IDG publishes more than 290 computer publications in over 75 countries. More than 90 million
people read one or more IDG publications each month.
Launched in 1990, IDG Books Worldwide is today the #1 publisher of best-selling computer books in the
United States. We are proud to have received eight awards from the Computer Press Association in recognition
of editorial excellence and three from Computer Currents’ First Annual Readers’ Choice Awards. Our best-
selling ...For Dummies® series has more than 50 million copies in print with translations in 31 languages. IDG
Books Worldwide, through a joint venture with IDG’s Hi-Tech Beijing, became the first U.S. publisher to
publish a computer book in the People’s Republic of China. In record time, IDG Books Worldwide has become
the first choice for millions of readers around the world who want to learn how to better manage their
businesses.
Our mission is simple: Every one of our books is designed to bring extra value and skill-building instructions
to the reader. Our books are written by experts who understand and care about our readers. The knowledge
base of our editorial staff comes from years of experience in publishing, education, and journalism —
experience we use to produce books to carry us into the new millennium. In short, we care about books, so
we attract the best people. We devote special attention to details such as audience, interior design, use of
icons, and illustrations. And because we use an efficient process of authoring, editing, and desktop publishing
our books electronically, we can spend more time ensuring superior content and less time on the technicalities
of making books.
You can count on our commitment to deliver high-quality books at competitive prices on topics you want
to read about. At IDG Books Worldwide, we continue in the IDG tradition of delivering quality for more than
30 years. You’ll find no better book on a subject than one from IDG Books Worldwide.

John Kilcullen
Chairman and CEO
IDG Books Worldwide, Inc.

Eighth Annual Eleventh Annual


Computer Press Computer Press
Awards 1992 Ninth Annual Tenth Annual Awards 1995
Computer Press Computer Press
Awards 1993 Awards 1994

IDG is the world’s leading IT media, research and exposition company. Founded in 1964, IDG had 1997 revenues of $2.05
billion and has more than 9,000 employees worldwide. IDG offers the widest range of media options that reach IT buyers
in 75 countries representing 95% of worldwide IT spending. IDG’s diverse product and services portfolio spans six key areas
including print publishing, online publishing, expositions and conferences, market research, education and training, and
global marketing services. More than 90 million people read one or more of IDG’s 290 magazines and newspapers, including
IDG’s leading global brands — Computerworld, PC World, Network World, Macworld and the Channel World family of
publications. IDG Books Worldwide is one of the fastest-growing computer book publishers in the world, with more than
700 titles in 36 languages. The “...For Dummies®” series alone has more than 50 million copies in print. IDG offers online
users the largest network of technology-specific Web sites around the world through IDG.net (http://www.idg.net), which
comprises more than 225 targeted Web sites in 55 countries worldwide. International Data Corporation (IDC) is the world’s
largest provider of information technology data, analysis and consulting, with research centers in over 41 countries and more
than 400 research analysts worldwide. IDG World Expo is a leading producer of more than 168 globally branded conferences
and expositions in 35 countries including E3 (Electronic Entertainment Expo), Macworld Expo, ComNet, Windows World
Expo, ICE (Internet Commerce Expo), Agenda, DEMO, and Spotlight. IDG’s training subsidiary, ExecuTrain, is the world’s
largest computer training company, with more than 230 locations worldwide and 785 training courses. IDG Marketing
Services helps industry-leading IT companies build international brand recognition by developing global integrated marketing
programs via IDG’s print, online and exposition products worldwide. Further information about the company can be found
at www.idg.com. 1/26/00
4682-1 fm.f.qc 11/20/00 15:45 Page vi

Credits
Acquisitions Editors Quality Control Technician
John Osborn Dina F Quan
Debra Williams Cauley
Permissions Editors
Project Editor Laura Carpenter
Kathi Duggan Laura Moss

Technical Editor Media Development Specialists


David Williams Laura Carpenter
Travis Silvers
Copy Editor
Luann Rouff Media Development Coordinators
Laura Carpenter
Project Coordinators Marisa Pearman
Louigene A. Santos
Danette Nurse Illustrators
Shelley Norris
Graphics and Production Specialists Rashell Smith
Robert Bihlmayer
John Greenough Proofreading and Indexing
Jude Levinson York Production Services
Michael Lewis
Gabriele McCann Cover Image
Victor Pérez-Varela Angela F. Hunckler
Ramses Ramirez Joyce Haughey

About the Author


Arthur Griffith has been programming computers for twenty-five years. He has
several years of experience in graphics programming, including X11 and motif. His
broad background includes oil and gas monitoring, satellite communications, insur-
ance company databases, real-time controls, and hardware diagnostic systems. He
specializes in writing computer language interpreters and compilers and has imple-
mented several special-purpose languages. Among the books he has written are
Java Master Reference and COBOL For Dummies. He is also the co-author of Peter
Norton’s Complete Guide to Linux. Arthur now lives in Homer, Alaska, and is a full-
time writer and teacher. You can contact him at [email protected].
4682-1 fm.f.qc 11/20/00 15:45 Page vii

For Mary
4682-1 fm.f.qc 11/20/00 15:45 Page viii
4682-1 fm.f.qc 11/20/00 15:45 Page ix

Preface
I f you want to write a KDE application, you’ve come to the right book.

This book is composed of numerous example programs, and each example is


accompanied by an explanation. When exploring or learning something new about
software, my personal preference is to have a simple example that shows me just
what I want and nothing else. Software is complicated enough that it becomes
impossible to explain all its nuances without offering examples, and an example
can be very confusing unless the key parts of it are clearly visible and explained.
Therefore, each example provided here is also a complete running program
designed to demonstrate just one thing.

The documentation of a program can be included as comments in the code, or it


can be separate text supplied along with the code. The examples in this book have
no embedded comments because each one is accompanied by text that explains it.
Leaving out comments produces a cleaner format, making it easier for a reader to
see the structure of the program. Most of the code is printed in the book with line
numbers, so the explanations can refer to specific lines.

What You Will Need


If you know how to program in C++, have access to a Linux computer, and are able
to download files from the Internet, this book will supply you with the know-how
you need to write KDE applications.

You can find a version of the development software on the CD, which is described
in Appendix A. Alternatively, see Appendix B to find out how to get the latest ver-
sion of everything. Appendix B contains a list of all the software you will need,
and where it can be found on the Internet.

Subject to Change
KDE is large and powerful, and it is moving fast. Fortunately, it is also very friendly.
4682-1 fm.f.qc 11/20/00 15:45 Page x

x Preface

Because KDE is an ongoing project, new things are being added constantly. This will
likely be the situation for the foreseeable future. Because of its open source status,
this growth will probably continue for the life of Linux and KDE.

From time to time, new methods for getting things done are added to the API, and it
is possible that some of the techniques described in this book will become outdated.
But KDE is quite stable now, so everything in the book should continue to work even
in the event of future changes. In some cases, if you run your applications from the
command line, there will be some text output to the console window. This text may
describe anything from a severe internal error to a simple piece of pertinent infor-
mation for the developer of the class you are using. This is typical of open source
development software, and symptomatic of software under development. Eventually,
in later versions, these messages will go away.

How to Use This Book


The book is divided into three parts. The first part is one continuous tutorial cover-
ing the basics of KDE programming. The second part is also composed of tutorials,
but the chapters can be consulted in any order on an as-needed basis. The third
part was not intended to be read sequentially — it is more of a reference section,
as are Appendixes C through G.

Appendix A or B: Installing the Software


If you have a late model Linux on a CD, you will have most of the software and may
have it all. If not, you have two options: You can install it from the CD supplied with
this book, or you can retrieve it from the Internet. Appendix A discusses the CD,
and Appendix B discusses the Internet.

Chapter 20: From Win32 to KDE


If you are a Win32 programmer, start by reading Chapter 20. This chapter is a point-
by-point comparison of two simple programs that are identical except that one is
written for Win32 and the other is written for KDE. Although there are some basic dif-
ferences, the underlying concepts behind writing a KDE application are very much
like those behind writing a Win32 application. For good measure, a GNOME program
is also included so you can compare the relative structure of all three applications.

Part I: Getting Started


Part I starts with the basics and puts the pieces together until you are able to cre-
ate applications that display buttons, labels, and other widgets, in whatever size,
4682-1 fm.f.qc 11/20/00 15:45 Page xi

Preface xi

shape, and position you would like them to be. The first chapter explains some of
the background information — mostly having to do with the Qt and KDE libraries
and how the various classes are used to construct running programs.

Chapter 2 is where the programming starts. This chapter describes the classes
available that can be used to create and display the main window for both Qt and
KDE applications. Chapter 3 expands on this by demonstrating methods for orga-
nizing the contents of windows and dialog boxes. Chapters 4 and 5 both cover the
subject of managing pop-up dialogs, both custom-built dialogs and the dialogs that
are predefined as part of either Qt or KDE. Chapter 6 explores the construction and
management of menus and toolbars. Chapter 7 describes the management of wid-
gets that come in groups — such as collections of buttons that toggle on and off
in relation to one another.

Part II: Step by Step


Part II can be studied from beginning to end, or you can skip around from one topic
to another as necessary. Although you will find a few cross-references from one
chapter to another, for the most part each chapter is independent of the others.

Chapter 8 describes how your program can respond to the mouse and keyboard.
Chapter 9 explains how you can manage and display pixel-level graphics — either
loaded from a file or compiled right into the program. Chapter 10 explains fonts
and the process for displaying strings of characters. Chapter 11 explores the options
you have for creating and managing colors. Chapter 12 contains several examples
of using the QPainter class to perform a wide range of detailed graphics rendering.
Chapter 13 delves deeper into graphics by exploring the process of manipulating
graphics to fit a specific size or for placement on a printed page. Chapter 13 also
includes a special section on animation. Chapter 14 contains examples of dragging
and dropping graphic and text objects. Chapter 15 explores the process of communi-
cating data from one application to another. Chapter 16 rounds out the section’s
presentation of classes with examples of some miscellaneous utilities. Chapter 17
is an exploration of KDE facilities for internationalization.

Part III: Reference and Mechanics


A widget is an object that contains a window of some kind and is capable of being
displayed on the screen. Chapter 18 is an alphabetical list of the widgets of Qt,
along with some examples of how to use them. Chapter 19 is a list, with examples,
of all of the KDE widgets.

Chapter 20 contains simple descriptive examples of the same program written in


Win32, KDE, and GNOME.
4682-1 fm.f.qc 11/20/00 15:45 Page xii

xii Preface

The Cross-Reference Appendixes


There is a lot of software here, and we all need some way to get a handle on it. The
appendixes contain reference information that can be very helpful in finding things.

Appendix Lists Contains

C Methods Each method name is listed, along with the classes


in which it can be found.
D Returned By The Qt and KDE classes have constructors, but you
can also acquire some of them from other classes
by calling methods that produce them. The
methods are listed here.
E Enumerated Types Enumerated types are listed alphabetically, showing
the classes in which they are defined and the
names of their values.
F Signals Signals are listed alphabetically with their argument
types and the classes from which they are emitted.
G Slots Slots are listed alphabetically with their argument
types and the classes that contain them.

Example Code
Most things described in this book are described by actual code samples. As much
as possible, these examples are complete running programs. I find that a simple
example — an example that demonstrates just one thing — is most useful to me
when I need to figure out (or be reminded) how to do something. Each example
is intended to demonstrate one, or possibly two, specific things.

The examples are not meant to demonstrate some kind of “correct” coding prac-
tice, or even the “correct” way to do a particular task. There is no correct way
because, in KDE as in all other software systems, there is usually more than one
way to get any particular job done. And the overall style and design of a program
are up to the programmer.

All of the example code, along with the makefiles for each, can be found here:

http://www.belugalake.com/book/kdebible
4682-1 fm.f.qc 11/20/00 15:45 Page xiii

Acknowledgments
I want to thank all of the KDE developers. This group has created an excellent
graphical user interface, and they have a right to be proud of what they’ve done.
I am grateful to the many members of the KDE developer’s group that took the time
to answer my questions and help keep me on the right track.

John Osborn came up with the original concept for this book. He defined the origi-
nal scope of the project and helped keep me in line when I tended to wander. I also
want to thank Laura Lewin, Andy Marinkovich, and Debra Williams Cauley for
putting things together in such a way that the book became a reality.

Kathi Duggan, while repairing things I wrote that no human being could otherwise
read, kept track of every chapter and graphic as it moved from one stage of produc-
tion to another. I feel more secure in putting my name on the book because David
Williams checked everything to make certain that the book was technically accurate.
Luann Rouff showed me how to convert my strange sentences into something that
could be read and understood.

And, as always, a special thank you to that special lady who makes everything
happen: Margot Maley at Waterside.
4682-1 fm.f.qc 11/20/00 15:45 Page xiv

Contents at a Glance
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Part I: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Chapter 1: What Is This Thing Called KDE? . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2: Creating and Displaying a Window . . . . . . . . . . . . . . . . . . . . 11
Chapter 3: Laying Out Widgets in a Window . . . . . . . . . . . . . . . . . . . . . . 27
Chapter 4: Displaying a Pop-Up Dialog . . . . . . . . . . . . . . . . . . . . . . . . . 65
Chapter 5: The Predefined Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Chapter 6: Menus and Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Chapter 7: Grouping Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

Part II: Step by Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173


Chapter 8: The Mouse and the Keyboard . . . . . . . . . . . . . . . . . . . . . . . 175
Chapter 9: Graphics File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Chapter 10: Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Chapter 11: Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Chapter 12: Drawing and Painting with QPainter . . . . . . . . . . . . . . . . . . 259
Chapter 13: Graphics Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Chapter 14: Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Chapter 15: Interprocess Communications and Applets . . . . . . . . . . . . . . 355
Chapter 16: Some General Utility Classes . . . . . . . . . . . . . . . . . . . . . . 371
Chapter 17: Internationalization and Configuration . . . . . . . . . . . . . . . . . 395

Part III: Reference and Mechanics . . . . . . . . . . . . . . . . . . . . 413


Chapter 18: The Widgets of Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Chapter 19: The Widgets of KDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Chapter 20: Comparative Anatomy of Windowing Programs . . . . . . . . . . . 581
4682-1 fm.f.qc 11/20/00 15:45 Page xv

Appendix A: What’s on the CD-ROM? . . . . . . . . . . . . . . . . . . . . . . . . . 591


Appendix B: Setting Up for Software Development . . . . . . . . . . . . . . . . . 597
Appendix C: Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Appendix D: Returned By . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
Appendix E: Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Appendix F: Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Appendix G: Slots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
End-User License Agreement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
GNU General Public License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
CD-ROM Installation Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 766
4682-1 fm.f.qc 11/20/00 15:45 Page xvi
4682-1 fm.f.qc 11/20/00 15:45 Page xvii

Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Part I: Getting Started 1


Chapter 1: What Is This Thing Called KDE? . . . . . . . . . . . . . . . . . 3
The Structure of a KDE Application . . . . . . . . . . . . . . . . . . . . . . . . 3
The Software Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
glib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
X11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
C++ API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Qt Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
KDE Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
About Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The QObject Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The MOC Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
About KDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Events Happen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Names of Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Chapter 2: Creating and Displaying a Window . . . . . . . . . . . . . . 11


Hello Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Hello KDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
A Simple Window Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Compound Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Listening to a Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Defining a Slot for a Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Chapter 3: Laying Out Widgets in a Window . . . . . . . . . . . . . . . 27


Geometric Widget Placement . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Size of a Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Fixed Grid Widget Placement . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Stretched Grid Widget Placement . . . . . . . . . . . . . . . . . . . . . . . . 34
Widgets in Multiple Grid Cells . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4682-1 fm.f.qc 11/20/00 15:45 Page xviii

xviii Contents

Vertical Box Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40


Horizontal Box Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Boxes with Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
A Layout Within a Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
A Layout That Is Also a Widget . . . . . . . . . . . . . . . . . . . . . . . . . . 50
ISa Instead of HASa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Widgets Inside Widgets (Horizontal) . . . . . . . . . . . . . . . . . . . . . . 55
Widgets Inside Widgets (Vertical) . . . . . . . . . . . . . . . . . . . . . . . . 59

Chapter 4: Displaying a Pop-Up Dialog . . . . . . . . . . . . . . . . . . 65


A Simple Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Using Signals and Slots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
A Signals and Slots Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
KDialogBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
KDialogBase Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Using KDialogBase to Build a Dialog . . . . . . . . . . . . . . . . . . . . . . . 83
An Alternate Approach To KDialogBase Data . . . . . . . . . . . . . . . . . . 88
KMesageBox Derives From KDialogBase . . . . . . . . . . . . . . . . . . . . 89

Chapter 5: The Predefined Dialogs . . . . . . . . . . . . . . . . . . . . 99


The About Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
QFileDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
QTabDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
QProgressDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

Chapter 6: Menus and Toolbars . . . . . . . . . . . . . . . . . . . . . . 123


KTMainWindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
The Menu Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Pop-up Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
The Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
The Status Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Chapter 7: Grouping Widgets . . . . . . . . . . . . . . . . . . . . . . . 147


KButtonBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Grouping Buttons with a Single Slot . . . . . . . . . . . . . . . . . . . . . . 150
Grouping Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Grouping Check Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Some Widgets Are Also Frames . . . . . . . . . . . . . . . . . . . . . . . . . 160
Framing Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The Box QFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The Panel QFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
The WinPanel QFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Using QFrame to Draw Lines . . . . . . . . . . . . . . . . . . . . . . . 166
Sharing Window Real Estate . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
4682-1 fm.f.qc 11/20/00 15:45 Page xix

Contents xix

Part II: Step by Step 173


Chapter 8: The Mouse and the Keyboard . . . . . . . . . . . . . . . . 175
From a Port to a Slot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
The Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Mouse Grabbing and Releasing . . . . . . . . . . . . . . . . . . . . . . . . . 182
Changing the Cursor’s Appearance . . . . . . . . . . . . . . . . . . . . . . . 184
Designing Your Own Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Chapter 9: Graphics File Formats . . . . . . . . . . . . . . . . . . . . . 197


Two Kinds of Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
The XPM Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Showing XPM from Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Loading a Pixmap from a File . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Using a Pixmap to Decorate a Button . . . . . . . . . . . . . . . . . . . . . 204
The XBM Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
The Bitmap Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Customizing Graphics for Menus and Toolbars . . . . . . . . . . . . . . . . 208

Chapter 10: Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215


The Anatomy of a Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Names of the Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Setting the Font of a Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Selecting a Font with QFontDialog . . . . . . . . . . . . . . . . . . . . . . . 221
Selecting a Font with KFontDialog . . . . . . . . . . . . . . . . . . . . . . . 223
Font Placement by Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Font Placement by Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . 231

Chapter 11: Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237


The Architecture of Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Constructing a QColor Object . . . . . . . . . . . . . . . . . . . . . . . . . . 242
The KColorDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
QColors in a QColorGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
QColorGroups in a QPalette . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Setting Colors for a Few Widgets . . . . . . . . . . . . . . . . . . . . . . . . 254
Using the QPalette for Your Own Coloring . . . . . . . . . . . . . . . . . . . 256

Chapter 12: Drawing and Painting with QPainter . . . . . . . . . . . 259


Painting Pixels to a QPaintDevice . . . . . . . . . . . . . . . . . . . . . . . . 259
Some Rectangle Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Pens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Standard Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
4682-1 fm.f.qc 11/20/00 15:45 Page xx

xx Contents

Creating Custom Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270


Every QPaintDevice Has Metrics . . . . . . . . . . . . . . . . . . . . . . . . 272
Pixel Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Drawing Arrays of Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Vector Line Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Line Segments and Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Ellipses and Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Drawing Parts of Circles and Ellipses . . . . . . . . . . . . . . . . . . . . . 285
Rectangles with Rounded Corners . . . . . . . . . . . . . . . . . . . . . . . 288
Drawing Pixmaps and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

Chapter 13: Graphics Manipulation . . . . . . . . . . . . . . . . . . . 295


Using a QPicture to Store Graphics . . . . . . . . . . . . . . . . . . . . . . . 295
Painting Graphics to a Printer . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Printer Information and Control . . . . . . . . . . . . . . . . . . . . . . . . 301
Fitting a Drawing to a Window . . . . . . . . . . . . . . . . . . . . . . . . . 306
Fitting a Drawing to a Subwindow . . . . . . . . . . . . . . . . . . . . . . . 307
Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Shear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Translate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Rotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
A Quadratic Bezier Curve . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Animation with Pixmap Sequences . . . . . . . . . . . . . . . . . . . . . . . 320
Accessing Pixel Values with QImage . . . . . . . . . . . . . . . . . . . . . . 326
Using an Icon Provider in a QFileDialog . . . . . . . . . . . . . . . . . . . . 331

Chapter 14: Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . 339


A Simple Text Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Drag and Drop of Both Text and Image Data . . . . . . . . . . . . . . . . . 344
Cut and Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

Chapter 15: Interprocess Communications and Applets . . . . . . . 355


The DCOP Communications Model . . . . . . . . . . . . . . . . . . . . . . . 356
Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
A Unique Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
An Example Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

Chapter 16: Some General Utility Classes . . . . . . . . . . . . . . . . 371


The String Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Examining a QString . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Modifying a QString . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
QString Number Conversion . . . . . . . . . . . . . . . . . . . . . . . 376
The QString Translator . . . . . . . . . . . . . . . . . . . . . . . . . . 378
4682-1 fm.f.qc 11/20/00 15:45 Page xxi

Contents xxi

The White Space of a QString . . . . . . . . . . . . . . . . . . . . . . . 379


QStringList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Running a Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
The QDate Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
The QTime Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
The QDateTime Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Reading from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Streaming Text to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Streaming Text from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392

Chapter 17: Internationalization and Configuration . . . . . . . . . . 395


A Translatable Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Declaring Translatable Strings . . . . . . . . . . . . . . . . . . . . . . . . . 399
Manipulating Translated Strings . . . . . . . . . . . . . . . . . . . . . . . . 400
Constructing the Translation Files . . . . . . . . . . . . . . . . . . . . . . . 401
Unicode and QChar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

Part III: Reference and Mechanics 413


Chapter 18: The Widgets of Qt . . . . . . . . . . . . . . . . . . . . . . 415
QButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
QButtonGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
QCheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
QColorDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
QComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
QDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
QFileDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
QFontDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
QFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
QGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
QGroupBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
QHBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
QHButtonGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
QHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
QHGroupBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
QIconView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
QInputDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
QLCDNumber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
QLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
QLineEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
QListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
QListView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
4682-1 fm.f.qc 11/20/00 15:45 Page xxii

xxii Contents

QMainWindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
QMenuBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
QMessageBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
QMultiLineEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
QPopupMenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
QPrintDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
QProgressBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
QProgressDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
QPushButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
QRadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
QScrollBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
QScrollView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
QSemiModal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
QSizeGrip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
QSlider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
QSpinBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
QSplitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
QStatusBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
QTabBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
QTabDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
QTabWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
QTextBrowser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
QTextView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
QToolBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
QToolButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
QVBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
QVButtonGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
QVGroupBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
QWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
QWidgetStack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
QWizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488

Chapter 19: The Widgets of KDE . . . . . . . . . . . . . . . . . . . . . 491


KAboutContainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
KAboutContainerBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
KAboutContributor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
KAboutDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
KAboutKDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
KAboutWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
KAccelMenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
KAnimWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
KAuthIcon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
KBugReport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
KButtonBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
KCharSelect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
KCharSelectTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
4682-1 fm.f.qc 11/20/00 15:45 Page xxiii

Contents xxiii

KCModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
KColorButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
KColorCells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
KColorCombo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
KColorDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
KColorPatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
KComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
KContainerLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
KDatePicker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
KDateTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
KDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
KDialogBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
KDialogBaseButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
KDirectionButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
KDockMainWindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
KDockWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
KDoubleNumInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
KDualColorButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
KEdFind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
KEdGotoLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
KEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
KEdReplace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
KFileDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
KFontChooser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
KFontDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
KFormulaEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
KFormulaToolBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
KGradientSelector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
KHSSelector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
KHTMLView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
KIconButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
KIconDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
KIconView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
KImageTrackLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
KIntNumInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
KIntSpinBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
KKeyButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
KLed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
KLineEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
KLineEditDlg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
KListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
KListView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
KMenuBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
KNumInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
KPaletteTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
KPanelApplet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
4682-1 fm.f.qc 11/20/00 15:45 Page xxiv

xxiv Contents

KPasswordDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
KPasswordEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
KPopupMenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
KProgress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
KRestrictedLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
KRootPermsIcon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
KRuler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
KSelector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
KSeparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
KSpellConfig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
KSpellDlg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
KSplitList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
KStatusBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
KStatusBarLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
KTextBrowser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
KTextPrintDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
KTMainWindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
KToolBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
KToolBarButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
KWizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
KXYSelector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579

Chapter 20: Comparative Anatomy of Windowing Programs . . . . 581


A Win32 Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
A KDE Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
A Point-by-Point Win32 and KDE Comparison . . . . . . . . . . . . . . . . . 586
Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
The Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
Responding to Events . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
The Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Program Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Global Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
A GNOME Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
4682-1 fm.f.qc 11/20/00 15:45 Page xxv

Contents xxv

Appendix A: What’s on the CD-ROM? . . . . . . . . . . . . . . . . . . 591

Appendix B: Setting Up for Software Development . . . . . . . . . . 597

Appendix C: Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603

Appendix D: Returned By . . . . . . . . . . . . . . . . . . . . . . . . . 657

Appendix E: Enumerated Types . . . . . . . . . . . . . . . . . . . . . . 687

Appendix F: Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

Appendix G: Slots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
End-User License Agreement . . . . . . . . . . . . . . . . . . . . . . . . . 756
GNU General Public License . . . . . . . . . . . . . . . . . . . . . . . . . 759
CD-ROM Installation Instructions . . . . . . . . . . . . . . . . . . . . . . 766
4682-1 fm.f.qc 11/20/00 15:45 Page xxvi
4682-1 ch01.f.qc 11/13/00 14:09 Page 3

What Is This
Thing Called
1
C H A P T E R

✦ ✦ ✦ ✦

KDE? In This Chapter

Understanding the
different parts and
the overall structure

T
of KDE
he name of the software is the K Desktop Environment,
called KDE for short. This chapter is an introduction to the Learning the part
application development environment of KDE. It is a graphical played by Qt
user interface that is popular on Linux and other flavors of the in application
UNIX family of operating systems. Virtually all graphical inter- development
faces in the UNIX family are built on top of the X Windowing
System. The X Windowing System gives the graphics its porta- Learning the
bility across many systems; the Qt library of graphics objects part played by
provides the basic building blocks of an application; and the KDE in application
KDE library provides a standard look and feel. development

Learning about
widgets and the
The Structure of a KDE Application event model
When you write a KDE application, you are writing code that ✦ ✦ ✦ ✦
will rest on top of a lot of other code. Most of the detailed work
of getting your application written has already been done, and
that work resides in the libraries of code that will link to your
application to do the things you would like for it to do. The
diagram in Figure 1-1 should give you some idea of the levels
of software that make up a KDE application.
4682-1 ch01.f.qc 11/13/00 14:09 Page 4

4 Part I ✦ Getting Started

Figure 1-1: The levels of software


Application
for a KDE application in Linux
KDE Classes

Qt Classes

C++ API

glib X11

System

The way the diagram is drawn makes it appear that the levels are completely sepa-
rate, but that’s not the case. For example, perfectly valid calls are made from KDE
classes to glib functions, and there is nothing to prevent your application from
making calls directly to, say, glib or the system calls. An application typically uses
classes from both KDE and Qt. However, the calls are only downward — for
example, no part of the Qt API makes use of anything in KDE.

This book is all about using the material at the second and third levels (KDE and
Qt) to create things that go at the top level (applications). There is little or no infor-
mation about using the things at the other levels. Actually, that is one of the main
purposes of KDE and Qt — they simplify the process of developing applications by
insulating the programmer from the minute details handled at the lower levels.

The Software Levels


The following sections describe each of the software levels shown in Figure 1-1.

System
This is the lowest layer of software available to every Linux application. A set of
low-level system calls provides direct access into the operation system, and its
drivers, to do things like open files and create directories. Because the Linux
kernel is written in C, these are all C function calls.

glib
This is a set of C functions, macros, and structures that are used by all the layers
above it; and, quite often, it is also used by applications. The glib library contains
functions for memory allocation, string formatting, date and time, I/O, and timers.
It also has utility functions for linked lists, arrays, hash tables, trees, quarks,
and caches. One of the crucial functions handled by glib is the main loop, which
4682-1 ch01.f.qc 11/13/00 14:09 Page 5

Chapter 1 ✦ What Is This Thing Called KDE? 5

enables KDE to handle multiple resources while it simultaneously executes the


code of an application.

X11
This is the graphics layer that handles the low-level functions used to control the
display. All the fundamental windowing functions are included — these are the func-
tions that display windows and respond to the mouse and keyboard. This library has
become very stable over the years and the version numbers have rarely changed.
Currently, it is version 11 (as indicated by its name). And, because version 11 is
in release 6, it is also known as X11R6. Its original name was without the version
number, so it is often simply called X.

C++ API
Everything above this layer is written using C++, so the C++ run-time system is
called on for things such as creating new objects and handling I/O streams.

Qt Classes
This set of C++ classes implements the various widgets (buttons, window frames,
and so on) that can be used to create an application. It has the capability of combing
windows to together to create complicated graphics dialogs. At the same time that it
displays these widgets, it can respond to the mouse and keyboard for more input,
and dispatch information from the input window to the correct part of the program.

KDE Classes
These classes modify and add functionality to the Qt classes. There is a large num-
ber of KDE classes, but the majority of them extend directly from one or more of the
Qt classes. This layer is what gives KDE its unique appearance, and standardizes
the way the window, mouse, and keyboard all interact with one another.

Applications
There are two basic flavors of applications. You can create either a Qt application
or a KDE application. A Qt application is one that creates a QApplication object to
initialize itself, while a KDE application initializes itself by creating a KApplication
object. The KApplication class extends the QApplication class by adding the
things that are necessary for the standard appearance and capabilities of a
KDE application.
4682-1 ch01.f.qc 11/13/00 14:09 Page 6

6 Part I ✦ Getting Started

About Qt
Qt is a library of C++ GUI application development software. Its purpose is to provide
everything needed to develop the user interface portion of applications. It does this
primarily in the form of a collection of C++ classes.

The Norwegian company Troll Tech (http://www.trolltech.com) first introduced


Qt as a commercial product in 1995.

The set of Qt classes is quite robust. There is nothing to stop you from writing com-
plete applications using Qt. In fact, to demonstrate the basic form of an application,
the first few examples in this book use only Qt. The Qt classes include everything
from basic window controls, drag and drop, and internationalization to network
programming.

In the past there has been some concern over the use of Qt in some open source
development areas because of limitations in software licensing. But licensing is
no longer a concern. Trolltech has recently released a version of Qt that is entirely
free, and it is licensed under the GPL (GNU General Public License). The same
software can also be used under the QPL (Q Public License), depending on the
particular licensing requirements. This dual licensing approach allows for
the development of both open source software and proprietary software.

With release Qt 2.2.1, there are three different version of the software:

✦ The Qt Free Edition is licensed under the GPL and can be downloaded
and freely used for any open source project.
✦ The Qt Professional Edition is intended for use by commercial and proprietary
software development. The license and the software must be purchased.
✦ The Qt Enterprise Edition is licensed the same as the Qt Professional Edition,
but contains additional software modules. These extensions include OpenGL,
networking, XML, spreadsheets, and a special optimized 2D graphics package.

The QObject Class


All but about a dozen of the Qt classes inherit from the base class QObject. This
means that virtually every class in the Qt library contains the same basic set of
methods. The constructor for this class can optionally accept the address of a
parent object, and a character string that assigns the object a name:

QObject(QObject *parent = 0, const char *name = 0);

The following methods are defined in QObject. Most of these methods are used in
one example or another in this book.

void blockSignals(bool b);


QObject *child(const char *name, const char *type = 0);
4682-1 ch01.f.qc 11/13/00 14:09 Page 7

Chapter 1 ✦ What Is This Thing Called KDE? 7

const QObjectList *children() const;


virtual const char *className() const;
static bool connect(const QObject *sender, const char *signal,
const QObject *receiver, const char *member);
bool connect(const QObject *sender, const char *signal,
const char *member) const;
static bool disconnect(const QObject *sender,
const char *signal, const QObject *receiver,
const char *member);
bool disconnect(const char *signal = 0,
const QObject *receiver = 0, const char *member = 0);
bool disconnect(const QObject *receiver,
const char *member = 0);
void dumpObjectInfo();
void dumpObjectTree();
virtual bool event(QEvent *);
virtual bool eventFilter(QObject *, QEvent *);
bool highPriority() const;
bool inherits(const char *) const;
virtual void insertChild(QObject *);
void installEventFilter(const QObject *);
bool isA(const char *) const;
bool isWidgetType() const;
void killTimer(int id);
void killTimers();
virtual QMetaObject *metaObject() const;
const char *name() const;
const char *name(const char *defaultName) const;
static const QObjectList *objectTrees();
QObject *parent() const;
QVariant property(const char *name) const;
QObjectList *queryList(const char *inheritsClass = 0,
const char *objName = 0, bool regexpMatch = TRUE,
bool recursiveSearch = TRUE);
virtual void removeChild(QObject *);
void removeEventFilter(const QObject *);
virtual void setName(const char *name);
bool setProperty(const char *name, const QVariant &value);
bool signalsBlocked() const;
int startTimer(int interval);
QStringList superClasses(bool includeThis = FALSE) const;
static QString tr(const char *);

Some Qt objects have the ability to emit signals that can be received by other objects
inside your program. A QObject object emits a signal whenever its destructor
is called:

void destroyed();

Signals, and the slots that receive them, are briefly described in the next section,
and many examples are included in the book.
4682-1 ch01.f.qc 11/13/00 14:09 Page 8

8 Part I ✦ Getting Started

The MOC Compiler


One feature used by developers is the Meta Object Compiler (also called the MOC
compiler). The MOC compiler reads your source code and generates special C++
source files for you to compile and link along with your application. These special
files contain the code necessary for one object to emit a “signal” that is received by
a “slot” in one or more other objects. This is the method used to asynchronously
transmit information from one object to another within an application.

The MOC compiler is triggered by the presence of the Q_OBJECT macro within a
class definition to determine whether to generate code, and what code is generated.
The resulting source code can be either compiled separately and linked, or simply
included in your code with the #include directive.

Using the MOC compiler not only activates the signals and slots, but also generates
code that enables some special methods that are defined in every Qt class (and
thus, by inheritance, in every object in your program). These special methods,
listed in Table 1-1, are defined in the QtObject class.

Table 1-1
The MOC Methods of QObject
Method Description

className() Returns, as a character string, the name of the class. This does
not require RTTI (Run Time Type Identification) support.
inherits() Returns a Boolean value that specifies whether or not this class
inherits from some other named class.
tr() Performs the translation of a string for internationalization
setProperty() Sets an object property by name
property() Returns a named object property
metaObject() Returns a QMetaObject object for the class. A meta object
contains detailed descriptive information for a class.

About KDE
KDE is an open source development project of a graphical desktop environment.
Other than being the first letter of the acronym, the K doesn’t stand for anything.
It is just a name.
4682-1 ch01.f.qc 11/13/00 14:09 Page 9

Chapter 1 ✦ What Is This Thing Called KDE? 9

The KDE software is constructed using Qt. The project began in 1996, the year after
the first version of Qt was released. Since then, the project has grown to become a
very complete desktop environment with a large collection of applications. To learn
more about the status and content of KDE, visit the http://www.kde.org Web site.

From the software developer’s point of view, KDE is quite simple. While most of
the software written as part of the KDE project is used as an integral part of the
desktop environment, a large number of classes have also been developed; and
they are included as part of a core KDE API. These classes are meant to help give
KDE applications a standard look and feel. Most of these classes inherit from one
or more classes of the Qt library, and some of the KDE classes add capabilities
beyond that of Qt, but most of them are simply for the sake of maintaining the stan-
dard appearance of KDE. It would be easy enough to write your entire application
using only the classes of Qt, but if you use the KDE classes, your application is
more likely to appear integrated with the rest of the desktop.

Events Happen
An application that runs in the K Desktop Environment is an event-driven program.
This means that when a program starts running, it displays its window (or windows)
and waits for input from the mouse or keyboard. This input comes wrapped inside
objects called events. An event can also tell the program that a window has been
closed, or that the window has been exposed after being hidden behind another
window. The application’s entire purpose is to respond intelligently to the keyboard
and mouse.

An application has one main top-level window. It can also have other windows.
These windows can exist for the entire life of the application, or they can appear
and disappear as the application responds to events.

Each window is encapsulated in a widget. The top-level window of an application is


a widget. Each pop-up window is also a widget. In fact, the entire display is made up
of widgets. Because one widget is capable of containing and displaying other wid-
gets, every button, label, and menu item is its own individual widget. Programming
the graphical display portion of your application is a matter of creating and com-
bining widgets, and then writing the code that activates the widgets and responds
to the events received by the widgets.

A widget is any class that inherits from the Qt class named QWidget. A QWidget
object contains and manages its own displayable window. It can also be set to
respond to events issued by the mouse and keyboard (and whatever else you
have for input) that are sent to the window inside the widget. It knows things
about its current visibility, its size, its background color, its foreground color,
its position on the display, and so on. You can use the widgets defined in either
Qt or KDE, or you can create your own by using QWidget as a base class.
4682-1 ch01.f.qc 11/13/00 14:09 Page 10

10 Part I ✦ Getting Started

The Names of Things


The Qt class names begin with the letter Q and the KDE class names begin with the
letter K. That way, when you read the source code of a program, you can determine
where a class is defined. If you find two classes that have the same name except for
the first letter, it means that one is an extension of the other. For example, the KDE
class KPixmap uses the Qt class QPixmap as its base class.

Every class in Qt and KDE is defined in a header file. In every case (well, almost
every case), the header file derives its name from the name of the class. For exam-
ple, the header file for the QPopupMenu class is named qpopupmenu.h, and the class
KFontDialog is defined in kfontdialog.h. However, this naming convention is not
universally true because more than one class can be defined in a header. For exam-
ple, the class KFontChooser is also defined in kfontdialog.h. Also, some source
filenames are abbreviated. For example, the header for KColorDialog is named
kcolordlg.h.

Summary
This chapter provided a short, and very general, introduction to the programming
environment of KDE. The concepts introduced in this chapter included:

✦ Several layers of software support the KDE software library.


✦ The X Windowing System controls the low-level GUI interface. The KDE library is
a thin layer of software that is very dependent on the Qt software immediately
below it.
✦ All applications are event driven. The application displays at least one
window, and then waits for input from the mouse or keyboard.

If you are a Windows programmer but are unfamiliar with KDE, you may want
to read Chapter 20, which compares a Windows program with a KDE program.
Otherwise, proceed to Chapter 2, which starts with examples of very simple
KDE applications.

✦ ✦ ✦
4682-1 ch02.f.qc 11/13/00 14:09 Page 11

Creating and
Displaying a
2
C H A P T E R

✦ ✦ ✦ ✦

Window In This Chapter

Writing a simple
program with just a
few lines of code

T his chapter discusses the fundamental form of a Qt or


KDE application program. If you are new to KDE, you
are going to be pleasantly surprised. A good deal of effort
Understanding that
implementing a KDE
program is simply
has gone into making the basics as simple possible. The implementing a
creation of a basic application is so simple it would be Qt program with
hard to get it wrong. added capabilities

The examples in this chapter are designed to explain the basic Creating a window
format of the source and the process necessary to convert that by coding a C++
source into an executable program. To keep things as simple object for it
as possible, and so you can see the relationship between the
various parts of a program, these examples all use simple hand- Responding to input
written makefiles. The first example is a minimal Qt application, by specifying slots
and the second is a minimal KDE application. Other examples to receive signals
show how you can respond to a pushbutton and create a
display widget containing other widgets. ✦ ✦ ✦ ✦

Hello Qt
The following example program creates and displays a simple
window. It doesn’t do anything other than display a line of
text, but it gives you an idea of the fundamental requirements
of a Qt program. The window is shown in Figure 2-1.

1 /* helloworld.cpp */
2 #include <qapplication.h>
3 #include <qlabel.h>
4 #include <qstring.h>
5
6 int main(int argc,char **argv)
7 {
4682-1 ch02.f.qc 11/13/00 14:09 Page 12

12 Part I ✦ Getting Started

8 QApplication app(argc,argv);
9 QLabel *label = new QLabel(NULL);
10 QString string(“Hello, world”);
11 label->setText(string);
12 label->setAlignment(
13 Qt::AlignVCenter | Qt::AlignHCenter);
14 label->setGeometry(0,0,180,75);
15 label->show();
16 app.setMainWidget(label);
17 return(app.exec());
18 }

Figure 2-1: A simple Qt program displaying text

The file qapplication.h included on line 2 is almost always included in the same
source file that contains the main() function. This example uses a QLabel widget
to display text, so it is necessary to also include qlabel.h. And a QString object
is required to specify the text displayed by the QLabel object, so qstring.h is
included on line 4.

Line 8 creates a QApplication object named app. The QApplication object is a


container that will hold the top-level window (or set of windows) of an application.
A top-level window is unique in that it never has a parent window in the applica-
tion. Because the QApplication object takes over things and manages your
application, there can only be one of these per program. Also, the creation of
a QApplication object initializes the Qt system, so it must exist before any
of the other Qt facilities are available.

A Qt program is a C++ program. This means that in order to start the program,
a function named main() will be called by the operating system. And, like all C++
programs, command-line options may or may not be passed to the main() function.
The command-line options are passed on to the Qt software as part of the initializa-
tion process, as shown on line 8.

The two command-line arguments, argc and argv, are used in the construction of
app because some special flags and settings can be specified. For example, starting
a Qt program with -geometry will specify the size and location of the window it
displays. By altering the profile information that starts a program, a user can
personalize a program’s appearance.

A QLabel widget is created on line 9. A QLabel widget is simply a window that is


capable of displaying a string of characters. The label is created with its specified
4682-1 ch02.f.qc 11/13/00 14:09 Page 13

Chapter 2 ✦ Creating and Displaying a Window 13

parent widget as NULL because this label is to be the top-level window, and top-level
windows have no parents. As it is created, the label contains no text, but it is pro-
vided text by being passed the QString object created on line 10. The QString
object is inserted into the QLabel with the call to setText() on line 11.

The default action for a QLabel is to display the character string centered vertically
and justified to the left, so the call to setAlignment() is made on line 12 to center
the text both vertically and horizontally.

The call to setGeometry() on line 14 determines the location, height, and width
of the label widget inside the QApplication window. For this example, the label
is positioned at location (0,0), which is the upper-left corner of the main window.
It is also instructed to be 180 pixels wide by 75 pixels high. Before anything is
displayed, the main window will query the label to find out its size, and then
the main window will set its own size to contain the label.

The call to show() on line 16 is necessary in order for the label to actually appear
on the window. The show() function does not immediately display the widget, it
only configures it so that it will be displayed when the time comes. The parent
window — in this case, the QApplication window — assumes the task of displaying
the label, but will only do so if there has been a call to the label’s show() method.
Another function, named hide(), can be used to cause a widget to disappear from
the display.

The call to setMainWidget() on line 11 inserts the label into the main window. To
keep this example simple, the QLabel object is used, but normally the widget will
be some sort of compound widget that contains the collection of widgets, text,
and other elements of the main window of an application.

Finally, a call is made to exec() on line 17. This function does not return until it
is time for the program to cease execution. It returns an int value representing
its completion status; and because we are not processing status codes, the value
is simply returned to the system.

Because the program is simple and consists of only one source file, the makefile that
compiles it is quite simple:

INCL= -I$(QTDIR)/include -I$(KDEDIR)/include


CFLAGS= -pipe -O2 -fno-strength-reduce
LFLAGS= -L$(QTDIR)/lib -L$(KDEDIR)/lib -L/usr/X11R6/lib
LIBS= -lqt -lX11 -lXext
CC=g++

helloworld: helloworld.o
$(CC) $(LFLAGS) -o helloworld helloworld.o $(LIBS)

helloworld.o: helloworld.cpp
4682-1 ch02.f.qc 11/13/00 14:09 Page 14

14 Part I ✦ Getting Started

clean:
rm -f helloworld
rm -f helloworld.o

.SUFFIXES: .cpp

.cpp.o:
$(CC) -c $(CFLAGS) $(INCL) -o $@ $<

The makefile assumes that the environment variables QTDIR and KDEDIR are
defined as the name of the installation directory of the Qt and KDE development
systems. Normally, these two environment variables have their definitions config-
ured when you install the software. Five names are defined inside the makefile, as
shown in Table 2-1.

Table 2-1
Variables Defined in the Makefile
Name Contents

INCL This is the path name of the location of the header files. This is passed to
the compiler to tell it where to look for header files. The compiler always
looks in /usr/include for the standard headers.
CFLAGS This is the list of options passed to the compiler. The -pipe option
instructs the compiler to use pipes, instead of temporary files, when passing
data between two stages of compilation. The -O2 option specifies a fairly
high level of optimization. The –fno-strength-reduce option prevents
the optimization from reducing or eliminating iteration variables.
LFLAGS This is a list of options passed to the linker. Each of the -L options specifies
a directory that is expected to contain one or more libraries.
LIBS This is the list of library namesthat will be needed by this program. The
named libraries will be sought in the directories named by LFLAGS. The
name of each will be expanded to name the library file. For example,
-lqt is changed to libqt.so, and -lX11 becomes libX11.so.
CC This is the name of the compiler.

The last two lines of the makefile are used to instruct make on how to form a com-
mand that will convert a .cpp file into a .o file. There is only one source file in this
example, but if there were more, using the conversion rule allows the compile
command to be defined once and be applied to the entire makefile.
4682-1 ch02.f.qc 11/13/00 14:09 Page 15

Chapter 2 ✦ Creating and Displaying a Window 15

Note There are an infinite number of ways to write a makefile. This example was made
relatively simple so it would be easy to read. As you develop an application, you
will probably discover other things need to be added to your makefiles.

Hello KDE
This example, shown in Figure 2-2, is the same as the previous one except it is
based on a KApplication object, rather than a QApplication object. Because
the KApplication class is based on QApplication, there are no fundamental dif-
ferences other than the addition of KDE facilities such as styles and themes, the
capability to use KDE widgets, access to the standard KDE configuration, access
to session management information, and the capability to launch the user’s Web
browser and e-mail client.

1 /* hellokde.cpp */
2 #include <kapp.h>
3 #include <qlabel.h>
4 #include <qstring.h>
5
6 int main(int argc,char **argv)
7 {
8 KApplication app(argc,argv,”hellokde”);
9 QLabel *label = new QLabel(NULL);
10 QString string(“Hello, KDE”);
11 label->setText(string);
12 label->setAlignment(
13 Qt::AlignVCenter | Qt::AlignHCenter);
14 label->setGeometry(0,0,180,75);
15 label->show();
16 app.setMainWidget(label);
17 return(app.exec());
18 }

Figure 2-2: A simple KDE program displaying text

The KApplication object is defined in the header file kapp.h included on line 2.
The kapp.h file includes the qapplication.h file, so every facility available to a
Qt program is also available to a KDE program. The header files included on lines 3
and 4 hold the definitions of the QLabel and QString classes.
4682-1 ch02.f.qc 11/13/00 14:09 Page 16

16 Part I ✦ Getting Started

The KApplication object is created on line 8 by being passed the command-line


arguments and a name for the application. This name can be used for such applica-
tion-specific tasks as locating icons, receiving messages, and reading configuration
information.

Because a KDE object is being used in this program, it is necessary to include the
KDE library that holds the object. There are some specialized KDE libraries, but
the main two libraries are libkdecore and libkdeui.

INCL= -I$(QTDIR)/include -I$(KDEDIR)/include


CFLAGS= -O2 -fno-strength-reduce
LFLAGS= -L$(QTDIR)/lib -L$(KDEDIR)/lib -L/usr/X11R6/lib
LIBS= -lkdecore -lkdeui -lqt -lX11 -lXext -ldl
CC=g++

hellokde: hellokde.o
$(CC) $(LFLAGS) -o hellokde hellokde.o $(LIBS)

hellokde.o: hellokde.cpp

clean:
rm -f hellokde
rm -f hellokde.o

.SUFFIXES: .cpp

.cpp.o:
$(CC) -c $(CFLAGS) $(INCL) -o $@ $<

The LIBS definition shows the inclusion of the libraries libkdecore.a, which con-
tains the core functionality of KDE; and libkdeui.a, which contains all of the KDE
widgets. KDE internally implements ODBC (Open Database Connectivity) by dynam-
ically loading ODBC drivers, so it is also necessary to include the library libdl.a.
The installation of KDE places these libraries in the default directory, so there is no
need to add a new search path to LFLAGS.

A Simple Window Class


The following example demonstrates the basic form used to create a widget of your
own. This program creates a MyLabel widget and displays it in the main window,
as shown in Figure 2-3. The MyLabel widget is quite simple — it inherits everything
from QLabel and doesn’t add any capabilities. The class definition is in the header
file mylabel.h.

1 /* mylabel.h */
2 #ifndef MYLABEL_H
4682-1 ch02.f.qc 11/13/00 14:09 Page 17

Chapter 2 ✦ Creating and Displaying a Window 17

3 #define MYLABEL_H
4
5 #include <qlabel.h>
6 #include <qstring.h>
7
8 class MyLabel: public QLabel
9 {
10 public:
11 MyLabel(QWidget *parent);
12 ~MyLabel();
13 };
14
15 #endif

Figure 2-3: Creating and displaying a widget

The preprocessor commands on lines 2, 3, and 15 are not required, but they are a
very good idea. As a growing application begins to get more complicated, the same
header file is likely to be included more than once in a single source file because it
is common to include header files inside other header files. By creating the defini-
tion of MYLABEL_H, this header can be included any number of times, but will be
compiled only once.

On line 8, the definition of the MyLabel class uses QLabel as its base class. This
necessitates the use of the include statement on line 5 to make the definition of
QLabel available. The header file qstring.h is included on line 6 as a convenience,
because the definition of QString is needed in the MyLabel constructor.

The MyLabel class is implemented in its own source file. It doesn’t do anything
other than pass the address of the parent widget from the constructor of MyLabel
to the constructor of the QLabel base class:

1 /* mylabel.cpp */
2 #include “mylabel.h”
3
4 MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
5 {
6 }
7 MyLabel::~MyLabel()
8 {
9 }
4682-1 ch02.f.qc 11/13/00 14:09 Page 18

18 Part I ✦ Getting Started

The following example creates and displays a MyLabel widget. Except for the object
used, the main() function of this program is very much the same as the one for the
previous example.

1 /* helloobject.cpp */
2 #include “mylabel.h”
3 #include <qapplication.h>
4
5 int main(int argc,char **argv)
6 {
7 QApplication app(argc,argv);
8 MyLabel *mylabel = new MyLabel(NULL);
9 QString string(“Hello, object”);
10 mylabel->setText(string);
11 mylabel->setAlignment(
12 Qt::AlignVCenter | Qt::AlignHCenter);
13 mylabel->setGeometry(0,0,180,75);
14 mylabel->show();
15 app.setMainWidget(mylabel);
16 return(app.exec());
17 }

The MyLabel object is created, manipulated, and displayed in exactly the same
way as the QLabel object in the previous example. The setAlignment() function
is inherited directly from QLabel, while setGeometry() and show() are inherited
from QWidget.

Note Object-oriented programming has numerous advantages, but the most well known
are probably the advantages found in handling graphical user interfaces (GUIs). Qt
takes full advantage of this fact. Every displayable object inherits its basic capabilities
from the base class QWidget, which means that every displayable window — label,
button, top-level window, or whatever — all have the same set of basic functions that
control things such as size, color, cursor appearance, mouse detection, and scrolling.
This not only makes it easy to create your own widgets, it automatically applies a
default uniform behavior and appearance to everything on the screen.

The makefile is very much like the previous one, except that it must take into account
the two separate .cpp files by compiling them both and linking them together:

INCL= -I$(QTDIR)/include -I$(KDEDIR)/include


CFLAGS= -pipe -O2 -fno-strength-reduce
LFLAGS= -L$(QTDIR)/lib -L$(KDEDIR)/lib -L/usr/X11R6/lib
LIBS= -lqt -lX11 -lXext
CC=g++

helloobject: helloobject.o mylabel.o


$(CC) $(LFLAGS) -o helloobject helloobject.o \
mylabel.o $(LIBS)

helloobject.o: helloobject.cpp mylabel.h

mylabel.o: mylabel.cpp mylabel.h


4682-1 ch02.f.qc 11/13/00 14:09 Page 19

Chapter 2 ✦ Creating and Displaying a Window 19

clean:
rm -f helloobject
rm -f mylabel.o
rm -f helloobject.o

.SUFFIXES: .cpp

.cpp.o:
$(CC) -c $(CFLAGS) $(INCL) -o $@ $<

Compound Widgets
The QApplication object displays, as the main window of the application, the wid-
get you assign to it with the call to the method assignMainWidget(). To display a
main window that contains more than a single item, you need to create your own
widget and use it as the one displayed as the main window. The following example
combines two buttons and a label into a single widget:

1 /* threewidget.h */
2 #ifndef THREEWIDGET_H
3 #define THREEWIDGET_H
4
5 #include <qpushbutton.h>
6 #include <qlabel.h>
7
8 class ThreeWidget: public QWidget
9 {
10 public:
11 ThreeWidget(QWidget *parent=0,const char *name=0);
12 private:
13 QPushButton *topButton;
14 QPushButton *bottomButton;
15 QLabel *label;
16 };
17
18 #endif

Lines 5 and 6 include the header files defining the widgets that are to be included as
part of the compound widget. There are to be two buttons and a label, and the loca-
tions to store their addresses are defined as private data on lines 13 through 15 of
threewidget.h.

1 /* threewidget.cpp */
2 #include “threewidget.h”
3
4 ThreeWidget::ThreeWidget(QWidget *parent,const char *name):
5 QWidget(parent,name )
6 {
7 setMinimumSize(120,180);
8 setMaximumSize(120,180);
9
4682-1 ch02.f.qc 11/13/00 14:09 Page 20

20 Part I ✦ Getting Started

10 topButton = new QPushButton(“Top Button”,this);


11 topButton->setGeometry(15,15,90,40);
12 label = new QLabel(“Middle Label”,this);
13 label->setGeometry(15,70,90,40);
14 label->setAlignment(AlignVCenter | AlignHCenter);
15 bottomButton = new QPushButton(“Bottom Button”,this);
16 bottomButton->setGeometry(15,125,90,40);
17 }

Because it needs to be a displayable widget, the ThreeWidget class uses QWidget


as its base class.

Lines 7 and 8 set the minimum and maximum sizes of this widget. The parent win-
dow will query this widget to determine its size. In this example, the minimum and
maximum settings are the same, which means that the window cannot be resized.
The displayed window, shown in Figure 2-4, cannot have its width or height
changed with the mouse.

Figure 2-4: Positioning and sizing buttons and labels

The button at the top is created on line 10. The second argument to the constructor
is the widget that is to be the parent of the button. In this example, the parent is the
new widget being constructed. The same parent/child relationship is established
for the label and the other button on lines 12 and 15.

The newly created widget has a displayable area that is 120 pixels wide and 180 pix-
els high. The widgets are positioned on this window by calls to setGeometry(). On
line 11, a call to setGeometry() positions the top button 15 pixels from the top and
15 pixels from the left side. The same call sets the button width to 80 pixels, and the
height to 40 pixels. Similarly, the calls to setGeometry() on lines 13 and 16 position
the other widgets. The first two arguments to setGeometry() are left and top; the
second two are width and height.

The main() function of this program treats the new compound widget just as it
would any other widget.

1 /* compound.cpp */
2 #include <qapplication.h>
4682-1 ch02.f.qc 11/13/00 14:09 Page 21

Chapter 2 ✦ Creating and Displaying a Window 21

3 #include “threewidget.h”
4
5 int main(int argc,char **argv)
6 {
7 QApplication app(argc,argv);
8 ThreeWidget threeWidget;
9 threeWidget.setGeometry(10,10,100,100);
10 app.setMainWidget(&threeWidget);
11 threeWidget.show();
12 return(app.exec());
13 }

The threeWidget object is created on line 8. A widget cannot be forced to fit a size
that is not valid for it, so the call to setGeometry() on line 9 has no effect because
of the minimum and maximum size settings in the widget. The call to show() on
line 11 instructs the widget, and all of the widgets it contains, to be visible.

Listening to a Button
A button is a widget, so it can be displayed just like any other widget. However, your
program will need to know when the user clicks on the button. The following example
displays the window shown in Figure 2-5 and responds to the button by halting:

1 /* exitbutton.cpp */
2 #include <qapplication.h>
3 #include <qpushbutton.h>
4 #include <qstring.h>
5
6 int main(int argc,char **argv)
7 {
8 QApplication app(argc,argv);
9 QString string(“Exit”);
10 QPushButton *button = new QPushButton(string,NULL);
11 QObject::connect(button,
12 SIGNAL(clicked()),&app,SLOT(quit()));
13 button->setGeometry(0,0,80,50);
14 button->show();
15 app.setMainWidget(button);
16 return(app.exec());
17 }

Figure 2-5: A button to exit the program


4682-1 ch02.f.qc 11/13/00 14:09 Page 22

22 Part I ✦ Getting Started

The button is prepared to respond to the mouse, but the response will go unnoticed
unless the button is instructed to send a message to some part of your program. A
message of this type is called a signal, and a method capable of receiving a signal is
called a slot. The call to QObject::connect() on lines 10 and 11 causes a copy of
the signal to be directed from clicked() in the button to the quit() method in
the application.

Note If you have worked with other event-driven systems, you are probably familiar with
the concept of callback functions. A slot is similar to a callback, but there are some
differences. The most important difference is that slots are type safe — if the argu-
ment types don’t match, the program won’t compile.

In the call to QObject::connect(), the first two arguments specify the source
of the signal as being the method named clicked() in the button. The signal is
broadcast. That is, the signal is sent whether or not there are slot functions set
to receive it. On the other hand, if several slots are set to receive the signal, they
each will receive a copy.

The second pair of arguments on the call to QObject::connect() specify that the
receiving slot is to be the quit() method in the QApplication.

Defining a Slot for a Signal


In order to have a widget receive a signal, it must define a slot and connect it to the
signal. The following example, shown in Figure 2-6, displays a button and a counter,
and whenever the button is pressed, the counter is incremented. Several things need
to be done to make this happen, but the Qt system handles most of the details. In
particular, there are some special macros and the Meta Object Compiler (MOC) to
handle most of the detail work automatically. The main() function in this example
simply creates and displays the widget:

1 /* count.cpp */
2 #include <qapplication.h>
3 #include “clickcount.h”
4
5 int main(int argc,char **argv)
6 {
7 QApplication app(argc,argv);
8 ClickCount clickcount;
9 app.setMainWidget(&clickcount);
10 clickcount.show();
11 return(app.exec());
12 }
4682-1 ch02.f.qc 11/13/00 14:09 Page 23

Chapter 2 ✦ Creating and Displaying a Window 23

Figure 2-6: The slot of the counter receives the signal of the button.

The ClickCount widget contains a button and a label. The label is used to display
the current counter value:

1 /* clickcount.h */
2 #ifndef CLICKCOUNT_H
3 #define CLICKCOUNT_H
4
5 #include <qpushbutton.h>
6 #include <qlabel.h>
7
8 class ClickCount: public QWidget
9 {
10 Q_OBJECT
11 public:
12 ClickCount(QWidget *parent=0,const char *name=0);
13 public slots:
14 void incrementCounter();
15 private:
16 int counter;
17 QLabel *label;
18 QPushButton *button;
19 };
20
21 #endif

The macro Q_OBJECT on line 10 must be present in any class that has a slot. (It also
must be present for a class that broadcasts a signal, as you’ll see in Chapter 5.) The
Q_OBJECT macro defines some of the standard methods that must be present in
order for signals and slots to work.

The method, named incrementCounter() on line 14, is categorized as a public


slot by the declaration on line 13. Other than being declared as a slot, increment
Counter() is the same as any other method in the class; and it can be called
directly as well as being called by a signal.

The constructor of the ClickCount class creates the layout containing the button
and the label, and makes the connection that will send a signal from the button to
the slot named incrementCounter():

1 /* clickcount.cpp */
2 #include <stdio.h>
4682-1 ch02.f.qc 11/13/00 14:09 Page 24

24 Part I ✦ Getting Started

3 #include “clickcount.h”
4
5 ClickCount::ClickCount(QWidget *parent,const char *name):
6 QWidget(parent,name )
7 {
8 setMinimumSize(120,125);
9 setMaximumSize(120,125);
10
11 counter = 0;
12 button = new QPushButton(“Add 1”,this);
13 button->setGeometry(15,15,90,40);
14 label = new QLabel(“0”,this);
15 label->setGeometry(15,70,90,40);
16 label->setAlignment(AlignVCenter | AlignHCenter);
17
18 QObject::connect(
19 button,SIGNAL(clicked()),
20 this,SLOT(incrementCounter()));
21 }
22 void ClickCount::incrementCounter()
23 {
24 char str[30];
25 sprintf(str,”%d”,++counter);
26 label->setText(str);
27 }

The calls to setMinimumSize() and setMaximumSize() on lines 8 and 9 fix the


size of the window at 120 × 125 pixels. The counter value is initialized on line 11,
and the button and the label are created and configured on lines 12 through 16.

The call to QObject::connect() on line 18 attaches a slot to a signal. The first two
arguments, on line 19, specify that the source of the signal is to be a method named
clicked(). The clicked() signal is a member of the button’s class, along with the
signals named pressed(), released(), and toggled().

Cross- You can find examples of creating signal methods in Chapter 5.


Reference

The second pair of QObject::connect() arguments, on line 20, specify the object
and method that are to receive the signal. The object is this (the current instance
of ClickCount) and the method is incrementCounter().

Every time the clicked() signal is sent by the button, it is received by the
incrementCounter() method, which adds 1 to the value being displayed and
updates the text of the button.
4682-1 ch02.f.qc 11/13/00 14:09 Page 25

Chapter 2 ✦ Creating and Displaying a Window 25

Note There is no real connection between the signal and the slot. A signal is broadcast
whether or not any slots are listening — and there can be any number of slots lis-
tening for the signal. Also, a slot can be set to listen for any number of signals.

The makefile for this example shows how the Meta Object Compiler) takes its input
as the source code of the header file defining the class, and produces a new source
file to be compiled and linked with the program:

1 INCL= -I$(QTDIR)/include -I$(KDEDIR)/include


2 CFLAGS= -O2 -fno-strength-reduce
3 LFLAGS= -L$(QTDIR)/lib -L$(KDEDIR)/include -L/usr/X11R6/lib
4 LIBS= -lqt -lX11 -lXext
5 CC=g++
6
7 count: count.o clickcount.o moc_clickcount.o
8 $(CC) $(LFLAGS) -o count count.o clickcount.o \
9 moc_clickcount.o $(LIBS)
10
11 count.o: count.cpp clickcount.h
12 clickcount.o: clickcount.cpp clickcount.h
13 moc_clickcount.cpp: clickcount.h
14 $(QTDIR)/bin/moc clickcount.h -o moc_clickcount.cpp
15
16 clean:
17 rm -f count
18 rm -f count.o
19 rm -f clickcount.o
20 rm -f moc_*
21
22 .SUFFIXES: .cpp
23
24 .cpp.o:
25 $(CC) -c $(CFLAGS) $(INCL) -o $@ $<

The dependencies on line 7 show that the program is not only dependent on count.o
and clickount.o, but also depends on something called moc_clickcount.o. The
file moc_clickcount.cpp is created by the MOC compiler from clickcount.h.
The Q_OBJECT macro adds some method prototypes to the ClickCount class defini-
tion, and the MOC compiler generates bodies for the new methods. The result is that
the repetitive (and thus error-prone) coding required for signals and slots is almost
completely automated.

Cross- There is more about how and why this is done — Chapter 5 examines the entire
Reference
process.
4682-1 ch02.f.qc 11/13/00 14:09 Page 26

26 Part I ✦ Getting Started

Summary
It takes very few lines of code to create a working Qt or KDE application. The details
of writing code to create the windows, and the low-level mechanics of listening for
user input, are all handled inside the API.

✦ A Qt program is created by having the mainline of the program create a


QApplication object and use it to control the windows. A KDE program
is formed the same way, using a KApplication object.
✦ The main window of an application is a single widget. This widget normally
contains a collection of other widgets that display information and supply
the user interface.
✦ An object can be written to broadcast one or more signals. An object can also
contain one or more slots designed to receive broadcast signals. The implemen-
tation details of slots and signals are automated through the use of macros and
the MOC compiler.

This chapter described how to create and display the main window of an application.
The next chapter deals with displaying pop-up windows and dialog boxes. There are
some pre-defined KDE and Qt dialog boxes, but you can also create your own.

✦ ✦ ✦
4682-1 ch03.f.qc 11/20/00 15:41 Page 27

Laying Out
Widgets in a
3
C H A P T E R

✦ ✦ ✦ ✦

Window In This Chapter

Positioning widgets
on the display using
specific x and y

T
coordinate values
his chapter is all about controlling the size and position
of a collection of widgets inside a window. Before an Attaching widgets
application can be programmed to respond to a button or to a grid coordinate
read some text typed by the user, it is necessary to present system
the button and the text entry widgets in some sort of reason-
able arrangement. This chapter explains not only how to Stretching and
place a widget where you want it, but also how to specify its shrinking widgets so
size and the action that will be taken when the window is they fit the display
resized.
Stacking widgets
Widgets can be positioned and sized by coordinate values or into a vertical box
by using a layout. The coordinate values are hard-coded pixel for display
locations that cannot be adjusted by the user. On the other
hand, a layout object positions and sizes the widgets (within Inserting widgets
maximum and minimum limits) relative to one another and into a horizontal
relative to the overall size of the containing window. box for display

Containing the
window of one
Geometric Widget Placement widget within the
window of another
You can specify the exact placement and size of each widget
by specifying four values: horizontal offset, vertical offset,
width, and height. The coordinate system is that of the parent ✦ ✦ ✦ ✦
window — that is, the window that contains the widgets. The
following example places three pushbuttons on an application
window.

Main
1 /* main.cpp */
2 #include <kapp.h>
4682-1 ch03.f.qc 11/20/00 15:41 Page 28

28 Part I ✦ Getting Started

3 #include “setxy.h”
4
5 int main(int argc,char **argv)
6 {
7 KApplication app(argc,argv,”setxy”);
8 SetXY setxy;
9 setxy.show();
10 app.setMainWidget(&setxy);
11 return(app.exec());
12 }

The mainline is quite simple. The header file is included on line 3. The widget with
the buttons is created on line 8, and is specified as the widget to be displayed as
the main application window by the call to setMainWidget() on line 10.

SetXY Header
1 /* setxy.h */
2 #ifndef SETXY_H
3 #define SETXY_H
4
5 #include <qpushbutton.h>
6
7 class SetXY: public QWidget
8 {
9 public:
10 SetXY(QWidget *parent=0,const char *name=0);
11 ~SetXY();
12 private:
13 QPushButton *button1;
14 QPushButton *button2;
15 QPushButton *button3;
16 };
17
18 #endif

The class SetXY is defined using QWidget as its base class. Its only data are the
three QPushButton pointers defined on lines 13 through 15. Normally, there would
be methods designated as slots to respond to the buttons, but this is a simple
placement demonstration so there will be no responses to the buttons.

Note Because the header file qpushbutton.h is included on line 5 of setxy.h, it will
be automatically included by both setxy.cpp and main.cpp. If another widget
were being used, and if that other widget also included qpushbutton.h, the
same header file would be included twice. That is why it is very important that the
precompiler directives on lines 2, 3, and 18 be used to prevent a header file from
being compiled more than once.
Other documents randomly have
different content
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the


free distribution of electronic works, by using or distributing this
work (or any other work associated in any way with the phrase
“Project Gutenberg”), you agree to comply with all the terms of
the Full Project Gutenberg™ License available with this file or
online at www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand,
agree to and accept all the terms of this license and intellectual
property (trademark/copyright) agreement. If you do not agree
to abide by all the terms of this agreement, you must cease
using and return or destroy all copies of Project Gutenberg™
electronic works in your possession. If you paid a fee for
obtaining a copy of or access to a Project Gutenberg™
electronic work and you do not agree to be bound by the terms
of this agreement, you may obtain a refund from the person or
entity to whom you paid the fee as set forth in paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only


be used on or associated in any way with an electronic work by
people who agree to be bound by the terms of this agreement.
There are a few things that you can do with most Project
Gutenberg™ electronic works even without complying with the
full terms of this agreement. See paragraph 1.C below. There
are a lot of things you can do with Project Gutenberg™
electronic works if you follow the terms of this agreement and
help preserve free future access to Project Gutenberg™
electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright
law in the United States and you are located in the United
States, we do not claim a right to prevent you from copying,
distributing, performing, displaying or creating derivative works
based on the work as long as all references to Project
Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg™ mission of promoting free
access to electronic works by freely sharing Project Gutenberg™
works in compliance with the terms of this agreement for
keeping the Project Gutenberg™ name associated with the
work. You can easily comply with the terms of this agreement
by keeping this work in the same format with its attached full
Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.

1.E. Unless you have removed all references to Project


Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project
Gutenberg™ work (any work on which the phrase “Project
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and
with almost no restrictions whatsoever. You may copy it,
give it away or re-use it under the terms of the Project
Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of
the copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute


this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in
paragraph 1.F.3, the Project Gutenberg Literary Archive
Foundation, the owner of the Project Gutenberg™ trademark,
and any other party distributing a Project Gutenberg™ electronic
work under this agreement, disclaim all liability to you for
damages, costs and expenses, including legal fees. YOU AGREE
THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE, STRICT
LIABILITY, BREACH OF WARRANTY OR BREACH OF CONTRACT
EXCEPT THOSE PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE
THAT THE FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of
receiving it, you can receive a refund of the money (if any) you
paid for it by sending a written explanation to the person you
received the work from. If you received the work on a physical
medium, you must return the medium with your written
explanation. The person or entity that provided you with the
defective work may elect to provide a replacement copy in lieu
of a refund. If you received the work electronically, the person
or entity providing it to you may choose to give you a second
opportunity to receive the work electronically in lieu of a refund.
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you
do or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status
by the Internal Revenue Service. The Foundation’s EIN or
federal tax identification number is 64-6221541. Contributions
to the Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact
Section 4. Information about Donations to
the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or determine
the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebookball.com

You might also like