Change Pixmap Position in QGraphicsScene/View in Qt5 - c++

I have a normal QGraphicsView/QGraphicsScene and all I want to do is load a QPixmap (.png) to the graphic and manually set the position of that QPixmap image. I've found solutions but they don't work on Qt5. Any ideas on how to achieve this on Qt5? Thank you.
QGraphicsScene* scene = new QGraphicsScene(QRect(0, 0, 600, 400));
QPixmap Pix(":/test.gif");
// doesnt work
//Pix.setGeometry(QRect(-30, 40, 260, 200));
QGraphicsPixmapItem *item1 = scene->addPixmap(Pix);
// doesnt work
//item1->setPos(-25, 45);
//scene->addPixmap(Pix)->setPos(0,0);
//QGraphicsView* view = new QGraphicsView(ui->centralWidget);
QGraphicsView* view = new QGraphicsView(this);
view->setScene(scene);

You can browse the image from your computer and set its position.
Just in case, It helps someone.
QString fileNamez = QFileDialog::getOpenFileName(this,"Open Filezzz","C:/");
QGraphicsPixmapItem *pm = scene->addPixmap( QPixmap(fileNamez) );
pm->setPos(xPos,yPos);

If you use a QGraphicsPixmapItem in your implementation, or indeed any Qt object, you need to include it in the cpp: -
#include <QGraphicsPixmapItem>

Related

Why does QGraphicsRectItem object stay at coordinate (0,0) regardless of the coordinates provided explicitly?

I'm new to Qt5 and tried all the solutions provided online but most of them are from 2011. Am I missing something? It reluctantly stays at (0,0).
File Name: sll.cpp
sll::sll(QWidget *parent) :
QWidget(parent),
ui(new Ui::sll)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QBrush redbrush(Qt::red);
QPen blackPen(Qt::black);
blackPen.setWidth(6);
rect = scene->addRect(500,500,100,50,blackPen,redbrush);
rect->setPos(-100,-100);
}
The relevant code from sll.h
private:
Ui::sll *ui;
//Graphics
QGraphicsScene *scene;
QGraphicsRectItem *rect;
};
The output:
Cause
QGraphicsView aligns the scene by default in the middle.
A graphics scene has dimensions, e.g. 1000x800 px. QGraphicsView::setSceneRect or QGraphicsScene::setSceneRect, which is equivalent, sets these dimensions. When one of those methods is not used explicitly, Qt determines the dimensions automatically, based on the content's geometry. This leads to the observed from you unintuitive visual behaviour.
Solution
Add
ui->graphicsView->setAlignment(Qt::AlignLeft | Qt::AlignTop);
ui->graphicsView->setSceneRect(-200, -200, 800, 800);
after
ui->graphicsView->setScene(scene);
Tip
To avoid possible surprises, do not use the x and y of the rectangle to set its position, but the setPos method of the corresponding QGraphicsItem. In other words
const QRectF &rect(500, 500, 100, 50);
scene->addRect(rect, blackPen, redbrush);
and
rect = scene->addRect(0, 0, 100, 50, blackPen, redbrush);
rect->setPos(500, 500);
might look as they do the same thing, but it is not like that. Use the second version.

Qt: setScreenRect = segfault

Basically I need something to draw on it,
that is simple to use with designer (for example inherit from QWidget and override of paintEvent require some efforts to use it in Qt designer).
So I add QGraphicsView to .ui file and write such code in button method:
QGraphicsScene *scene = ui->graphicsView->scene();
const QRectF scene_r = ui->graphicsView->sceneRect();
//here I see that scene_r == {0, 0, 0, 0};
then I add such code:
const QSize draw_size = ui->graphicsView->size();
//draw_size == {100, 100}
scene->setSceneRect(QRectF(0, 0, draw_size.width(), draw_size.height()));
and got segfault on line scene->setSceneRect what is going on here?
Actually I want right such code:
//scene size == draw_size
QPixmap pix(draw_size.width(), draw_size.height());
//fill pix with external program
scene->addPixmap(map_pix);
is it possible?
I never before work with QGraphicsView.
The reason you get a segfault is because you haven't set any scene for the view, hence the scene() will return a null pointer.
QGraphicsScene scene;
QGraphicsView view(&scene);
view.show();
Alternative is to create the view without a scene then add it later:
void setScene(QGraphicsScene *scene)
Reference:
http://doc.qt.io/qt-5/qgraphicsview.html

Want to place a QPushButton onto a GraphicView... but no button is displayed?

