Qt Container Classes 续(1)

Escene2021 2011-04-05 06:25:13
加精
QList<T>是一个同时拥有QVector<T>和QLinkedList<T>的大多数有点的顺序存储容器类。它像QVector<T>一样支持快速的随机访问,重载了[]操作符,提供了索引访问的方式;它像 QLinkedList<T>一样,支持快速的添加、删除操作。除非我们需要进行在很大的集合的中间位置的添加、删除操作,或者是需要所有元素在内存中必须连续存储,否则我们应该一直使用Qlist<T>。

  QList<T>有几个特殊的情况。一个是QStringList,这是 QList<QString>的子类,提供针对QString的很多特殊操作。QStack<T>和 QQueue<T>分别实现了数据结构中的堆栈和队列,前者具有push(), pop(), top()函数,后者具有enqueue(), dequeue(), head()函数。具体情况请查阅API文档。

  另外需要指出的一点是,我们所说的模板类中的占位符T,可以使基本数据类型,比如int,double等,也可以指针类型,可以是类类型。如果是类类型的话,必须提供默认构造函数,拷贝构造函数和赋值操作符。Qt的内置类中的 QByteArray,QDateTime,QRegExp,QString和QVariant是满足这些条件的。但是,QObject的子类并不符合这些条件,因为它们通常缺少拷贝构造函数和赋值操作符。不过这并不是一个问题,因为我们可以存储QObject的指针,而不是直接存储值。T也可以是一个容器,例如:

QList<QVector<int> > list;

  注意,在最后两个>之间有一个空格,这是为了防止编译器把它解析成>>操作符。这个空格是必不可少的,切记切记!

  下面我们来看一个类:
class Movie 
{ 
public: 
        Movie(const QString &title = "", int duration = 0); 
 
        void setTitle(const QString &title) { myTitle = title; } 
        QString title() const { return myTitle; } 
        void setDuration(int duration) { myDuration = duration; } 
        QString duration() const { return myDuration; } 
 
private: 
        QString myTitle; 
        int myDuration; 
};


  我们能不能把这个类放进Qt容器类呢?答案是肯定的。下面我们来对照着前面所说的要求:第一,虽然这个类的构造函数有两个参数,但是这两个参数都有默认值,因此,像Movie()这种写法是允许的,所以,它有默认构造函数;第二,这个类表面上看上去没有拷贝构造函数和赋值操作符,但是C++编译器会为我们提供一个默认的实现,因此这个条件也是满足的。对于这个类而言,默认拷贝构造函数已经足够,无需我们自己定义。所以,我们可以放心的把这个类放进Qt的容器类。

...全文
892 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
玩QT没怎么用过...
zncggaofei 2011-04-08
  • 打赏
  • 举报
回复
QList<Movie > list
jacklondon 2011-04-08
  • 打赏
  • 举报
回复
现在还有人用 QT 啊?我还以为都转 C#/Java 了呢。
zlcp520 2011-04-07
  • 打赏
  • 举报
回复
容存入剪贴板
火山1009 2011-04-07
  • 打赏
  • 举报
回复
转载请注明出处:::http://devbean.blog.51cto.com/448512/245988
听风牧语 2011-04-07
  • 打赏
  • 举报
回复
先瞧瞧,以后再细读
寒沙胜雪 2011-04-07
  • 打赏
  • 举报
回复
QT。。。。这东西跨平台性能怎么样?
iamlate 2011-04-07
  • 打赏
  • 举报
回复
C++ GUI with QT4第11章的摘要吧,不错。楼主很有耐心
nwpu9283 2011-04-06
  • 打赏
  • 举报
回复
好东西 先收藏
jxliuyunpeng 2011-04-06
  • 打赏
  • 举报
