我最近写了个程序,用paintEvent来画图,但始终没有画成功。似乎是update()没有引起paintEvent的调用。你能帮我看一下,万分感激!!程序还在调试,功能就是:作一个框架,显示一张图片。框架是一个类,它继承了QMainWindow。图片的显示是一个类,它继承了QWidget,改写了paintEvent方法。但现在连图片都显示不了。我把我的程序贴出来吧:
/*图片的显示是一个类,它继承了QWidget,改写了paintEvent方法。*/
/*类的声明*/
class Imageviewer:public QWidget
{
Q_OBJECT
public:
Imageviewer();
void setPixmap(QString filename) ;
protected:
void paintEvent ( QPaintEvent *event ) ;
private:
QPixmap pixmap ;
};
/*类的定义*/
Imageviewer::Imageviewer()
{
pixmap = QPixmap("/mnt/hgfs/share_file/qt_image/3.jpg");
qDebug("111 pixmap is null? %d", pixmap.isNull());
}
void Imageviewer::setPixmap(QString filename)
{
pixmap.load(filename) ;
update() ;
qDebug("222 pixmap is null? %d:%s", pixmap.isNull(),qPrintable(objectName()));
}
void Imageviewer::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0, 0, pixmap);
/*没有被调用*/
qDebug("333 pixmap is null? %d", pixmap.isNull());
}
/*框架是一个类,它继承了QMainWindow*/
/*类的声明*/
class Imagewindow :public QMainWindow
{
Q_OBJECT
public:
Imagewindow() ;
private:
QScrollArea *scrollArea ;
Imageviewer *image;
};
/*类的定义*/
Imagewindow::Imagewindow()
{
scrollArea = new QScrollArea ;
image = new Imageviewer;
scrollArea -> setWidget(image);
setCentralWidget(scrollArea);
image->setPixmap("/mnt/hgfs/share_file/qt_image/3.jpg") ;
}
/*主函数*/
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Imagewindow *pWidget = new Imagewindow;
pWidget->resize(300, 300);
pWidget->show();
return app.exec();
}