Want to place a QPushButton onto a GraphicView... but no button is displayed?
I am creating a new window for each plot. On each plot I want to put some buttons for functionality. I don't know if I should put the view on another type of window... if so how? thx
QGraphicsScene *scene = new QGraphicsScene();
QPixmap image;
image.load(fileInfo.filePath(), 0);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
QGraphicsView *view = new QGraphicsView();
view->setScene(scene);
view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "My Plot"));
view->resize(1000, 1000);
view->show();
QPushButton *m_button6 = new QPushButton("ok", view);
m_button6->setGeometry(QRect(QPoint(50, 50), QSize(50, 50)));
connect(m_button6, SIGNAL(released()), this, SLOT(handleButton5()));
wrap it around QGraphicsWidget.
QGraphicsWidget *pBtn = scene->addWidget(m_button6);
scene->addItem(pBtn)
oh! don't forget:
m_button6->show();
widgets are hidden by default!

C++ Qt QGraphicsView: Remembering the position of scrollbars after reload

I have the following problem with scrollbars in graphics view. My application takes a PDF file and creates (in some way) a QImage out of it. The QImage is then converted to QPixmap, which is used to create a QGraphicsScene and from the QGraphicsScene I create a QGraphicsView. The QGraphicsView is added to the central widget and displayed.
The code looks approximately like this
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
In the application, the view gets updated every time the PDF file changes. Now the problem is that the thing, which is in the PDF file, can also change in size and the scrollbars get messed up. I want the scrollbars after update to be in a position, such that I will see the same part of the PDF file as I saw before the update.
Can you give me some advice on how to accomplish this? I've searched for this issue, but nothing has worked in my case so far (I could have also be doing something wrong).
Thank you for your answers!
Before changing the view's contents remember scrollbar positions using view->horizontalScrollBar()->value() and view->verticalScrollBar()->value().
After changing reset previous values using setValue(int).
It's bad that you didn't provide any code that you have tried to use to accomplish this so I can't tell you what were you doing wrong.
Here is an example:
int pos_x = view->horizontalScrollBar()->value();
int pos_y = view->verticalScrollBar()->value();
pixmap_item->setPixmap(QPixmap::fromImage(new_image));
view->horizontalScrollBar()->setValue(pos_x);
view->verticalScrollBar()->setValue(pos_y);
Here pixmap_item is the stored result of scene->addPixmap(pixmap). It has QGraphicsPixmapItem* type.
I've changed the code, but it behaves strangely.
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
int hsbv = -1;
int vsbv = -1;
if (view != NULL) {
hsbv = view->horizontalScrollBar()->value();
vsbv = view->verticalScrollBar()->value();
}
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
view->horizontalScrollBar()->setVealue(hsbv);
view->verticalScrollBar()->setValue(vsbv);
When I print out the view->horizontalScrollBar()->value() at the end, I always get 83 and I get 479 for the view->verticalScrollBar()->value(), which is very wierd, but when I print hsbv and vsbv I get reasonable numbers.

Create a window in qt with shape from an image

Can some one explain me how to make a window in qt according to the shape of some object in an image , for example i have an image of a tree , using that i need to create a window in the shape of a tree ..
After a long search , myself found a good solution , check out this ..
#include <QtGui>
class myMainWindow:public QMainWindow
{
public:
myMainWindow():QMainWindow()
{
setMask((new QPixmap("saturn.png"))->mask());
QPalette* palette = new QPalette();
palette->setBrush(QPalette::Background,QBrush(QPixmap("saturn.png")));
setPalette(*palette);
setWindowFlags(Qt::FramelessWindowHint);
QWidget *centralWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout();
centralWidget->setLayout(layout);
QPushButton* button1 = new QPushButton("Button 1");
button1->setFixedSize(80,50);
layout->addWidget(button1,0,0);
setCentralWidget(centralWidget);
};
~myMainWindow(){};
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
myMainWindow *window = new myMainWindow();
window->resize(600, 316);
window->show();
return app.exec();
}
Here is a recipe for making a widget with a semi-transparent background colour. Just expand from there by making the background fully transparent, then display the tree image on top of that as a background image. Note that the widget will still behave like a rectangular widget in regards to laying out its child elements, so you probably need to deal with this using some custom layout inside the tree shape.
Start from the docs for QWidget::setMask. It has a version which takes a QBitmap and one that takes a QRegion. This is the fundamental function in getting a transparent widget. The toolkit also includes a clock example using the QRegion version -- I suspect a bitmap is just as easy though.