View Full Version : Promote to Custom Widget makes the widget disappear !

7th May 2006, 17:14
I want to be able to do drag & drop on a QListWidget, so I chose to subclass it. To do so, in Designer I selected it and clicked "Promote to custom widget", I named the new class FileList. I also created filelist.h, which I post there :

#include <QtGui>

class FileList : public QListWidget {

FileList (QWidget *parent = 0);
void dragEnterEvent (QDragEnterEvent *event);
void dropEvent (QDropEvent *event);

I also created filelist.cpp, to implement drag & drop functions :

#include "filelist.h"

FileList::FileList (QWidget *parent) : QListWidget(parent) {

void FileList::dragEnterEvent (QDragEnterEvent *event) {

void FileList::dropEvent (QDropEvent *event) {

The problem is, in Designer preview, or in the final binary, the list does not appear anymore... Any clue ?

7th May 2006, 17:33
You don't have to subclass QListWidget to use drag&drop.

About Designer: The preview should be fine, as the widget should be shown as QListWidget. You must have messed something up, but it's hard to say what without the ui file.

7th May 2006, 17:49
I see a list in Designer, but when I do a Ctrl+R, the list disappears.
I subclassed because gcc complained when I tried to redefine QListWidget::dragEnterEvent or QListWidget::dropEvent, saying "no member function declared in class"... The function is marked as "virtual protected".
Do you have any pointer on how to do Drag & Drop with QListWidget ?
Thanks for your answer anyway :)

7th May 2006, 18:03
You don't need to alter those methods. You just need to tell the widget to accept drops and then use QListWidgetItem::setFlags() to return Qt::ItemIsDragEnabled and maybe also Qt::ItemIsDropEnabled (depending on the effect you want to achieve). QListWidget should handle the rest. The only thing I couldn't find is a way to select which mime-types should be accepted. It is possible that I didn't look search long enough :)

7th May 2006, 18:09
I don't see your point (please forget a newbie :) ), if my list is empty, I won't have any QListWidgetItem inside... I want to drop file from another app (ex Nautilus, Konqueror, Finder, Explorer...) onto the list. How will the list know what I want to do with my files ?

7th May 2006, 18:25
It has a default behaviour:

bool QListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &index)
QListWidget *view = ::qobject_cast<QListWidget*>(QObject::parent());
if (index.isValid()) row = index.row();
else if (row == -1) {
row = lst.count();
return view->dropMimeData(row, data, action);

bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
QModelIndex idx;
int row = index;
int column = 0;
if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
// QAbstractListModel::dropMimeData will overwrite on the index if row == -1 and column == -1
idx = model()->index(row, column);
row = -1;
column = -1;
return d_func()->model()->QAbstractListModel::dropMimeData(data, action , row, column, idx);

In any case, even if you do subclass QListWidget, you should alter its dropMimeData method and not events.

7th May 2006, 18:35
the fact is, my QListWidget has acceptDrops, showDropIndicator & dragEnabled set to true, but I can't drop a file on the list, from nautilus or konqueror... I must be missing something :confused: ...

8th May 2006, 09:22
It is possible that QListWidget doesn't have a method to set the types which should be accepted as drops. Try subclassing and reimplementing mimeTypes() method. If tells the widget, what types can it operate on. Of course you shouldn't need to subclass, but I can't find a method to set those types. It's probably a bug and should be reported to Trolltech.

8th May 2006, 10:53
Subclassing was my original idea, but that made the widget disappear... I can't believe there is no simple way to drop items on a QListWidget :) . As you see, I did not redefine mimeTypes() when I subclassed, but I don't think this could make the widget disappear... I'll test that as soon as possible.
Thanks for all your answers

8th May 2006, 14:21
Nothing should make that widget dissapear in the preview if the only thing you did was promoting QListWidget to a custom widget.