I build a widget has a qlabel, below is its constructor
Qt Code:
  1. #ifndef VIEWER_H
  2. #define VIEWER_H
  3.  
  4. #include <iostream>
  5.  
  6. #include <QWidget>
  7. #include <QPixmap>
  8. #include <QLabel>
  9. #include <QImage>
  10. #include <QVBoxLayout>
  11.  
  12.  
  13. using namespace std;
  14.  
  15. class Viewer : public QWidget
  16. {
  17. Q_OBJECT
  18.  
  19. public:
  20. Viewer(QWidget *parent=0);
  21. public slots:
  22. void setIplImage();
  23.  
  24.  
  25. private:
  26.  
  27. QVBoxLayout *mainLayout;
  28. QLabel *movieLabel;
  29.  
  30.  
  31. };
  32.  
  33. #endif
  34.  
  35. Viewer::Viewer(QWidget* parent)
  36. : QWidget(parent)
  37. {
  38. movieLabel = new QLabel(tr("No movie loaded"));
  39. movieLabel->setAlignment(Qt::AlignCenter);
  40. movieLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  41. movieLabel->setBackgroundRole(QPalette::Dark);
  42. movieLabel->setAutoFillBackground(true);
  43. movieLabel->setScaledContents(true);
  44.  
  45.  
  46.  
  47. mainLayout = new QVBoxLayout;
  48. mainLayout->addWidget(movieLabel);
  49. setLayout(mainLayout);
  50.  
  51. }
  52.  
  53.  
  54. void
  55. Viewer::setIplImage()
  56. {
  57.  
  58. QImage image("test.jpg");
  59. //image.save("test2.jpg"); //it works
  60.  
  61. movieLabel->setPixmap(QPixmap::fromImage(image)); //compile success but runtime error with access violation.
  62.  
  63. }
To copy to clipboard, switch view to plain text mode 

and if i use it in constructor, the setPixmap() can run.
Thanks for any comments