我是纯
新手,很纯,很纯……
最近研究一个载入
图片的
问题,很疼很疼……
问题一:之前有师兄给了我一个在
widget里面载入图片的例子,可是自己按照他的代码重新写个工程就不能载入,我是直接新建工程,把他的代码粘进去,并且图片也照样放在我的工程文件夹下面,不知道为什么就是载入不了。代码是:
#include "mapwidget.h"
#include <QtGui>
#include <math.h>
MapWidget::MapWidget()
{
readMap();
map.load("map.png");
zoom = 50;
int width = map.width();
int height = map.height();
QGraphicsScene *scene = new QGraphicsScene(this);
scene->setSceneRect(-width/2,-height/2,width,height);
setScene(scene);
setCacheMode(CacheBackground);
QSlider *slider = new QSlider;
slider->setOrientation(Qt::Vertical);
slider->setRange(1,100);
slider->setTickInterval(10);
slider->setValue(50);
connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom(int)));
QLabel *zoominLabel = new QLabel;
zoominLabel->setScaledContents(true);
zoominLabel->setPixmap(QPixmap(":/images/zoomin.png"));
QLabel *zoomoutLabel = new QLabel;
zoomoutLabel->setScaledContents(true);
zoomoutLabel->setPixmap(QPixmap(":/images/zoomout.png"));
// create coordinate area
QFrame *coordFrame = new QFrame;
QLabel *label1 = new QLabel(tr("GraphicsView :"));
viewCoord = new QLabel;
QLabel *label2 = new QLabel(tr("GraphicsScene :"));
sceneCoord = new QLabel;
QLabel *label3 = new QLabel(tr("map :"));
mapCoord = new QLabel;
QGridLayout *grid = new QGridLayout;
grid->addWidget(label1,0,0);
grid->addWidget(viewCoord,0,1);
grid->addWidget(label2,1,0);
grid->addWidget(sceneCoord,1,1);
grid->addWidget(label3,2,0);
grid->addWidget(mapCoord,2,1);
grid->setSizeConstraint(QLayout::SetFixedSize);
coordFrame->setLayout(grid);
// zoom layout
QVBoxLayout *zoomLayout = new QVBoxLayout;
zoomLayout->addWidget(zoominLabel);
zoomLayout->addWidget(slider);
zoomLayout->addWidget(zoomoutLabel);
// coordinate area layou
QVBoxLayout *coordLayout = new QVBoxLayout;
coordLayout->addWidget(coordFrame);
coordLayout->addStretch();
QHBoxLayout *layout = new QHBoxLayout;
layout->addLayout(zoomLayout);
layout->addLayout(coordLayout);
layout->addStretch();
layout->setMargin(30);
layout->setSpacing(10);
setLayout(layout);
setWindowTitle("Map Widget");
setMinimumSize(600,400);
}
// read map information
void
MapWidget::readMap()
{
/* QFile mapFile("maps.txt");
QString mapName;
int ok = mapFile.open(QIODevice::ReadOnly);
if (ok)
{
QTextStream t(&mapFile);
if (!t.atEnd())
{
t >> mapName;
t >> x1 >> y1 >> x2 >> y2;
}
}
map.load(mapName); */
map.load("map.png");
if (map.isNull())
printf("map is null");
}
void
MapWidget::drawBackground(
QPainter *painter, const QRectF &rect)
{
painter->drawPixmap(int(sceneRect().left()),int(sceneRect().top()),map);
}
问题2:请问那句: map.load("map.png");
为什么要不路径?还有他的构造函数里在read()之后又来一个 map.load(":/image/map.png");是什么意思?
问题3:那个MapWidget::drawBackground函数都
没有再构造函数里调用,他是拿来干嘛的?
问题4:为什么那个read()函数里要一个Qfile 定义一个新量?那个txt是已存在的吧?有什么用啊?
纯学习的请教,若有大牛能亲自指点感激不尽,我qq是120643924 [ 此帖被kakaxibush在2011-05-23 19:55重新编辑 ]