当前位置: 技术问答>linux和unix
困扰多时的QT中显示的问题!
来源: 互联网 发布时间:2015-10-15
本文导语: 我要在自己建的窗口中显示从服务器下载的图片,显示部分我自己建了一个类ImageViewer,从服务器上获取图片都已经成功,但是显示就是不出来.下面是我的ImageViewer的定义,我会在另外一个类中调用它,也就是在我下载完毕...
我要在自己建的窗口中显示从服务器下载的图片,显示部分我自己建了一个类ImageViewer,从服务器上获取图片都已经成功,但是显示就是不出来.下面是我的ImageViewer的定义,我会在另外一个类中调用它,也就是在我下载完毕后调用ImageViewer中的openImage来显示,但是根据我的调试结果来看,程序无法进入ImageViewer类中的paintEvent函数,所以导致无法显示,我在QT的帮助文档中查找出来每次调用repaint()函数的时候就可以直接进入paintEvent函数,但是我为什么调用了repaint()就不能进入,如果有大侠知道望告之应该怎么样才能正确调用啊,请各位大侠帮小弟解决一下,万分感谢.特奉上100分作为感谢:)
class ImageViewer : public QWidget
{
Q_OBJECT
public :
ImageViewer(QWidget *parent=0, const char *name = 0);
bool openImage(const QString &fileName);
protected :
void paintEvent(QPaintEvent *);
private:
bool modified;
QImage *image;
};
ImageViewer :: ImageViewer(QWidget *parent, const char *name) : QWidget(parent, name) //Inherit QDialog
{
modified = false;
}
bool ImageViewer::openImage(const QString &fileName)
{
QImage *loadedImage = new QImage;
if (!loadedImage->load(fileName))
return false;
image = loadedImage;
modified = false;
repaint();
return true;
}
void ImageViewer::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), *image);
}
调用的时候的程序段为
imaViewer = new ImageViewer(this);
imaViewer->openImage("image1.jpg");
delete imaViewer;
imaViewer = 0;
class ImageViewer : public QWidget
{
Q_OBJECT
public :
ImageViewer(QWidget *parent=0, const char *name = 0);
bool openImage(const QString &fileName);
protected :
void paintEvent(QPaintEvent *);
private:
bool modified;
QImage *image;
};
ImageViewer :: ImageViewer(QWidget *parent, const char *name) : QWidget(parent, name) //Inherit QDialog
{
modified = false;
}
bool ImageViewer::openImage(const QString &fileName)
{
QImage *loadedImage = new QImage;
if (!loadedImage->load(fileName))
return false;
image = loadedImage;
modified = false;
repaint();
return true;
}
void ImageViewer::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), *image);
}
调用的时候的程序段为
imaViewer = new ImageViewer(this);
imaViewer->openImage("image1.jpg");
delete imaViewer;
imaViewer = 0;
|
Don't try relese imaViewer so fast,
just remove line "delete imaViewer;"!
just remove line "delete imaViewer;"!