我想用 qgraphicsitem 做一些单元测试。下面是测试 qgraphicsitem 的简单代码。
QTest::mousePress(viewToTest->viewport(),Qt::LeftButton,Qt::NoModifier,viewToTest->mapFromScene(50,50));
QGraphicsItem *item = viewToTest->itemAt(viewToTest->mapFromScene(50,50));
TestEventLoop::loop(1000);
QCOMPARE(item->isSelected(),true);
当我运行代码时,测试通过了。但我没有看到围绕 qgraphicsitem 的选择矩形。问题是什么?
下面是完整的代码。
//MyItem implementation
#include "MyItem.h"
#include <QtCore/QDebug>
#include <QtGui/QGraphicsSceneMouseEvent>
MyItem::MyItem()
{
setFlags(QGraphicsItem::ItemIsSelectable);
contextMenu = new QMenu;
testAction = new QAction(contextMenu);
testAction->setText("Test action");
contextMenu->addAction(testAction);
}
MyItem::~MyItem()
{
}
QRectF MyItem::boundingRect() const
{
return QRectF(-5,-5,60,60);
}
void MyItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /* = 0 */ )
{
QPen itemPen;
itemPen.setColor(Qt::black);
itemPen.setWidth(2);
painter->setPen(itemPen);
painter->drawRect(QRectF(0,0,50,50));
if(isSelected())
{
QPen selectedPen;
selectedPen.setColor(Qt::red);
selectedPen.setWidth(4);
painter->setPen(selectedPen);
painter->drawRect(QRectF(-4,-4,58,58));
}
}
void MyItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
if(event->button() == Qt::RightButton)
{
contextMenu->move(event->screenPos());
contextMenu->show();
}
else if(event->button() == Qt::LeftButton)
{
update();
}
else{}
QGraphicsItem::mousePressEvent(event);
}
QPainterPath MyItem::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
//MyView implementation
#include "MyView.h"
#include "TestEventLoop.h"
#include <QtGui/QMouseEvent>
#include <QtCore/QDebug>
#include <QtGui/QGraphicsItem>
MyView::MyView()
: QGraphicsView()
{
}
MyView::~MyView()
{
}
void MyView::mousePressEvent(QMouseEvent *event)
{
qDebug()<<"Mouse clicked to:"<<event->pos();
QGraphicsView::mousePressEvent(event);
TestEventLoop::loop(1000);
}
void MyView::mouseMoveEvent( QMouseEvent *event )
{
qDebug()<<"Mouse moved to:"<<event->pos();
}
//GraphicsViewTester implementation
#include "GraphicsViewTester.h"
#include "MyView.h"
#include "TestEventLoop.h"
#include <QtGui/QGraphicsItem>
GraphicsViewTester::GraphicsViewTester(MyView *view)
{
viewToTest = view;
}
GraphicsViewTester::~GraphicsViewTester()
{
}
void GraphicsViewTester::itemMouseClickTest()
{
QTest::mousePress(viewToTest->viewport(),Qt::LeftButton,Qt::NoModifier,viewToTest->mapFromScene(50,50));
QGraphicsItem *item = viewToTest->itemAt(viewToTest->mapFromScene(50,50));
TestEventLoop::loop(1000);
QCOMPARE(item->isSelected(),true);
}
//TestEventLoop implementation
#include "TestEventLoop.h"
TestEventLoop* TestEventLoop::instance = NULL;
TestEventLoop::TestEventLoop()
: QEventLoop()
{
}
TestEventLoop::~TestEventLoop()
{
}
TestEventLoop* TestEventLoop::getInstance()
{
if(instance == NULL)
{
instance = new TestEventLoop;
}
else{}
return instance;
}
void TestEventLoop::loop(const int msecs)
{
QTimer timer;
connect(&timer,SIGNAL(timeout()),TestEventLoop::getInstance(),SLOT(quit()));
timer.setSingleShot(true);
timer.start(msecs);
instance->exec();
}
//TestManager implementation
#include "TestManager.h"
#include "mainwindow.h"
#include "GraphicsViewTester.h"
TestManager::TestManager(void)
{
}
TestManager::~TestManager(void)
{
}
void TestManager::runTests()
{
MainWindow mainWindow;
GraphicsViewTester tester(mainWindow.sceneView());
QTest::qExec(&tester);
}
//MainWindow implementation
#include "mainwindow.h"
#include <QtCore/QDebug>
#include <QtGui/QGraphicsScene>
#include <QtGui/QHBoxLayout>
#include "MyView.h"
#include "MyItem.h"
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(0,0,ui.centralWidget->width(),ui.centralWidget->height());
QHBoxLayout *layout = new QHBoxLayout;
myView = new MyView;
myView->setScene(scene);
layout->addWidget(myView);
ui.centralWidget->setLayout(layout);
MyItem *myItem = new MyItem;
myItem->setPos(0,0);
scene->addItem(myItem);
}
MainWindow::~MainWindow()
{
delete myView;
}
MyView* MainWindow::sceneView() const
{
return myView;
}