Results 1 to 10 of 10

Thread: Promote to Custom Widget makes the widget disappear !

  1. #1
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Promote to Custom Widget makes the widget disappear !

    Hi,
    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 :
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class FileList : public QListWidget {
    4. Q_OBJECT
    5.  
    6. public:
    7. FileList (QWidget *parent = 0);
    8. void dragEnterEvent (QDragEnterEvent *event);
    9. void dropEvent (QDropEvent *event);
    10. };
    To copy to clipboard, switch view to plain text mode 

    I also created filelist.cpp, to implement drag & drop functions :
    Qt Code:
    1. #include "filelist.h"
    2.  
    3. FileList::FileList (QWidget *parent) : QListWidget(parent) {
    4. }
    5.  
    6. void FileList::dragEnterEvent (QDragEnterEvent *event) {
    7. if(event->mimeData()->hasFormat("text/uri-list"))
    8. event->acceptProposedAction();
    9. }
    10.  
    11. void FileList::dropEvent (QDropEvent *event) {
    12. qWarning(event->mimeData()->text().toAscii());
    13. }
    To copy to clipboard, switch view to plain text mode 

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

  2. The following user says thank you to madcat for this useful post:

    MurDoK (29th April 2007)

  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promote to Custom Widget makes the widget disappear !

    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.

  4. The following user says thank you to wysota for this useful post:

    madcat (7th May 2006)

  5. #3
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Promote to Custom Widget makes the widget disappear !

    Hi,
    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

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promote to Custom Widget makes the widget disappear !

    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

  7. The following user says thank you to wysota for this useful post:

    madcat (7th May 2006)

  8. #5
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Promote to Custom Widget makes the widget disappear !

    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 ?

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promote to Custom Widget makes the widget disappear !

    It has a default behaviour:

    Qt Code:
    1. bool QListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
    2. int row, int column, const QModelIndex &index)
    3. {
    4. Q_UNUSED(column);
    5. QListWidget *view = ::qobject_cast<QListWidget*>(QObject::parent());
    6. if (index.isValid()) row = index.row();
    7. else if (row == -1) {
    8. row = lst.count();
    9. }
    10. return view->dropMimeData(row, data, action);
    11. }
    12.  
    13. bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
    14. {
    15. int row = index;
    16. int column = 0;
    17. if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
    18. // QAbstractListModel::dropMimeData will overwrite on the index if row == -1 and column == -1
    19. idx = model()->index(row, column);
    20. row = -1;
    21. column = -1;
    22. }
    23. return d_func()->model()->QAbstractListModel::dropMimeData(data, action , row, column, idx);
    24. }
    To copy to clipboard, switch view to plain text mode 

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

  10. The following user says thank you to wysota for this useful post:

    madcat (7th May 2006)

  11. #7
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Promote to Custom Widget makes the widget disappear !

    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 ...

  12. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promote to Custom Widget makes the widget disappear !

    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.

  13. The following user says thank you to wysota for this useful post:

    madcat (8th May 2006)

  14. #9
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Promote to Custom Widget makes the widget disappear !

    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

  15. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promote to Custom Widget makes the widget disappear !

    Nothing should make that widget dissapear in the preview if the only thing you did was promoting QListWidget to a custom widget.
    Attached Files Attached Files

Similar Threads

  1. QwtPlot - promote to custom widget
    By MrGarbage in forum Qwt
    Replies: 7
    Last Post: 10th February 2011, 11:12
  2. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 15:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.