首先,谢谢你的回复。
按照你的说法,我再看了下书,早上再编了一下,能出来个静态的。以下是静态的矩形
QGraphicsRectItem* item = new QGraphicsRectItem(0,0,100,100);
item->setPen( QPen(QColor(255,0,0)) );
QGraphicsScene* scene = new QGraphicsScene;
scene->addItem( item );
ui.graphicsView->setScene( scene );
我是直接在test.ui里,也就是可视化的那个界面里直接添加一个
graphicsView
那又怎么才能利用 mouseMoveEvent编写个能用鼠标拖出来的矩形,碰到这个,又晕了。
这是我想动态画矩形的程序
x = firstPoint.x();
y = firstPoint.y();
w = endPoint.x() - x;
h = endPoint.y() - y;
QGraphicsRectItem* item = new QGraphicsRectItem(x,y,w,h);
item->setPen( QPen(QColor(255,0,0)) );
QGraphicsScene* scene = new QGraphicsScene;
scene->addItem( item );
ui.graphicsView->setScene( scene );
//QGraphicsView *view =new QGraphicsView(scene);
//view->resize(640,480);
//view->show();
//item->setPos( 100,100 );
//item->setZValue( 0.0 );
}
test1::~test1()
{
}
void test1::mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::LeftButton)
firstPoint=event->pos();
}
void test1::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()==Qt::LeftButton) //鼠标左键按下的同时移动鼠标
{
endPoint=event->pos();
update();
}
}
使劲点,就是不出来。。
[ 此帖被za21112在2011-01-18 08:45重新编辑 ]