1、矩形的参数
2、widge.cpp
未使用自动生成的ui
#include "chess.h"
Chess::Chess(QWidget *parent)
: QWidget(parent)
{
Init();
}
Chess::~Chess()
{
}
//-----------events-------------
//画背景 绘图事件里
void Chess::paintEvent(QPaintEvent *){
//方式一:用画家画矩形的方式
// QPainter painter(this);//画家
// QRect rec;
// rec.setTopLeft(QPoint(0,0));
// rec.setBottomRight(QPoint(this->width(),this->height()));
// QPixmap pix("../png/2.jpg");
// bool ret;
// ret=pix.load("../png/2.jpg");
// if(ret==true)
// painter.drawPixmap(rec,pix);
//方式二:直接使用构造函数
QPainter painter(this);//画家
QRect rec(QPoint(0,0),QPoint(this->width(),this->height()));//定义矩形
QPixmap pix(bg_filename);
bool ret;
if(ret==true)
painter.drawPixmap(rec,pix);
}
//-----------end event----------
void Chess::Init(){
bg_filename.clear();
bg_filename = "../png/2.jpg";
}
//-----------function-----------
//实现方法 提供接口
void Chess::ChangeBackgroundImage(const QString filename){
this->bg_filename=filename;
}
//-----------end function-------
3、widge.h
#ifndef CHESS_H
#define CHESS_H
#include <QWidget>
#include <QPainter>
class Chess : public QWidget
{
Q_OBJECT
public:
Chess(QWidget *parent = 0);
~Chess();
//供外部改变背景
void ChangeBackgroundImage(const QString filename);
protected:
void paintEvent(QPaintEvent *);//绘图事件
private:
QString bg_filename;
void Init();
};
#endif // CHESS_H