Results 1 to 4 of 4

Thread: QPainter crash when generating icon after drag-drop

  1. #1
    Join Date
    Nov 2010
    Location
    Beijing, China
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QPainter crash when generating icon after drag-drop

    I have a series of QButton subclasses between which drag-drop operations can be performed. Each button has text and an icon, which is simply a badge with one or two characters (usually a number):

    Qt Code:
    1. QIcon GMScriptCenter::badgeIcon(const QSize &size, const QColor &outlineColor, const QColor &fillColor, const QString &badgeText)
    2. {
    3. QPixmap pixmap(size);
    4. pixmap.fill(Qt::transparent);
    5. QPainter painter(&pixmap);
    6. painter.setRenderHint(QPainter::Antialiasing);
    7.  
    8. painter.setPen(QPen(outlineColor));
    9. int minWH = qMax(qMin(size.width(), size.height()), 1);
    10. QRect rect(QPoint(0, 0), size);
    11. painter.setBrush(QBrush(fillColor)); // commenting either this, or...
    12. painter.drawRoundedRect(rect.adjusted(1, 1, -1, -1), minWH/5, minWH/5); // ...this prevents crash
    13.  
    14. if (!badgeText.isNull()) {
    15. painter.setFont(QFont("Trebuchet MS", size.height()-10));
    16. painter.setPen(QPen(Qt::white));
    17. painter.drawText(rect, badgeText, QTextOption(Qt::AlignCenter));
    18. }
    19.  
    20. return QIcon(pixmap);
    21. }
    To copy to clipboard, switch view to plain text mode 

    Usually, I can call this function to generate an icon and it works fine. But in one circumstance, it does not. After an accepted "drop", in the dropEvent method of my button subclass, I call:

    Qt Code:
    1. this->setIcon(GMScriptCenter::shared().badgeIcon(QSize(this->iconSize()), Qt::darkBlue, QColor(0, 80, 180, 200), "1"));
    To copy to clipboard, switch view to plain text mode 

    It crashes! And bizarrely, it doesn't crash if I comment out either the setBrush or drawRoundedRect lines. I can generate this badge with just a frame and text and it's fine. It's when I try to use that brush...

    What on earth could be happening? Is this a thread-related issue?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter crash when generating icon after drag-drop

    QIcon takes a const reference to a pixmap.
    And when you return from the slot, your pixmap is destroyed, leaving the reference invalid.
    To test this, make your pixmap static and see if it still crashes.
    If it does not, better change the pixmap from static to a member, or create it on the heap.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2010
    Location
    Beijing, China
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter crash when generating icon after drag-drop

    That does indeed prevent a crash. I thought I had solved it by moving the update() call that was coming from a successful drop on the target side to just after [QDrag]->exec() on the source side (using the drop target from QDrag info).

    Now you've made me think about what's happening here more carefully. Does that mean that the QIcon keeps hold of the QPixmap? If I do create it on the heap, who's reponsible for deleting it? Apologies if this seems like a daft question...

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter crash when generating icon after drag-drop

    Does that mean that the QIcon keeps hold of the QPixmap?
    No, it means exactly the opposite.
    If the QIcon had its own deep copy, it wouldn't need the external pixmap object to stay alive.

    If I do create it on the heap, who's reponsible for deleting it? Apologies if this seems like a daft question...
    Not darf at all, would be better if more people would think that far to ask it.
    The answer depends on how you allocate your pixmap.
    If you give it a QObject parent, you don't have to worry about destruction, the parent will.
    If its parent-less, you will have to take care of deleting it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 2
    Last Post: 13th October 2010, 21:51
  2. QListWidgetItem subclass - Crash program in drag/drop
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 09:24
  3. Replies: 20
    Last Post: 15th August 2008, 23:19
  4. Icon Change in Drag & Drop
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2006, 12:03
  5. Replies: 5
    Last Post: 23rd May 2006, 11:40

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.