Hi All

I'm using the QClipboard class provided by Qt. I'm trying to create a QList that holds the last twenty items that have been copied.

I am declaring the list as follows:

QList<const QMimeData*> *copiedList;

I am creating the list as follows:

copiedList = new QList<const QMimeData*>;

The QClipboard method mimeData returns a const QMimeData* const that contains the copied data. Ideally I want to add this to the QList and then be able to call the QClipboard method setMimeData to update the clipboard data. setMimeData accepts a QMimeData* that is non constant. I am currently unsure about how to best store the const QMimeData* const data and 'convert it' so that it is non constant. The code I have so far is:

if(copiedList->count() > MAXDATACOUNT)
            {
                copiedList->pop_back();
            }

            copiedList->push_front(clipboard->mimeData(modes));

            clipboard->setMimeData(copiedList->at(0), modes);

I receive an error "Convert between const QMimeData* const and const QMimeData*". Any tips of advice how to get around this, or an alternative solution would be great. Thanks in advance.

Cam

Recommended Answers

All 3 Replies

Well, in the expression "const QMimeData* const", the first const means that the QMimeData is not changeable (constant) and the second const means that the pointer to it is not going to change either. If you don't need to change the pointer (i.e. create your own QMimeData object), then make your list QList<const QMimeData* const>.

But, possibly, depending on the implementation QList, you might not be allowed to do that because a const QMimeData* const is not well copyable. So in that case, keep QList<const QMimeData*> and make this line instead of what you had:

copiedList->push_front(const_cast<const QMimeData*>(clipboard->mimeData(modes)));

I pretty sure this will work.

Thanks Mike! I think this is exactly what I'm after. I'm experiencing a segmination fault now which I'm trying to work around, but I think the casting is fine!

If you have any thoughts on this, that would be great:

As the clipboard->mimeData(modes) method returns a const QMimeData* const. If I store this pointer in the list, the next time clipboard->mimeData(modes), would the data at the pointers location be overwritten? If so, I think that is where my problem is.

Thanks again.

Are you sure that the pointer returned by mimeData(modes) cannot be null? If not then you should probably test for NULL before inserting the pointer into the list. Also whenever you store pointers in a container you have to be careful that you do not in some other part of the code delete the object through some other pointer and then later try and use the stored pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.