Results 1 to 2 of 2

Thread: Custom widget for QtCreator, QLabel problem

  1. #1
    Join Date
    Aug 2013
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Question Custom widget for QtCreator, QLabel problem

    Hi,
    I'm trying to implement a drag-and-drop feature in my application, so I created couple widgets as QtDesginer plugins, co i can put those visually in QtCreator.
    First widget is an Icon widget which should be draggable and second widget is a container from which icons should be dragged over to another widget.

    Icon widget is created as QWidget with vertical layout and two QLabel objects - one for QPixmap icon and second for displaying name.

    here is the code for the icon widget :

    Qt Code:
    1. #ifndef DRAGGABLEICONWIDGET_H
    2. #define DRAGGABLEICONWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QFrame>
    6. #include <QLabel>
    7. #include <QMetaProperty>
    8. #include <QHBoxLayout>
    9. #include <QVBoxLayout>
    10. #include <QPixmap>
    11.  
    12. class DraggableIconWidget : public QWidget
    13. {
    14. Q_OBJECT
    15. Q_PROPERTY(QString group READ group WRITE setGroup)
    16. Q_PROPERTY(QString name READ name WRITE setName)
    17. Q_PROPERTY(QString displayName READ displayName WRITE setDisplayName)
    18. Q_PROPERTY(QPixmap icon READ icon WRITE setIcon)
    19. public:
    20. DraggableIconWidget(QWidget *parent = 0);
    21.  
    22. QString group();
    23. void setGroup(QString);
    24.  
    25. QString name();
    26. void setName(QString);
    27.  
    28. QString displayName();
    29. void setDisplayName(QString);
    30.  
    31. QPixmap icon();
    32. void setIcon(QPixmap);
    33. private:
    34. QString group_;
    35. QString name_;
    36. QString displayName_;
    37. QPixmap icon_;
    38.  
    39. QLabel *iconLabel_;
    40. QLabel *nameLabel_;
    41. QVBoxLayout *layout_;
    42. };
    43.  
    44. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "draggableiconwidget.h"
    2.  
    3. DraggableIconWidget::DraggableIconWidget(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. iconLabel_ = new QLabel();
    7. iconLabel_->setAlignment(Qt::AlignCenter);
    8. iconLabel_->setText("");
    9.  
    10. nameLabel_ = new QLabel();
    11. nameLabel_->setAlignment(Qt::AlignCenter);
    12. nameLabel_->setText("name");
    13.  
    14. layout_ = new QVBoxLayout(this);
    15. layout_->addWidget(iconLabel_);
    16. layout_->addWidget(nameLabel_);
    17.  
    18. this->setLayout(layout_);
    19. }
    20.  
    21. QString DraggableIconWidget::group()
    22. {
    23. return this->group_;
    24. }
    25. void DraggableIconWidget::setGroup(QString s)
    26. {
    27. this->group_ = s;
    28. }
    29. QString DraggableIconWidget::name()
    30. {
    31. return this->name_;
    32. }
    33. void DraggableIconWidget::setName(QString s)
    34. {
    35. this->name_ = s;
    36. }
    37. QString DraggableIconWidget::displayName()
    38. {
    39. return this->displayName_;
    40. }
    41. void DraggableIconWidget::setDisplayName(QString s)
    42. {
    43. this->displayName_ = s;
    44. this->nameLabel_->setText(this->displayName_);
    45. }
    46. QPixmap DraggableIconWidget::icon()
    47. {
    48. //this works fine in creator
    49. //return this->icon_;
    50. if(!this->iconLabel_->pixmap())
    51. return QPixmap();
    52. return *this->iconLabel_->pixmap();
    53. }
    54. void DraggableIconWidget::setIcon(QPixmap p)
    55. {
    56. //this->icon_ = p;
    57. this->iconLabel_->setPixmap(p);
    58. }
    To copy to clipboard, switch view to plain text mode 

    and here is the code for dragging function in the second custom widget:

    Qt Code:
    1. void DragWidget::mousePressEvent(QMouseEvent *e)
    2. {
    3. DraggableIconWidget *widget = static_cast<DraggableIconWidget*>(childAt(e->pos()));
    4. if(!widget)
    5. return;
    6.  
    7. QPixmap pixmap = widget->icon(); //it crash here
    8. QString data = widget->group() + ":" + widget->name();
    9.  
    10. QMimeData *mimeData = new QMimeData();
    11. mimeData->setText(data);
    12.  
    13. QDrag *drag = new QDrag(this);
    14. drag->setMimeData(mimeData);
    15. drag->setPixmap(pixmap);
    16. drag->setHotSpot(e->pos() - widget->pos());
    17.  
    18. QPixmap tempPixmap = *widget->iconLabel()->pixmap();
    19. QPainter painter;
    20. painter.begin(&tempPixmap);
    21. painter.fillRect(pixmap.rect(),QColor(127,127,127,127));
    22. painter.end();
    23.  
    24. if(drag->exec(Qt::CopyAction | Qt::MoveAction,Qt::CopyAction) == Qt::MoveAction)
    25. {
    26. widget->close();
    27. }
    28. else {
    29. widget->show();
    30. widget->setIcon(pixmap);
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    the problem is with getting the pixmap back from QLabel object. if the icon() function which return the pixmap, would be without the if statment the QtCreator is crashing everytime I try to open *.ui file and the same thing is when i'm trying to get the pixmap from item in my custom frame widget for dragging.

    It works fine in QtCreator (loads .ui files and i can change the object icon) when i'm using second variable QPixmap icon_ in the class, and in the icon() function i return the this->icon_ variable, but then it also will crash when I'm trying to obtain pixmap form within the second widget.

    Previously i was using my own widget for icons, but It was based on QLabel object, and then dragging and everything else was fine, but then i can't add second label with text so I must find another way for doing it.

    here is the stack dump:
    Qt Code:
    1. 0 QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> >::data qscopedpointer.h 135 0xe27a2e
    2. 1 qGetPtrHelper<QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> > > qglobal.h 2455 0xa73637
    3. 2 QLabel::d_func qlabel.h 166 0xe5add8
    4. 3 QLabel::pixmap qlabel.cpp 437 0xa6e575
    5. 4 DraggableIconWidget::icon draggableiconwidget.cpp 48 0x401d16
    6. 5 DragWidget::mousePressEvent dragwidget.cpp 14 0x4015bb
    7. 6 QWidget::event qwidget.cpp 8371 0x6d6370
    8. 7 QFrame::event qframe.cpp 557 0xa6ac28
    9. 8 QApplicationPrivate::notify_helper qapplication.cpp 4562 0x68bf46
    10. 9 QApplication::notify qapplication.cpp 4105 0x68a190
    11. 10 QCoreApplication::notifyInternal qcoreapplication.cpp 946 0x69dd5a1a
    12. 11 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 234 0xd2653a
    13. 12 QApplicationPrivate::sendMouseEvent qapplication.cpp 3171 0x6889fc
    14. 13 QETWidget::translateMouseEvent qapplication_win.cpp 3365 0x6f31a9
    15. 14 QtWndProc@16 qapplication_win.cpp 1698 0x6edf9e
    16. 15 USER32!GetDC C:\WINDOWS\system32\user32.dll 0x7e368734
    To copy to clipboard, switch view to plain text mode 

    To compile my custom widgets I used Qt 4.84 with MSVC compiler, and for building my test application i'm using Qt 4.8.4 with MinGw compiler
    Everything was tested in QtCreator 2.8.0

    Any ideas what could be wrong with my code ? Thnx in advance for any clues

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom widget for QtCreator, QLabel problem

    A quick change to aviod crashing.
    Qt Code:
    1. //DraggableIconWidget *widget = static_cast<DraggableIconWidget*>(childAt(e->pos()));
    2. DraggableIconWidget *widget = dynamic_cast<DraggableIconWidget*>(childAt(e->pos()));
    3. if(!widget)
    4. return;
    To copy to clipboard, switch view to plain text mode 
    Also note that static_cast does not set the widget to 0 if the types are uncompatible, dynamic_cast is supposed to be used here.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Problem with a custom slot of a qlabel
    By nomwise in forum Qt Programming
    Replies: 6
    Last Post: 27th February 2012, 13:26
  2. Replies: 1
    Last Post: 26th November 2010, 09:22
  3. Replies: 2
    Last Post: 25th August 2010, 10:25
  4. Replies: 2
    Last Post: 30th July 2009, 05:58
  5. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 12:55

Tags for this Thread

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.