Hi
I need a bit of help displaying the cursor position of a pixel in a pixmap (4k x 1k pixels) using QGraphicsView . I have subclassed QGraphicsView
{
Q_OBJECT;
public:
virtual ~ viewWidget();
signals:
void grabPosition
(const QPoint);
private:
float xScale;
float yScale;
};
class viewWidget : public QGraphicsView
{
Q_OBJECT;
public:
viewWidget(QWidget* parent =0);
virtual ~ viewWidget();
void paintEvent(QPaintEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
signals:
void grabPosition(const QPoint);
private:
QPoint p;
QString posString;
float xScale;
float yScale;
};
To copy to clipboard, switch view to plain text mode
viewWidget
::viewWidget(QWidget* parent
)xScale(1.0),
yScale(1.0)
{
setMouseTracking(true);
}
viewWidget::~viewWidget(){};
void
{
paint.
setPen(QColor(Qt
::yellow));
paint.drawText(p,posString);
}
void
{
if (scene()==NULL)
return;
p=mapToScene(event->pos());
posString
= QString::number( (p.
x() ) + "," + QString::number( p.
y() );
cout << qPrintable(posString) << endl;
update();
}
viewWidget::viewWidget(QWidget* parent )
:QGraphicsView(parent),
xScale(1.0),
yScale(1.0)
{
setMouseTracking(true);
}
viewWidget::~viewWidget(){};
void
viewWidget::paintEvent(QPaintEvent* event)
{
QGraphicsView::paintEvent(event);
QPainter paint(this);
paint.setPen(QColor(Qt::yellow));
paint.drawText(p,posString);
}
void
viewWidget::mouseMoveEvent(QMouseEvent* event)
{
if (scene()==NULL)
return;
p=mapToScene(event->pos());
posString = QString::number( (p.x() ) + "," + QString::number( p.y() );
cout << qPrintable(posString) << endl;
update();
}
To copy to clipboard, switch view to plain text mode
I create a QGraphicsScene from a pixmap and add it to the view as shown
view=new viewWidget(this); //Create a view widget
scene
->addPixmap
(QPixmap::fromImage(*image
));
view->setScene(scene);
view->show();
view=new viewWidget(this); //Create a view widget
scene=new QGraphicsScene(view);
scene->addPixmap(QPixmap::fromImage(*image));
view->setScene(scene);
view->show();
To copy to clipboard, switch view to plain text mode
However the pixel position is not shown on the view, I get an error from the debugger
warning: QPainter::setPen: Painter not active
What am I doing wrong?
Thanks
Bookmarks