Learning Qt 5! (14):拖放

Qt 学习之路 2(52):使用拖放:使用系统提供的拖放对象QMimeData进行拖放数据的存储。
代码的例子来自 C++ GUI Programming with Qt4, 2nd Edition,有空应学习一下此书。

QMimeData的用法:比如使用QMimeData::setText()创建文本,使用QMimeData::urls()创建 URL 对象等。

Qt 学习之路 2(53):自定义拖放数据(文中的第二个例子待完成)
但是,若希望使用一些自定义的对象作为拖放数据,比如自定义类等等,单纯使用QMimeData可能就没有那么容易了。为了实现这种操作,可以从下面三种实现方式中选择一个:

1.将自定义数据作为QByteArray对象,使用QMimeData::setData()函数作为二进制数据存储到QMimeData中,然后使用QMimeData::data()读取。
2.继承QMimeData,重写其中的formats()和retrieveData()函数操作自定义数据。
3.如果拖放操作仅仅发生在同一个应用程序,可以直接继承QMimeData,然后使用任意合适的数据结构进行存储。

这三种选择各有千秋:第一种方法不需要继承任何类,但是有一些局限:即是拖放不会发生,我们也必须将自定义的数据对象转换成QByteArray对象,在一定程度上,这会降低程序性能;另外,如果你希望支持很多种拖放的数据,那么每种类型的数据都必须使用一个QMimeData类,这可能会导致类爆炸。后两种实现方式则不会有这些问题,或者说是能够减小这种问题,并且能够让我们有完全的控制权。

最后是用Qt实现剪贴板的功能。
Qt 学习之路 2(54):剪贴板
文中例子的set clipboard功能似乎没有实现?

Q&A:
1.on_rightButton_clicked 没有看到connect 任何 signal,它是如何被QToolButton触发的?

on_rightButton_clicked() 这种函数是由 ui 文件生成的槽,Qt 会根据函数名的规律自动连接。这是信号槽的自动连接方式。不过,注意这种自动连接只能通过 ui 文件的方式实现,也就是说,如果你自己定义一个类似命名的函数,Qt 还是不会自动连接的。

2.代码中event->buttons() & Qt::LeftButton 和event->buttons() == Qt::LeftButton完全是等价的,平常也是基本采用后一种写法,文中这种写法是有什么优越性吗?

event->buttons() & Qt::LeftButton 和 event->buttons() == Qt::LeftButton 在这里的效果一致,但这两个语句完全不等价(效果一致仅仅是一个特例)。event->buttons() 的返回值是 Qt::MouseButtons,这是一个 flag 类型,可以使用 | 进行按位或的操作。也就是说,event->buttons() 可能返回类似 Qt::LeftButton | Qt::RightButton (鼠标左右键同时按下),因此,要判断“正确的”左键是否按下,只能使用按位与运算。如果平时使用的是后面的写法,有可能存在一定的隐患(当然,后者确定只按下左键,不考虑多建一起按下的情况,也许是更常见的判断)。

3.私有的信号槽,就是发送信号和接收信号也是在本对象,这样是不是函数调用更好呢?

如果的确是这样,可以使用函数调用,并且函数调用性能更好的。

4.对selectionText()看不太懂,能不能讲一下。

selectionText() 函数为了将选区的数据转换成字符串。函数中 model() 获得数据模型;selectedRanges().at(0) 获得第一个选区。由于我们的选区是二维的,所以使用两个 for 循环进行遍历。

5.在void DataTableWidget::dropEvent(QDropEvent *event)这段代码中,有两个问题,希望豆子老师解答下:
(1)T qobject_cast(QObject * object)转换后值不为0只能说明object是T的类型或者是T的父类,如何能保证数据来自同一应用程序,为什么不用前一篇中的source != this来判断呢?
(2)在代码最后为什么调用QTableWidget::mouseMoveEvent函数?

(1)如果需要保证数据来自同一应用程序,可以使用 source != this 的判断,因为这里只是演示程序,因此并没有作详尽的判断。
(2)调用父类的实现主要为了保证在两个判断分支都失败的情况下,move 事情仍然能够正确相应。如果没有影响的话,应该也可以选择将这个语句移到 else 里面。

About This Book, Unleash the power of Qt 5 with C++14Integrate useful third-party libraries such as OpenCVPackage and deploy your application on multiple platforms, Who This Book Is For, This book will appeal to developers and programmers who would like to build GUI-based applications. Knowledge of C++ is necessary and the basics of Qt would be helpful., What You Will Learn, Create stunning UIs with Qt Widget and Qt QuickDevelop powerful, cross-platform applications with the Qt frameworkDesign GUIs with the Qt Designer and build a library in it for UI previewHandle user interaction with the Qt signal/slot mechanism in C++Prepare a cross-platform project to host a third-party libraryBuild a Qt application using the OpenCV APIUse the Qt Animation framework to display stunning effectsDeploy mobile apps with Qt and embedded platforms, In Detail, Qt 5.7 is an application development framework that provides a great user experience and develops full-capability applications with Qt Widgets, QML, and even Qt 3D., This book will address challenges in successfully developing cross-platform applications with the Qt framework. Cross-platform development needs a well-organized project. Using this book, you will have a better understanding of the Qt framework and the tools to resolve serious issues such as linking, debugging, and multithreading. Your journey will start with the new Qt 5 features. Then you will explore different platforms and learn to tame them. Every chapter along the way is a logical step that you must take to master Qt. The journey will end in an application that has been tested and is ready to be shipped., Style and approach, This is an easy-to-follow yet comprehensive guide to building applications in Qt. Each chapter covers increasingly advanced topics, with subjects grouped according to their complexity as well as their usefulness. Packed with practical examples and explanations, Mastering Qt contains everything you need to take your applications to the next level.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值