PDA

View Full Version : Problem with events changing behavior of QListWidget



riklaunim
21st December 2008, 23:10
I'm trying to make a basic file manager based on custom QlistWidget showing files and folders like Konqueror/Dolphin (IconMode) using PyQt4 and Python. The problem I have is about using correct events for the tasks I want to code.
- For context menu I use contextMenuEvent and at this point no problem with this

- For dropping files/folders on folders or moving them I use dropEvent – it works for dropping on folders, but there is small painting glitch – the items stay where they are, and also show up (only painted) in the drop place if its a blank space - do I have drag them in code (using Qdrag etc.) to that position? (it works if dropEvent isn't defined)

- For clicking on an item I use mouseReleaseEvent and check if it's the “1” button and there is 1 element selected (so it won't run on selecting elements). The problem is that when I use mouseReleaseEvent the dragging of multiple items doesn't work – I select few items and pressing mouse button on them again doesn't give the drag option, just selects the item under cursor or makes another selection – how can I solve this? (works if mouseReleaseEvent is not defined, using mousePressEvent makes selection rectangle go crazy and select from top-left corner to cursor position)

wysota
22nd December 2008, 00:42
Is there a particular reason why you use such a low level approach instead of using what QListWidget and QListView (along with QDirModel) offer?

riklaunim
22nd December 2008, 01:27
Is there a particular reason why you use such a low level approach instead of using what QListWidget and QListView (along with QDirModel) offer?

There may be some small reasons (I don't know if they can be "moved" to the qdirmodel solution) - like detecting file MIME (and based on this - icon). I didn't used QDirModel and it features yet, so I used QListWidget with QDir. Should I use the model instead of QListWidget/QDir?

wysota
22nd December 2008, 08:04
There may be some small reasons (I don't know if they can be "moved" to the qdirmodel solution) - like detecting file MIME (and based on this - icon).
Yes, that's not a problem. You use QFileIconProvider for that.

Should I use the model instead of QListWidget/QDir?

Yes, I think so. Also bear in mind there are two file-system models - QDirModel and QFileSystemModel, so choose the one that's best for your needs.

And regardless of whether you use model based approach or item based approach reimplementing low level events where appropriate signals are available might not be the best idea.

riklaunim
22nd December 2008, 15:31
I've made a simple implementation base on QDirModel and QListView: http://www.wklej.org/id/30776/
- I have the context menu signal
- I have the activated element signal (open file / goto folder)
- I have flags for dropping elements on folders

I don't have anything to handle drops. From what I've read the "dropMimeData" of QDirModel should handle that but in my example is not run on drops at all. Am I missing something or do I have to use something else?

jpn
22nd December 2008, 16:09
QDirModel is read-only by default.

riklaunim
22nd December 2008, 16:31
QDirModel is read-only by default.

and? ;) I can set setReadOnly(False) but it doesn't change anything with dropMimeData.

jpn
22nd December 2008, 16:47
and? ;) I can set setReadOnly(False) but it doesn't change anything with dropMimeData.
A read-only QDirModel won't accept drops at all. You can check the Extended Dir View example in our wiki. It uses QDirModel with drag'n'drop.

riklaunim
22nd December 2008, 17:37
I've seen it, and I have set parameters responsible for drag & drop:


dirModel->setReadOnly(false);

listView->setDragEnabled(true);
listView->setAcceptDrops(true);

I can drag and select items, I can drop them on folders or empty spaces, but I don't have anything to receive those drops in code.

jpn
22nd December 2008, 18:20
Do you mean that your dropMimeData() implementation doesn't get called at all? Then there must be something wrong because QDirModel surely does the dropping work in dropMimeData().

Btw, your flags() shouldn't be necessary. QDirModel already adds Qt::ItemIsDropEnabled where appropriate:


Qt::ItemFlags QDirModel::flags(const QModelIndex &index) const
{
Q_D(const QDirModel);
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
if (!d->indexValid(index))
return flags;
flags |= Qt::ItemIsDragEnabled;
if (d->readOnly)
return flags;
QDirModelPrivate::QDirNode *node = d->node(index);
if ((index.column() == 0) && node->info.isWritable()) {
flags |= Qt::ItemIsEditable;
if (fileInfo(index).isDir()) // is directory and is editable
flags |= Qt::ItemIsDropEnabled; // <---
}
return flags;
}

riklaunim
22nd December 2008, 19:10
Ok, flags can be skipped when setReadOnly is True... The bastard that blocked dropMimeData is IconMode: self.ui.listView.setViewMode(QtGui.QListView.IconM ode)