回复
好像不是来问问题的,是来当老师的
A Brief History of Qt Part I: Basic Qt Chapter 1. Getting Started Hello Qt Making Connections Laying Out Widgets Using the Reference Documentation Chapter 2. Creating Dialogs Subclassing QDialog Signals and Slots in Depth Rapid Dialog Design Shape-Changing Dialogs Dynamic Dialogs Built-in Widget and Dialog Classes Chapter 3. Creating Main Windows Subclassing QMainWindow Creating Menus and Toolbars Setting Up the Status Bar Implementing the File Menu Using Dialogs Storing Settings Multiple Documents Splash Screens Chapter 4. Implementing Application Functionality The Central Widget Subclassing QTableWidget Loading and Saving Implementing the Edit Menu Implementing the Other Menus Subclassing QTableWidgetItem Chapter 5. Creating Custom Widgets Customizing Qt Widgets Subclassing QWidget Integrating Custom Widgets with Qt Designer Double Buffering
Part II: Intermediate Qt Chapter 6. Layout Management Laying Out Widgets on a Form Stacked Layouts Splitters Scrolling Areas Dock Windows and Toolbars Multiple Document Interface Chapter 7. Event Processing Reimplementing Event Handlers Installing Event Filters Staying Responsive during Intensive Processing Chapter 8. 2D Graphics Painting with QPainter Coordinate System Transformations High-Quality Rendering with QImage Item-Based Rendering with Graphics View Printing Chapter 9. Drag and Drop Enabling Drag and Drop Supporting Custom Drag Types Clipboard Handling Chapter 10. Item View Classes Using the Item View Convenience Classes Using Predefined Models Implementing Custom Models Implementing Custom Delegates Chapter 11. Container Classes Sequential Containers Associative Containers Generic Algorithms Strings, Byte Arrays, and Variants Chapter 12. Input/Output Reading and Writing Binary Data Reading and Writing Text Traversing Directories Embedding Resources Inter-Process Communication Chapter 13. Databases Connecting and Querying Viewing Tables Editing Records Using Forms Presenting Data in Tabular Forms Chapter 14. Multithreading Creating Threads Synchronizing Threads Communicating with the Main Thread Using Qt's Classes in Secondary Threads Chapter 15. Networking Writing FTP Clients Writing HTTP Clients Writing TCP Client–Server Applications Sending and Receiving UDP Datagrams Chapter 16. XML Reading XML with QXmlStreamReader Reading XML with DOM Reading XML with SAX Writing XML Chapter 17. Providing Online Help Tooltips, Status Tips, and "What's This?" Help Using a Web Browser to Provide Online Help Using QTextBrowser as a Simple Help Engine Using Qt Assistant for Powerful Online Help
Part III: Advanced Qt Chapter 18. Internationalization Working with Unicode Making Applications Translation-Aware Dynamic Language Switching Translating Applications Chapter 19. Look and Feel Customization Using Qt Style Sheets Subclassing QStyle Chapter 20. 3D Graphics Drawing Using OpenGL Combining OpenGL and QPainter Doing Overlays Using Framebuffer Objects Chapter 21. Creating Plugins Extending Qt with Plugins Making Applications Plugin-Aware Writing Application Plugins Chapter 22. Application Scripting Overview of the ECMAScript Language Extending Qt Applications with Scripts Implementing GUI Extensions Using Scripts Automating Tasks through Scripting Chapter 23. Platform-Specific Features Interfacing with Native APIs Using ActiveX on Windows Handling X11 Session Management Chapter 24. Embedded Programming Getting Started with Qt/Embedded Linux Customizing Qt/Embedded Linux Integrating Qt Applications with Qtopia Using Qtopia APIs
Part IV: Appendixes Appendix A. Obtaining and Installing Qt A Note on Licensing Installing Qt/Windows Installing Qt/Mac Installing Qt/X11 Appendix B. Building Qt Applications Using qmake Using Third-Party Build Tools Appendix C. Introduction to Qt Jambi Getting Started with Qt Jambi Using Qt Jambi in the Eclipse IDE Integrating C++ Components with Qt Jambi Appendix D. Introduction to C++ for Java and C# Programmers Getting Started with C++ Main Language Differences The Standard C++ Library About the Authors Jasmin Blanchette Mark Summerfield Production Index
Rapid GUI programming with Python and Qt 1 Contents 8 Foreword 14 Introduction 16 Part I: Python Programming 22 Chapter 1. Data Types and Data Structures 24 Executing Python Code 25 Variables and Objects 27 Numbers and Strings 30 Collections 44 Built-in Functions 52 Summary 56 Exercises 57 Chapter 2. Control Structures 60 Conditional Branching 61 Looping 64 Functions 70 Exception Handling 81 Summary 87 Exercises 87 Chapter 3. Classes and Modules 90 Creating Instances 92 Methods and Special Methods 94 Inheritance and Polymorphism 114 Modules and Multifile Applications 119 Summary 122 Exercises 123 Part II: Basic GUI Programming 124 Chapter 4. Introduction to GUI Programming 126 A Pop-Up Alert in 25 Lines 127 An Expression Evaluator in 30 Lines 131 A Currency Converter in 70 Lines 136 Signals and Slots 142 Summary 151 Exercise 152 Chapter 5. Dialogs 154 Dumb Dialogs 156 Standard Dialogs 162 Smart Dialogs 169 Summary 177 Exercise 178 Chapter 6. Main Windows 180 Creating a Main Window 181 Handling User Actions 205 Summary 216 Exercise 217 Chapter 7. Using Qt Designer 220 Designing User Interfaces 223 Implementing Dialogs 231 Testing Dialogs 236 Summary 238 Exercise 239 Chapter 8. Data Handling and Custom File Formats 242 Main Window Responsibilities 244 Data Container Responsibilities 250 Saving and Loading Binary Files 255 Saving and Loading Text Files 264 Saving and Loading XML Files 271 Summary 280 Exercise 281 Part III: Intermediate GUI Programming 282 Chapter 9. Layouts and Multiple Documents 284 Layout Policies 285 Tab Widgets and Stacked Widgets 287 Splitters 295 Single Document Interface (SDI) 298 Multiple Document Interface (MDI) 305 Summary 315 Exercise 316 Chapter 10. Events, the Clipboard, and Drag and Drop 318 The Event-Handling Mechanism 318 Reimplementing Event Handlers 320 Using the Clipboard 325 Drag and Drop 327 Summary 332 Exercise 333 Chapter 11. Custom Widgets 336 Using Widget Style Sheets 337 Creating Composite Widgets 340 Subclassing Built-in Widgets 341 Subclassing QWidget 343 Summary 360 Exercise 361 Chapter 12. Item-Based Graphics 364 Custom and Interactive Graphics Items 366 Animation and Complex Shapes 383 Summary 393 Exercise 394 Chapter 13. Rich Text and Printing 396 Rich Text Editing 397 Printing Documents 413 Summary 426 Exercise 427 Chapter 14. Model/View Programming 428 Using the Convenience Item Widgets 430 Creating Custom Models 438 Creating Custom Delegates 451 Summary 457 Exercise 458 Chapter 15. Databases 460 Connecting to the Database 461 Executing SQL Queries 461 Using Database Form Views 466 Using Database Table Views 472 Summary 485 Exercise 486 Part IV: Advanced GUI Programming 488 Chapter 16. Advanced Model/View Programming 490 Custom Views 491 Generic Delegates 498 Representing Tabular Data in Trees 507 Summary 520 Exercise 520 Chapter 17. Online Help and Internationalization 524 Online Help 525 Internationalization 527 Summary 534 Exercise 535 Chapter 18. Networking 536 Creating a TCP Client 538 Creating a TCP Server 544 Summary 549 Exercise 549 Chapter 19. Multithreading 552 Creating a Threaded Server 554 Creating and Managing Secondary Threads 559 Implementing a Secondary Thread 567 Summary 572 Exercise 573 This Is Not Quite the End 574 Appendix A. Installing 576 Installing on Windows 576 Installing on Mac OS X 581 Installing on Linux and Unix 585 Appendix B. Selected PyQt Widgets 590 Appendix C. Selected PyQt Class Hierarchies 596 Index 600 A 601 B 602 C 604 D 606 E 608 F 610 G 611 H 611 I 612 J 613 K 613 L 614 M 615 N 616 O 616 P 617 Q 618 R 630 S 631 T 637 U 639 V 639 W 640 X 640 Y 640 Z 640
Python Cookbook英文版 Table of Contents Foreword Preface 1. Python Shortcuts 1.1 Swapping Values Without Using a Temporary Variable 1.2 Constructing a Dictionary Without Excessive Quoting 1.3 Getting a Value from a Dictionary 1.4 Adding an Entry to a Dictionary 1.5 Associating Multiple Values with Each Key in a Dictionary 1.6 Dispatching Using a Dictionary 1.7 Collecting a Bunch of Named Items 1.8 Finding the Intersection of Two Dictionaries 1.9 Assigning and Testing with One Statement 1.10 Using List Comprehensions Instead of map and filter 1.11 Unzipping Simple List-Like Objects 1.12 Flattening a Nested Sequence 1.13 Looping in Parallel over Index and Sequence Items 1.14 Looping Through Multiple Lists 1.15 Spanning a Range Defined by Floats 1.16 Transposing Two-Dimensional Arrays 1.17 Creating Lists of Lists Without Sharing References 2. Searching and Sorting 2.1 Sorting a Dictionary 2.2 Processing Selected Pairs of Structured Data Efficiently 2.3 Sorting While Guaranteeing Sort Stability 2.4 Sorting by One Field, Then by Another 2.5 Looking for Items in a Sorted Sequence Using Binary Search 2.6 Sorting a List of Objects by an Attribute of the Objects 2.7 Sorting by Item or by Attribute 2.8 Selecting Random Elements from a List Without Repetition 2.9 Performing Frequent Membership Tests on a Sequence 2.10 Finding the Deep Index of an Item in an Embedded Sequence 2.11 Showing Off Quicksort in Three Lines 2.12 Sorting Objects Using SQL's ORDER BY Syntax 3. Text 3.1 Processing a String One Character at a Time 3.2 Testing if an Object Is String-Like 3.3 Aligning Strings 3.4 Trimming Space from the Ends of a String 3.5 Combining Strings 3.6 Checking Whe

16,214

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