然后重载paintEvent函数,在paintEvent里面画张图片就搞定了。代码:
qpicmsgbox.h
///////////////////////////////////
#ifndef QPICMSGBOX_H
#define QPICMSGBOX_H
#include <QMessageBox>
#include <QImage>
class QPicMsgBox : public QMessageBox
{
Q_OBJECT
public:
explicit QPicMsgBox(QWidget *parent = 0)
QImage *pImage
protected:
virtual void paintEvent(QPaintEvent *paintevent)
signals:
public slots:
}
#endif // QPICMSGBOX_H
///////////////////////////////////////////////
qpicmsgbox.cpp
////////////////////////////////////////////
#include "qpicmsgbox.h"
#include <QString>
#include <QPainter>
QPicMsgBox::QPicMsgBox(QWidget *parent) :
QMessageBox(parent)
{
QString fileName = tr("7.jpg")
pImage = new QImage(fileName)
}
void QPicMsgBox::paintEvent(QPaintEvent *paintevent)
{
QPainter painter(this)
painter.drawImage(0, 0, pImage->scaled(this->size()))
QMessageBox::paintEvent(paintevent)
}
/////////////////////////////////////
//绘制的背景图就是7.jpg缩放的,和消息框一样大。
调用上面QPicMsgBox的时候就添加代码:
#include "qpicmsgbox.h"
/////////////////////
void MainWindow::on_actionMsg_triggered() ////调用图片消息对话框
{
QPicMsgBox *pMsgBox = new QPicMsgBox
pMsgBox->setWindowTitle(QString(tr("hello")))
pMsgBox->setText(QString(tr("hellogfkjdsgkldfsljgldsfgklfdkjhglkdfs")))
pMsgBox->addButton(QMessageBox::Ok)
pMsgBox->addButton(QMessageBox::Close)
pMsgBox->exec()/////////////exec是模式对话框,show是非模式的
}
void ScrollArea::set_image(char *filename){
QImage *ppm = new QImage(filename)
label = new QLabel()
printf("ppm->width()=%d, ppm->height()=%d\n",ppm->width(), ppm->height())//获取图片的宽度和高度
label->setPixmap(QPixmap::fromImage(*ppm))
this->setWidget(label)
/*设置窗口最大高度和宽度为1024*768*/
this->setMaximumHeight(768)
this->setMaximumWidth(1024)
this->resize(QSize( ppm->width()+5, ppm->height() +5))
return
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)