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的容器类。

...全文
909 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
玩QT没怎么用过...
zncggaofei 2011-04-08
  • 打赏
  • 举报
回复
QList<Movie > list
jacklondon 2011-04-08
  • 打赏
  • 举报
回复
现在还有人用 QT 啊?我还以为都转 C#/Java 了呢。
zlcp520 2011-04-07
  • 打赏
  • 举报
回复
容存入剪贴板
HONG_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

21,487

社区成员

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

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