|
我做一个小程序,有一点不明白:下面的程序运行是正确的,可以见到图片,但是当我改成定义label后,并且在paintEvent中将第一句改成QPainter p(label)后,运行后就见不到图片了呢??
//form1.h
#ifndef FORM1_H
#define FORM1_H
#include <qpixmap.h>
#include <qvbox.h>
#include <qlabel.h>
class Form1:public QVBox
{
Q_OBJECT
public:
Form1(QWidget *parent=0,const char *name=0);
private:
QPixmap pic;
//QLabel *label;
protected:
void paintEvent(QPaintEvent*);
};
#endif
//form1.cpp
#include "form1.h"
#include <qpainter.h>
Form1::Form1(QWidget *parent,const char *name):QVBox(parent,name)
{
pic.load("/home/huting/2.jpg");
//label=new QLabel(this);
}
void Form1::paintEvent(QPaintEvent*)
{
QPainter p(this);
p.drawPixmap(0,0,pic);
}
//main.cpp
#include <qapplication.h>
#include "form1.h"
int main(int argc,char **argv)
{
QApplication a(argc,argv);
Form1 *win=new Form1;
win->resize(400,300);
a.setMainWidget(win);
win->show();
return a.exec();
}
|
|