使用QTest :: mousePress进行QGraphicsItem选择

时间:2012-11-22 09:53:21

标签: qt unit-testing qgraphicsview qgraphicsitem qtestlib

我想用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;
}

1 个答案:

答案 0 :(得分:1)

在QTestLib教程中,他们在Chapter 3中说:

  

QTestLib具有一些测试图形用户界面的机制。   QTestLib发送,而不是模拟本机窗口系统事件   内部Qt事件。这意味着没有副作用   机器测试正在运行。

缺少选择矩形可能是由此引起的。我遇到了由此导致的QTableView问题:双击无法正常工作,因为本机双击事件打开了单元格进行编辑,而测试事件没有。