<1>. 头文件(类声明)
class CPreviewWidge : public QWidget
{
Q_OBJECT
public:
CPreviewWidge( const QString &strPath, QWidget *parent = 0);
~CPreviewWidge();
QSize sizeHint()const;
protected:
void paintEvent(QPaintEvent *e); //九宫图重绘函数
private:
QImage m_imgLTop; //九宫图图片左上
QImage m_imgLCur; //九宫图图片左中
QImage m_imgLBut; //九宫图图片左下
QImage m_imgCTop; //九宫图图片中上
QImage m_imgCCur; //九宫图图片中中
QImage m_imgCBut; //九宫图图片中下
QImage m_imgRTop; //九宫图图片右上
QImage m_imgRCur; //九宫图图片右中
QImage m_imgRBut; //九宫图图片右下
};
<2>. cpp文件(类定义)
CPreviewWidge::CPreviewWidge( const QString &strPath, QWidget *parent ) //传参:九宫图中上图片路径和控件父类对象
: QWidget( parent )
{
int iLeft = strPath.lastIndexOf( "_" );
QString strLeftPath = strPath.left( iLeft );
m_imgLTop = QImage( strLeftPath + "_ltop.png" );
m_imgLCur = QImage( strLeftPath + "_lcur.png" );
m_imgLBut = QImage( strLeftPath + "_lbot.png" );
m_imgCTop = QImage( strLeftPath + "_ctop.png" );
m_imgCCur = QImage( strLeftPath + "_ccur.png" );
m_imgCBut = QImage( strLeftPath + "_cbot.png" );
m_imgRTop = QImage( strLeftPath + "_rtop.png" );
m_imgRCur = QImage( strLeftPath + "_rcur.png" );
m_imgRBut = QImage( strLeftPath + "_rbot.png" );
}
CPreviewWidge::~CPreviewWidge()
{
}
QSize CPreviewWidge::sizeHint() const
{
int iHeight = m_imgLTop.height() + m_imgLCur.height() + m_imgLBut.height();
int iWidth = m_imgLTop.width() + m_imgCTop.width() + m_imgRTop.width();
return QSize( iWidth, iHeight );
}
void CPreviewWidge::paintEvent ( QPaintEvent* e )
{
QWidget::paintEvent( e );
QPainter painter( this );
QRect r = rect();
painter.drawImage( r.topLeft().x(), r.topLeft().y(), m_imgLTop );
painter.drawImage( r.topRight().x() - m_imgRTop.width(), r.topRight().y(), m_imgRTop );
painter.drawImage( r.bottomLeft().x(), r.bottomLeft().y() - m_imgLBut.height(), m_imgLBut );
painter.drawImage( r.bottomRight().x() - m_imgRBut.width(), r.bottomRight().y() - m_imgRBut.height(), m_imgRBut );
QRect rectLCut( r.topLeft().x(), r.topLeft().y() + m_imgLTop.height(),
m_imgLCur.width(), ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
painter.drawImage( rectLCut, m_imgLCur );
QRect rectRCut( r.topRight().x() - m_imgRTop.width(), r.topRight().y() + m_imgRTop.height(),
m_imgRCur.width(), ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
painter.drawImage( rectRCut, m_imgRCur );
QRect rectCTop( r.topLeft().x() + m_imgLTop.width(), r.topLeft().y(),
( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(), m_imgCTop.height() );
painter.drawImage( rectCTop, m_imgCTop );
QRect rectCBot( r.bottomLeft().x() + m_imgLBut.width(), r.bottomLeft().y() - m_imgLBut.height(),
( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(), m_imgCBut.height() );
painter.drawImage( rectCBot, m_imgCBut );
QRect rectCCur( r.topLeft().x() + m_imgLTop.width(), r.topLeft().y() + m_imgLTop.height(),
( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(),
( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
painter.drawImage( rectCCur, m_imgCCur );
}