2

I want to do some unit tests with qgraphicsitem. Below is simple code to test 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);

When I run the code, test passes. But I don't see selection rect around qgraphicsitem. What is the problem?

Below is the full code.

//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 Answer 1

1

In the QTestLib Tutorial, they say in Chapter 3:

QTestLib features some mechanisms to test graphical user interfaces. Instead of simulating native window system events, QTestLib sends internal Qt events. That means there are no side-effects on the machine the tests are running on.

The lack of the selection rectangle could be caused by this. I had a problem with a QTableView caused by this: Double clicks did not work properly as the native double click event opened the cell for editing while the test event did not.

2
  • As far as I understand from your experience and Qt documentation solution is not possible at the moment Nov 22, 2012 at 13:43
  • It should be possible to create your own (faked) native events and insert them into Qt's Event Loop directly (not using QTest's methods). However I haven't performed any further research on this as the QTestLib behaviour was sufficient for our needs (sending a click to the Table View and another to the Viewport openend the cell for editing).
    – Tim Meyer
    Nov 23, 2012 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.