As my work are going well on my long project of QT, I've tried to resolve my actual steps, but I think it is not working, so I have one question:
Draw points from data file on QGraphicsView
My code is writting actually at the end of the process a data file where the first line is the number of elements (size of the vector) and the other lines the values of the point with x and y separated by a blank.
res.dat
250
12 23
30 40
25 67
...
I would like to read this file (with the fstream) and to display each points of my vector on a QGraphicsView in order to have a graphic result, and ideally but not the most important result, to lie this point to have the desired form.
I have already tried this part of code on a main source file to test, but it is just doing a circle and I think it is not reading my file.
main.cpp
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPointF>
#include <QVector>
#include <QApplication>
#include <fstream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int n;
std::ifstream ifs1("res.dat");
ifs1 >> n;
QVector <QPointF> points(n);
// Create a view, put a scene in it and add tiny circles
// in the scene
QGraphicsView * view = new QGraphicsView();
QGraphicsScene * scene = new QGraphicsScene();
view->setScene(scene);
for(int i = 1; i< n; i++)
scene->addEllipse(points[i].x(), points[i].y(), 512, 512);
// Show the view
view->show();
return a.exec();
}
Thank you in advance for reading, and for your response.
From your code it's completely missing the part where you read the actual coordinates of the points.
I have changed slightly your code to read the points into the vector but please remember:
a) This code is all but "solid" for example if your res.dat file is just a bit out of format it crashes.
b) With this loop you really do not need to know how many points you have in the file, right now it keeps reading until the end of file.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int n;
std::ifstream ifs1("res.dat");
ifs1 >> n;
QVector <QPointF> points;
qreal px,py;
while (ifs1 >> px) {
ifs1 >> py;
points.append(QPointF(px,py));
}
// Create a view, put a scene in it and add tiny circles
// in the scene
QGraphicsView * view = new QGraphicsView();
QGraphicsScene * scene = new QGraphicsScene();
view->setScene(scene);
foreach (QPointF point, points)
scene->addEllipse(point.x(), point.y(), 51, 51);
// Show the view
view->show();
return a.exec();
}
After you helped me, I've gone further in my code, and I have a problem I don't understand.
in the header file
//All includes
class ProjectWindow;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
// GraphicsView 1
QGraphicsView view1;
QGraphicsScene scene1;
QGraphicsPixmapItem item1;
QGraphicsPixmapItem item2;
QString fileName;
// GraphicsView 2
QGraphicsScene scene2;
QGraphicsView view2;
QVector <QPointF> points;
qreal px,py;
QPointF point;
QPainter painter;
QGraphicsEllipseItem *ellipse;
public slots:
void openBrowser();
void drawcirc();
void drawCircle();
void runSnakes();
void displayResult();
};
#endif // MAINWINDOW_H
in the .cpp file
void MainWindow::displayResult()
{
scene2.removeItem(&item2); //removing a picture
scene2.addItem(&item2); // adding a picture
scene2.setSceneRect(scene2.itemsBoundingRect());
// Reading res.dat file and plot the contour
int m;
std::ifstream ifs2("final_contour.dat");
ifs2 >> m;
std::cout << m;
while (ifs2 >> px)
{
ifs2 >> py;
points.append(QPointF(py,px));
}
foreach(point, points)
ellipse = scene2.addEllipse(QRectF(point.y(), width-point.x(),1,1),QPen(Qt::red));
// Saving the final contour into a picture
QImage pixmap(width, width, QImage::Format_ARGB32_Premultiplied);
painter.begin(&pixmap);
painter.setRenderHint(QPainter::Antialiasing, false);
scene2.render(&painter);
painter.end();
pixmap.save("finalcontour.bmp", "BMP");
}
I want to remove the ellipse I add to the scene with removeItem() (in order to see the differents results with changing parameters), so I need to create a QGraphicsEllipseItem and to tell to the scene, addItem() that I can remove later the item with removeItem().
the header file
//All includes
class ProjectWindow;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
// GraphicsView 1
QGraphicsView view1;
QGraphicsScene scene1;
QGraphicsPixmapItem item1;
QGraphicsPixmapItem item2;
QString fileName;
// GraphicsView 2
QGraphicsScene scene2;
QGraphicsView view2;
QVector <QPointF> points;
qreal px,py;
QPointF point;
QPainter painter;
QGraphicsEllipseItem ellipse; // The only line changed
public slots:
void openBrowser();
void drawcirc();
void drawCircle();
void runSnakes();
void displayResult();
};
#endif // MAINWINDOW_H
the cpp file
void MainWindow::displayResult()
{
scene2.removeItem(&item2); //removing a picture
scene2.addItem(&item2); // adding a picture
scene2.setSceneRect(scene2.itemsBoundingRect());
// Reading res.dat file and plot the contour
int m;
std::ifstream ifs2("final_contour.dat");
ifs2 >> m;
std::cout << m;
while (ifs2 >> px)
{
ifs2 >> py;
points.append(QPointF(py,px));
}
foreach(point, points)
ellipse.setRect(QRectF(point.y(), width-point.x(),1,1));
ellipse.setPen(QPen(Qt::red));
scene2.addItem(&ellipse);
// Saving the final contour into a picture
QImage pixmap(width, width, QImage::Format_ARGB32_Premultiplied);
painter.begin(&pixmap);
painter.setRenderHint(QPainter::Antialiasing, false);
scene2.render(&painter);
painter.end();
pixmap.save("finalcontour.bmp", "BMP");
}
But changing the way I want to add the ellipse, it is not displaying the whole points like this with the first way I define the ellipse:
But just one point and not the vector of points, like this:
Related
I am originally loading image in QGraphicsView and using this method for basic zoom out and zoom in functionality.
However, I am unable to retrieve actual image pixel position using mapToScene functionality in eventFilter function of Graphics_view_zoom class. The below code produces behaviour exactly as windows photo viewer zooming only selected region.
MapToScene() returns same Point as mouse event position.
Here is the class which deals with zooming.
#include "Graphics_view_zoom.h"
#include <QMouseEvent>
#include <QApplication>
#include <QScrollBar>
#include <qmath.h>
Graphics_view_zoom::Graphics_view_zoom(QGraphicsView* view)
: QObject(view), _view(view)
{
_view->viewport()->installEventFilter(this);
_view->setMouseTracking(true);
_modifiers = Qt::ControlModifier;
_zoom_factor_base = 1.0015;
}
void Graphics_view_zoom::gentle_zoom(double factor) {
_view->scale(factor, factor);
_view->centerOn(target_scene_pos);
QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
_view->viewport()->height() / 2.0);
QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
_view->centerOn(_view->mapToScene(viewport_center.toPoint()));
emit zoomed();
}
void Graphics_view_zoom::set_modifiers(Qt::KeyboardModifiers modifiers) {
_modifiers = modifiers;
}
void Graphics_view_zoom::set_zoom_factor_base(double value) {
_zoom_factor_base = value;
}
bool Graphics_view_zoom::eventFilter(QObject *object, QEvent *event) {
if (event->type() == QEvent::MouseMove) {
QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
QPointF delta = target_viewport_pos - mouse_event->pos();
// Here I want to get absolute image coordinates
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) {
target_viewport_pos = mouse_event->pos();
target_scene_pos = _view->mapToScene(mouse_event->pos());
}
} else if (event->type() == QEvent::Wheel) {
QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
if (QApplication::keyboardModifiers() == _modifiers) {
if (wheel_event->orientation() == Qt::Vertical) {
double angle = wheel_event->angleDelta().y();
double factor = qPow(_zoom_factor_base, angle);
gentle_zoom(factor);
return true;
}
}
}
Q_UNUSED(object)
return false;
In mainwindow.cpp,
I am creating object of this class and loading an image as below:
m_GraphicsScene = new QGraphicsScene();
pixmapItem = new QGraphicsPixmapItem();
m_GraphicsScene->addItem(multiview[i].pixmapItem);
view_wrapper = new Graphics_view_zoom(ui->GraphicsView);
ui->GraphicsView->setScene(multiview[i].m_GraphicsScene);
pixmapItem->setPixmap(QPixmap::fromImage("img.jpg"));
multiview[view].m_GraphicsView->fitInView(QRectF(0,0,640,320),Qt::KeepAspectRatio);
Can anyone help with how do I achieve this ?
Keep in mind that the scaling you use only scales the scene, not the items. Given this, the position of the pixel can be obtained, so the algorithm is:
Obtain the mouse position with respect to the QGraphicsView
Transform that position with respect to the scene using mapToScene
Convert the coordinate with respect to the scene in relation to the item using mapFromScene of the QGraphicsItem.
Considering the above, I have implemented the following example:
#include <QtWidgets>
#include <random>
static QPixmap create_image(const QSize & size){
QImage image(size, QImage::Format_ARGB32);
image.fill(Qt::blue);
std::random_device rd;
std::mt19937_64 rng(rd());
std::uniform_int_distribution<int> uni(0, 255);
for(int i=0; i< image.width(); ++i)
for(int j=0; j < image.height(); ++j)
image.setPixelColor(QPoint(i, j), QColor(uni(rng), uni(rng), uni(rng)));
return QPixmap::fromImage(image);
}
class GraphicsView : public QGraphicsView
{
Q_OBJECT
Q_PROPERTY(Qt::KeyboardModifiers modifiers READ modifiers WRITE setModifiers)
public:
GraphicsView(QWidget *parent=nullptr): QGraphicsView(parent){
setScene(new QGraphicsScene);
setModifiers(Qt::ControlModifier);
auto item = scene()->addPixmap(create_image(QSize(100, 100)));
item->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
item->setPos(40, 40);
fitInView(QRectF(0, 0, 640, 320),Qt::KeepAspectRatio);
resize(640, 480);
}
void setModifiers(const Qt::KeyboardModifiers &modifiers){
m_modifiers = modifiers;
}
Qt::KeyboardModifiers modifiers() const{
return m_modifiers;
}
signals:
void pixelChanged(const QPoint &);
protected:
void mousePressEvent(QMouseEvent *event) override{
if(QGraphicsPixmapItem *item = qgraphicsitem_cast<QGraphicsPixmapItem *>(itemAt(event->pos()))){
QPointF p = item->mapFromScene(mapToScene(event->pos()));
QPoint pixel_pos = p.toPoint();
emit pixelChanged(pixel_pos);
}
QGraphicsView::mousePressEvent(event);
}
void wheelEvent(QWheelEvent *event) override{
if(event->modifiers() == m_modifiers){
double angle = event->orientation() == Qt::Vertical ? event->angleDelta().y(): event->angleDelta().x();
double factor = qPow(base, angle);
applyZoom(factor, event->pos());
}
}
private:
void applyZoom(double factor, const QPoint & fixedViewPos)
{
QPointF fixedScenePos = mapToScene(fixedViewPos);
centerOn(fixedScenePos);
scale(factor, factor);
QPointF delta = mapToScene(fixedViewPos) - mapToScene(viewport()->rect().center());
centerOn(fixedScenePos - delta);
}
Qt::KeyboardModifiers m_modifiers;
const double base = 1.0015;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GraphicsView *view = new GraphicsView;
QLabel *label = new QLabel;
QObject::connect(view, &GraphicsView::pixelChanged, label, [label](const QPoint & p){
label->setText(QString("(%1, %2)").arg(p.x()).arg(p.y()));
});
label->setAlignment(Qt::AlignCenter);
QWidget w;
QVBoxLayout *lay = new QVBoxLayout(&w);
lay->addWidget(view);
lay->addWidget(label);
w.show();
return a.exec();
}
#include "main.moc"
It may be better to use a custom graphics scene subclassed from QGraphicsScene as this makes extracting the necessary coordinates much simpler. The only snag is you have to have the QGraphicsPixmapItem::pos available in the custom QGraphicsScene class - I have included a full working example which uses Graphics_view_zoom.h and Graphics_view_zoom.cpp from the linked question. The position of the QGraphicsPixmapItem is passed to a member of the QGraphicsScene subclass, Frame, in order to make the necessary correction.
#include <QPixmap>
#include <QGraphicsPixmapItem>
#include <QGraphicsTextItem>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <qfont.h>
#include "Graphics_view_zoom.h"
class Frame : public QGraphicsScene {
Q_OBJECT
public:
QGraphicsTextItem * coords;
QPointF pic_tl;
Frame::Frame(QWidget* parent)
: QGraphicsScene(parent) {
coords = new QGraphicsTextItem();
coords->setZValue(1);
coords->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
addItem(coords);
}
void Frame::tl(QPointF p) {
pic_tl = p;
}
protected:
void Frame::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
QPointF pos = event->scenePos();
coords->setPlainText("(" + QString("%1").arg(int(pos.x() - pic_tl.x())) + ", "
+ QString("%1").arg(int(pos.y() - pic_tl.y())) + ")");
coords->setPos(pos);
coords->adjustSize();
}
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QMainWindow* main = new QMainWindow();
QGraphicsView* GraphicsView = new QGraphicsView(main);
Graphics_view_zoom* view_wrapper = new Graphics_view_zoom(GraphicsView);
Frame* frame = new Frame(main);
QGraphicsPixmapItem* pixmapItem = new QGraphicsPixmapItem();
frame->addItem(pixmapItem);
GraphicsView->setScene(frame);
// Loads a 497x326 pixel test image
pixmapItem->setPixmap(QPixmap(":/StackOverflow/test"));
// small offset to ensure it works for pictures which are not at
// (0,0). Larger offsets produce the same result but require manual
// adjustments of the view, I have neglected those for brevity as
// they are not in the scope of the question.
pixmapItem->setPos(-20, 20);
frame->tl(pixmapItem->pos());
GraphicsView->fitInView(QRectF(0, 0, 640, 320), Qt::KeepAspectRatio);
GraphicsView->centerOn(pixmapItem->pos());
main->resize(1920, 1080);
main->show();
GraphicsView->resize(main->width(), main->height());
return a.exec();
}
This will display the image coordinates of the pixel under the mouse relative to (0,0) at the top left corner.
The mouse is not visible in these screenshots but in the first it is in exactly the upper left corner and the second it is in exactly the lower right corner. If these coordinates are needed inside the Graphics_view_zoom object then you simply have to scope the Frame instance appropriately, or pass the value as needed.
Note - the exact coordinates displayed may not precisely represent the position of the mouse in this example since they are cast to ints for demonstration, but the floating point values can be easily accessed since QGraphicsSceneMoveEvent::scenePos() returns a QPointF. Additionally, note that in running this demonstration there may be some (hopefully very small) variation on where the mouse appears to be relative to it's 'actual' position - I recommend using Qt::CrossCursor to allay this. For example on my system the default cursor is off by about a pixel for certain areas on my smaller display, this is also affected by the zoom level - higher zoom will produce more accurate results, less zoom will be less accurate.
I have QGraphicsView, QGraphicsScene and QGraphicsRectItem.
QGraphicsRectItem in the QGraphicsScene and the last one in the QGraphicsView. I want to move QGraphicsRectItem with mouse by clicking on it only! But in my implementation it moves if I click on any position on my QGraphicsScene. Whether it is my QGraphicsRectItem or some other place. And the second issue. The item has been moved to the center of the scene. Clicking on it again it starts to move from the home location.
void Steer::mousePressEvent(QMouseEvent *click)
{
offset = click->pos();
}
void Steer::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
p1->setPos(event->localPos() - offset); //p1 movable item
}
}
What do I do wrong?
UPDATE:
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Steer w;
w.show();
return a.exec();
}
widget.h
#ifndef STEER_H
#define STEER_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QPoint>
#include <QGraphicsRectItem>
class Steer : public QGraphicsView
{
Q_OBJECT
private:
QGraphicsScene *scene;
QGraphicsRectItem *p1;
QPoint offset;
public:
explicit Steer(QGraphicsView *parent = 0);
~Steer(){}
public slots:
void mousePressEvent(QMouseEvent * click);
void mouseMoveEvent(QMouseEvent * event);
};
#endif // STEER_H
widget.cpp
#include "widget.h"
#include <QBrush>
Steer::Steer(QGraphicsView *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene;
p1 = new QGraphicsRectItem;
//add player
p1->setRect(760, 160, 10, 80);
//add scene
scene->setSceneRect(0, 0, 800, 400);
//add moveable item
scene->addItem(p1);
//set scene
this->setScene(scene);
this->show();
}
void Steer::mousePressEvent(QMouseEvent *click)
{
offset = click->pos();
}
void Steer::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
p1->setPos(event->localPos() - offset);
}
}
I'd try a different approach that is a little easier to understand:
#include <QtWidgets>
class Steer : public QGraphicsView
{
public:
Steer()
{
scene = new QGraphicsScene;
p1 = new QGraphicsRectItem;
//add player
p1->setRect(0, 0, 10, 80);
p1->setX(760);
p1->setY(160);
//add scene
scene->setSceneRect(0, 0, 800, 400);
//add moveable item
scene->addItem(p1);
//set scene
this->setScene(scene);
this->show();
}
protected:
void mousePressEvent(QMouseEvent * click)
{
if (p1->contains(p1->mapFromScene(click->localPos()))) {
lastMousePos = click->pos();
} else {
lastMousePos = QPoint(-1, -1);
}
}
void mouseMoveEvent(QMouseEvent * event)
{
if(!(event->buttons() & Qt::LeftButton)) {
return;
}
if (lastMousePos == QPoint(-1, -1)) {
return;
}
p1->setPos(p1->pos() + (event->localPos() - lastMousePos));
lastMousePos = event->pos();
}
private:
QGraphicsScene *scene;
QGraphicsRectItem *p1;
QPoint lastMousePos;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Steer w;
w.show();
return a.exec();
}
There are a few things to point out here:
Don't use setRect() to set the position of a QGraphicsRectItem. It doesn't work the way you think it might. Always use setPos() to change the position of an item.
Rename offset to something more descriptive. I chose lastMousePos. Instead of just updating it once when the mouse is pressed, also update it whenever the mouse is moved. Then, it's simply a matter of getting the difference between the two points and adding that to the position of the item.
Check if the mouse is actually over the item before reacting to move events. If the mouse isn't over the item, you need some way of knowing that, hence the QPoint(-1, -1). You may want to use a separate boolean flag for this purpose. This solves the problem that you saw, where it was possible to click anywhere in the scene to get the item to move.
Also, note the mapFromScene() call: the contains() function works in local coordinates, so we must map the mouse position which is in scene coordinates before testing if it's over the item.
The event functions are not slots, they're virtual, protected functions.
You could also consider handling these events in the items themselves. You don't need to do it from within QGraphicsView, especially if you have more than one of these items that need to be dragged with the mouse.
I have a label in my GUI that displays an image as a QPixmap. I want to be able to draw a continuous line on my image by simply clicking anywhere on the image to select the start point and then make a second point somewhere else by clicking on a other part of the image. The two points should connect immediately after placing the second point and I want to be able to continue that same line by placing more points on the image.
While I know how to draw something on a QPixmap, the mouse event which I need to use to get the coordinates for the points is really confusing me as I’m still fairly new to Qt.
Any examples for a solution would be much appreciated.
I suggest you to use QGraphicsView for this purpose. Use my code snippet which works perfectly.
Subclass QGraphicsScene:
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QPoint>
#include <QMouseEvent>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
signals:
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
public slots:
private:
QPolygon pol;
};
#endif // GRAPHICSSCENE_H
.cpp file:
#include "graphicsscene.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
addPixmap(QPixmap("G:/2/qt.jpg"));//your pixmap here
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//qDebug() << "in";
if (mouseEvent->button() == Qt::LeftButton)
{
QPoint pos = mouseEvent->scenePos().toPoint();
pol.append(pos);
if(pol.size() > 1)
{
QPainterPath myPath;
myPath.addPolygon(pol);
addPath(myPath,QPen(Qt::red,2));
}
}
}
Usage:
#include "graphicsscene.h"
//...
GraphicsScene *scene = new GraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
Result:
If you want save new pixmap (or just get pixmap) as image, use this code:
QPixmap pixmap(ui->graphicsView->scene()->sceneRect().size().toSize());
QString filename("example.jpg");
QPainter painter( &pixmap );
painter.setRenderHint(QPainter::Antialiasing);
ui->graphicsView->scene()->render( &painter, pixmap.rect(),pixmap.rect(), Qt::KeepAspectRatio );
painter.end();
pixmap.save(filename);
With render() you can also grab different areas of your scene.
But this code can be better: we create and paint same polygon. If we can remember last painted point, then we can paint line by line (begin of line is end of last line). In this case we don't need all points, we need just last point.
As I promised(Code improvement):just provide additional variable QPoint last; instead of QPolygon pol; and use next code:
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//qDebug() << "in";
if (mouseEvent->button() == Qt::LeftButton)
{
QPoint pos = mouseEvent->scenePos().toPoint();
if(last.isNull())
{
last = pos;
}
else
{
addLine(QLine(last,pos),QPen(Qt::red,2));
last = pos;
}
}
}
As you can see, you store only last point and paint only last line. User can clicks thousands time and now you not need to store this unnecessary points and do this unnecessary repainting.
My app display a long scientific vertically-scrollable picture (1024 x 99999999... px) as a sequence of QPixmap 1024x128 blocks. This allows me to scroll a picture with minimal CPU-cost by picking needed blocks from a table: block_id = y_coord/128. Also, QPixmap is preferred "pixel container" for fast screen output.
But now I have a stream of new data coming to the application and need the new data to be added and displayed at the bottom of the long picture. Minimal portion: 1024x1 (a line). Also, I would like to display each new line as soon as possible (close to real-time). Each new portion of 128 lines will be "packed" to QPixmap, but until I received enough data I cannot build a whole block.
What approach should I consider for displaying the new data?
This video gives an idea of "adding new lines of data", except in my case the flow goes up: http://www.youtube.com/watch?v=Dy3zyQNK7jM
You can simply, directly, modify the bottom row of QPixmaps and update() the window (if the bottom row is in range).
You might find using a QImage is more efficient for half-baked rows, depending on how quickly you update/repaint.
On contemporary Qt, when using the raster backend, QPixmap offers no benefits compared to QImage. Everything is rendered to a big QImage backing buffer that then gets blitted to the screen. So just use QImage.
You can have a QImage that is 128 pixels high, but you only draw the part of it that was already filled with data. The part without data is either not drawn, or hangs below the visible area of the window, and is thus effectively invisible.
Here is a quick example I put together. I don't know if it is the most efficient, but it shows off the basic idea you are looking at:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QVector>
#include <QGraphicsScene>
#include <QTimerEvent>
#define TILE_HEIGHT 128
#define TILE_WIDTH 1024
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QPixmap generateLine();
public slots:
void timerEvent(QTimerEvent *);
private:
QGraphicsView * m_view;
QGraphicsScene * m_scene;
QVector <QGraphicsPixmapItem *> m_tiles;
QVector <QGraphicsPixmapItem *> m_lineBuffer;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QPixmap>
#include <QtGlobal>
#include <QDateTime>
#include <QTimer>
#include <QPaintEngine>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setFixedWidth(TILE_WIDTH);
this->setCentralWidget(m_view = new QGraphicsView());
m_scene = new QGraphicsScene;
m_view->setScene(m_scene);
QPixmap p(TILE_WIDTH, TILE_HEIGHT);
p.fill(Qt::black);
m_tiles.append(new QGraphicsPixmapItem(p));
m_tiles.last()->setPos(0,0);
m_scene->addItem(m_tiles.last());
qsrand(QDateTime::currentMSecsSinceEpoch());
this->startTimer(0);
}
MainWindow::~MainWindow()
{
}
void MainWindow::timerEvent(QTimerEvent *)
{
// if your generated data is on another thread, you may want to do some thread
// synchronization with a Mutex and a Mutex Locker so you don't stomp on your
// buffers
// static bool busy = false;
// static int skipCount = 0;
// if(busy)
// {
// skipCount++;
// qDebug() << "Skipped Line count =" << skipCount;
// return;
// }
// busy = true;
// grab a new line
QPixmap linePix = generateLine();
int y = m_tiles.size()*TILE_HEIGHT + m_lineBuffer.size()*1;
// append it to the line buffer
m_lineBuffer.append(new QGraphicsPixmapItem(linePix));
// add it to the scene
m_scene->addItem(m_lineBuffer.last());
m_lineBuffer.last()->setPos(0, y);
// scroll it into view
m_view->ensureVisible(m_lineBuffer.last());
if(m_lineBuffer.size() >= TILE_HEIGHT)
{
// when the line buffer is "full"
// or ready to be made into a tile
// compile all the qpixmaps into a single "tile"
static QRectF source(0,0, TILE_WIDTH, 1);
QPixmap tile(TILE_WIDTH, TILE_HEIGHT);
QPainter painter;
painter.begin(&tile);
for(int i = 0; i < m_lineBuffer.size(); i++)
{
painter.drawPixmap(QRectF(0, i, TILE_WIDTH, 1),
m_lineBuffer.at(i)->pixmap(),
source);
}
painter.end();
// add it into the tiles list
m_tiles.append(new QGraphicsPixmapItem(tile));
// add it to the scene
m_tiles.last()->setPos(0, (m_tiles.size() - 1)*TILE_HEIGHT);
m_scene->addItem(m_tiles.last());
// scroll it into view
m_view->ensureVisible(m_tiles.last());
// Clean up the line buffer
foreach(QGraphicsPixmapItem * pi, m_lineBuffer)
{
m_scene->removeItem(pi);
delete pi;
}
m_lineBuffer.clear();
}
// busy = false;
}
QPixmap MainWindow::generateLine()
{
// create a random pixmap of TILE_WIDTH x 1
static int img_width = TILE_WIDTH;
QImage img(img_width,1, QImage::Format_RGB16);
for(int i = 0; i< img_width; i++)
{
img.setPixel(i, 0, qrand()%65536);
}
return QPixmap::fromImage(img);
}
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
I'm new here and i can't solve this thing.
I have a QGraphicScene (which at first initializes a Cartesian plane) in which it draw points \ lines with the methods AddLine \ ecc.Everything works.But I want to have a method that delete all "objects" drawn on the scene and reloads the cartesian plane.Any suggestion?
Here you can find my classes:
class draw: public QWidget
{
Q_OBJECT
private:
QGraphicsScene scene;
int x1;
int y1;
int x2;
int y2;
public:
disegna (QWidget *parent = 0);
void setdot(QString,QString);
void setsegment(QString,QString,QString,QString);
~disegna(){}
};
draw::draw(QWidget *parent) : QWidget(parent) {
scene.setBackgroundBrush(Qt::white);
scene.addRect(QRectF(0,0, 600, 200));
scene.addLine(0, 100 ,600, 100);
scene.addLine(300, 0 ,300, 200);
scene.addEllipse(297.5,97.5,5,5,QPen(), QBrush(Qt::red));
int i=0;
for (int a=0;a<120;a++) {
scene.addLine(i+5, 98 ,i+5, 102);
i=i+5;
}
int j=0;
for (int a=0;a<40;a++) {
scene.addLine(298,j+5,302,j+5);
j=j+5;
}
QGraphicsView * view = new QGraphicsView(&scene,this);
view->show();
}
void draw::setdot(QString x1,QString y1){
scene.addEllipse(x1.toInt()+298, 98-y1.toInt(),4,4,QPen(), QBrush(Qt::blue));
}
void draw::setsegment(QString x1,QString y1,QString x2,QString y2) {
scene.addLine(x1.toInt()+300, 100-y1.toInt(),x2.toInt()+300, 100-y2.toInt(),QPen());
scene.addEllipse(x1.toInt()+298, 100-y1.toInt(),4,4,QPen(), QBrush(Qt::blue));
scene.addEllipse(x2.toInt()+298, 100-y2.toInt(),4,4,QPen(), QBrush(Qt::blue));
}
For cleaning the scene, did you tried the clear method ?
If you want to clear and redraw, I suggest you to move the drawing code from your constructor to a method.
Then you can call clear then your drawing method.
Hope it helps