Results 1 to 10 of 10

Thread: Replace Pixmap in GraphicsScene

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Replace Pixmap in GraphicsScene

    Quote Originally Posted by anda_skoa View Post
    Hmm, weird.

    Have you tried saving the image? I.e. to see if it is somehow damaged?
    Though I would assume that this would already cause a problem in QPixmap::fromImage().

    Or maybe try showing the resulting pixmap in a separate QLabel, to verify it being OK.

    Cheers,
    _
    Well setting to a label results in a crash in exactly the same place. Oddly, saving to a jpg file works fine (and not attempting to display onscreen). Its upside down, but that's is the only thing odd I see about the image.

    I feel like there is something fundamental about what's wrong. Something I'm just not aware of in the Qt world. I don't think its threads, all the crashes and breakpoints show WinMain in the stack. But I have no idea what it could be.

    Thanks for your help, anyway.

  2. #2
    Join Date
    Dec 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Replace Pixmap in GraphicsScene

    Perhaps it is something about the event handler. If I set the label to one of the stored pixmaps in the constructor, it works. I see the image. But the captured frame still crashes it.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow),
    4. frame(640, 480),
    5. pix_no(0)
    6. {
    7. ui->setupUi(this);
    8.  
    9. frame.load("campix1.jpg");
    10. ui->label->setPixmap(frame);
    11.  
    12. QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    13. cam = new QCamera(cameras.first(), this);
    14. cam->load();
    15. frame_grabber = new CameraFrameGrabber();
    16. cam->setViewfinder(frame_grabber);
    17. cam->setCaptureMode(QCamera::CaptureViewfinder);
    18. connect(frame_grabber, SIGNAL(frameAvailable(QImage)), this, SLOT(handleFrame(QImage)));
    19. cam->start();
    20. frame_timer.start();
    21.  
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void MainWindow::handleFrame(const QImage& img)
    30. {
    31. frame_times.push_back(frame_timer.elapsed());
    32. frame_timer.restart();
    33. if (frame_times.size() > 10) frame_times.pop_front();
    34. int frame_count = frame_times.size();
    35. int frame_total = 0;
    36. for(int i = 0; i < frame_count; ++i)
    37. {
    38. frame_total += frame_times[i];
    39. }
    40. double fps = 0;
    41. if (frame_count)
    42. {
    43. fps = (frame_total / frame_count);
    44. }
    45. ui->sb_fps->setValue(fps);
    46.  
    47. frame = QPixmap::fromImage(img);
    48.  
    49. QString fname("campix");
    50. fname += QString::number(++pix_no);
    51. fname += ".jpg";
    52. bool success = frame.save(fname);
    53.  
    54. ui->label->setPixmap(frame);
    55.  
    56. this->update();
    57. }
    To copy to clipboard, switch view to plain text mode 

    This is the stack while stopped at a breakpoint in the handleFrame event.

    Qt Code:
    1. 1 MainWindow::handleFrame mainwindow.cpp 62 0x7ff6d7082e44
    2. 2 MainWindow::qt_static_metacall moc_mainwindow.cpp 73 0x7ff6d7087bb5
    3. 3 QMetaObject::activate qobject.cpp 3742 0x5d14daf2
    4. 4 QMetaObject::activate qobject.cpp 3603 0x5d14d268
    5. 5 CameraFrameGrabber::frameAvailable moc_cameraframegrabber.cpp 128 0x7ff6d7087f1b
    6. 6 CameraFrameGrabber::present cameraframegrabber.cpp 56 0x7ff6d7086e00
    7. 7 DSCameraSession::presentFrame dscamerasession.cpp 683 0x7ffc074ca264
    8. 8 DSCameraSession::qt_static_metacall moc_dscamerasession.cpp 111 0x7ffc074da2e6
    9. 9 QMetaCallEvent::placeMetaCall qobject.cpp 503 0x5d156c81
    10. 10 QObject::event qobject.cpp 1263 0x5d14f6df
    11. 11 QApplicationPrivate::notify_helper qapplication.cpp 3799 0x5d8a5e8e
    12. 12 QApplication::notify qapplication.cpp 3159 0x5d8a0713
    13. 13 QCoreApplication::notifyInternal2 qcoreapplication.cpp 988 0x5d0f78c6
    14. 14 QCoreApplication::sendEvent qcoreapplication.h 231 0x5d1027f2
    15. 15 QCoreApplicationPrivate::sendPostedEvents qcoreapplication.cpp 1649 0x5d0f9112
    16. 16 QEventDispatcherWin32::sendPostedEvents qeventdispatcher_win.cpp 1295 0x5d1b180a
    17. 17 QWindowsGuiEventDispatcher::sendPostedEvents qwindowsguieventdispatcher.cpp 82 0x7ffbfc70c204
    18. 18 qt_internal_proc qeventdispatcher_win.cpp 445 0x5d1af521
    19. 19 CallWindowProcW USER32 0x7ffc29f41c24
    20. 20 DispatchMessageW USER32 0x7ffc29f4156c
    21. 21 QEventDispatcherWin32::processEvents qeventdispatcher_win.cpp 845 0x5d1aff2b
    22. 22 QWindowsGuiEventDispatcher::processEvents qwindowsguieventdispatcher.cpp 74 0x7ffbfc70c1c4
    23. 23 QEventLoop::processEvents qeventloop.cpp 135 0x5d0f24e8
    24. 24 QEventLoop::exec qeventloop.cpp 210 0x5d0f272e
    25. 25 QCoreApplication::exec qcoreapplication.cpp 1261 0x5d0f55ff
    26. 26 QGuiApplication::exec qguiapplication.cpp 1640 0x7ffbfccf3208
    27. 27 QApplication::exec qapplication.cpp 2976 0x5d8a021a
    28. 28 main main.cpp 10 0x7ff6d7082749
    29. 29 WinMain qtmain_win.cpp 123 0x7ff6d708afbd
    30. 30 invoke_main exe_common.inl 109 0x7ff6d7088bed
    31. 31 __scrt_common_main_seh exe_common.inl 264 0x7ff6d7088a8e
    32. 32 __scrt_common_main exe_common.inl 309 0x7ff6d708894e
    33. 33 WinMainCRTStartup exe_winmain.cpp 17 0x7ff6d7088c09
    34. 34 BaseThreadInitThunk KERNEL32 0x7ffc2a4a8364
    35. 35 RtlUserThreadStart ntdll 0x7ffc2c1a5e91
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Replace Pixmap in GraphicsScene

    Very strange, the conversion to QPixmap should have consumed the original pixel data, anything afterwards should be fine.

    Can you try something like this?

    Qt Code:
    1. QImage imgCopy = img;
    2. imgCopy.detach();
    3. QPixmap frame = QPixmap::fromImage(imgCopy);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  4. #4
    Join Date
    Dec 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Replace Pixmap in GraphicsScene

    Woah! That worked!

    In my code the QPixmap frame is a member variable. So it is not falling out of scope. The QImage however, as a stack variable, is falling out of scope. So I guess we have to proceed from the supposition that a reference to the image was being held.

    Reviewing the documentation I see that the overload:

    Qt Code:
    1. QPixmap QPixmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor)
    To copy to clipboard, switch view to plain text mode 

    says "...without copying if possible." I guess that actually applies to both overloads.

    I suppose the lesson learned is to look for detach()? If I see it I need to be mindful of the possibility that there may be lingering references if I don't call it?

    Anyway, thanks again. I can move forward with my project.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Replace Pixmap in GraphicsScene

    Maybe but I expect more a threading problem.

    Anyway, since it works,lets not dwell on that

    Cheers,
    _

  6. #6
    Join Date
    Dec 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Replace Pixmap in GraphicsScene

    Hmm. It'd have to be an implicit thread. I don't start one and all crashes and break points show WinMain in the stack trace.

Similar Threads

  1. Pixmap transform: translate pixmap
    By DeepKling in forum Qt Programming
    Replies: 5
    Last Post: 23rd January 2015, 20:34
  2. Replies: 1
    Last Post: 19th April 2011, 11:17
  3. To find X, Y position of a pixmap item in Graphicsscene
    By augusbas in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2011, 06:09
  4. transfer graphicsscene on form1 to graphicsscene on form2
    By rogerholmes in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2009, 20:37
  5. GraphicsScene Question
    By QbelcorT in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2008, 08:03

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
  •  
Qt is a trademark of The Qt Company.