Hi all,
I have a custom QGraphicsItem:
header:
#ifndef MERCATOR_GRAPHICS_ITEM_H
#define MERCATOR_GRAPHICS_ITEM_H
#include <QPainter>
#include <QGraphicsItem>
#include <QDebug>
#include <QSize>
{
public:
Mercator_graphics_item
(QSize size_of_view
);
void resize
(QSize new_size
);
private:
};
#endif // MERCATOR_GRAPHICS_ITEM_H
#ifndef MERCATOR_GRAPHICS_ITEM_H
#define MERCATOR_GRAPHICS_ITEM_H
#include <QPainter>
#include <QGraphicsItem>
#include <QDebug>
#include <QSize>
class Mercator_graphics_item : public QGraphicsItem
{
public:
Mercator_graphics_item(QSize size_of_view);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void resize(QSize new_size);
QSize get_dimension();
private:
QSize * size_of_Item;
};
#endif // MERCATOR_GRAPHICS_ITEM_H
To copy to clipboard, switch view to plain text mode
.cpp file:
#include "mercator_graphics_item.h"
Mercator_graphics_item
::Mercator_graphics_item(QSize size_of_view
){
size_of_Item
= new QSize(size_of_view.
width(),size_of_view.
height());
qDebug() << boundingRect();
}
QRectF Mercator_graphics_item
::boundingRect() const {
}
{
qDebug() << "paint called";
Q_UNUSED(option);
Q_UNUSED(widget);
painter->drawRect(map);
}
void Mercator_graphics_item
::resize(QSize new_size
) {
*size_of_Item = new_size;
update();
}
QSize Mercator_graphics_item
::get_dimension() {
return *this->size_of_Item;
}
#include "mercator_graphics_item.h"
Mercator_graphics_item::Mercator_graphics_item(QSize size_of_view)
{
size_of_Item = new QSize(size_of_view.width(),size_of_view.height());
qDebug() << boundingRect();
}
QRectF Mercator_graphics_item::boundingRect() const
{
return QRectF(0,0,100,100);
}
void Mercator_graphics_item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
qDebug() << "paint called";
Q_UNUSED(option);
Q_UNUSED(widget);
QRectF map = boundingRect();
painter->drawRect(map);
}
void Mercator_graphics_item::resize(QSize new_size)
{
*size_of_Item = new_size;
update();
}
QSize Mercator_graphics_item::get_dimension()
{
return *this->size_of_Item;
}
To copy to clipboard, switch view to plain text mode
and this is the method where I create my item:
ui->setupUi(this);
Mercator_graphics_item map(ui->graphicsView->size());
qDebug() << ↦
draw_scene->addItem(&map);
draw_scene->update();
ui->graphicsView->setScene(draw_scene);
ui->setupUi(this);
draw_scene = new QGraphicsScene(this);
Mercator_graphics_item map(ui->graphicsView->size());
qDebug() << ↦
draw_scene->addItem(&map);
draw_scene->update();
ui->graphicsView->setScene(draw_scene);
To copy to clipboard, switch view to plain text mode
draw_scene is declared in a different file and it is of type QGraphicsScene *. The problem is that when I run my application nothing is drawn on the graphicsView, and the paint method is not called, I know because the string "print called" (line 17 of .cpp file) is never printed on my console..
Can someone explain me what I'm doing wrong please?
I used the same procedure on another project and in it works well...
Bookmarks