Fit QWidget to QGraphicsScene - qt

I need to make the QWidget the same size as parent QGraphicsScene even on resizing the dock widget.
ui->w1_move->setParent(NULL);
view1 = new QGraphicsView(this);
scene1 = new QGraphicsScene(this);
view1->setScene(scene1);
QGraphicsProxyWidget *pwig = scene1->addWidget(ui->w1_move);
ui->dockWidgetContents->layout()->addWidget(view1);
EDIT:
Complete code here: github

i added a custom resize:
void MyGraphicsView::resizeEvent(QResizeEvent *event)
{
scn->setSceneRect(0,0,event->size().width(),event->size().height());
wig->resize(event->size());
QGraphicsView::resizeEvent(event);
}

Related

Resize QGraphicsPixmapItem

I need to place a clickable picture on the QGraphicsScene. This is how I did it:
class myGraphicsPixmapItem: public QGraphicsPixmapItem
{
public:
myGraphicsPixmapItem() { }
~myGraphicsPixmapItem() {}
void mousePressEvent(QGraphicsSceneMouseEvent* event)
{
qDebug() << "Clicked!";
}
};
QPixmap pic;
pic.load(":/img/pic.png");
QGraphicsScene* scene = new QGraphicsScene();
view = new GraphicsView(scene);
myGraphicsPixmapItem* pixmapItem = new myGraphicsPixmapItem;
pixmapItem->setPixmap(pic);
scene->addItem(pixmapItem);
But I don't know how to make it smaller. Please tell, how to make smaller QGraphicsPixmapItem or is there another way to place a clickable and resizable picture on the QGraphicsScene?
you should use scaled QPixmap::scaled instead of QGraphicsPixmapItem .
sample code :
QPixmap bgPixmap(fileName);
QPixmap scaled = bgPixmap.scaled(QSize(64, 64));
and use scaled as your QPixmap .

automatic resize using qgraphicsview with qgraphicsscene and qopenglwidget

I'm trying to use QGraphicsView with QOpenGLWidget in a MainWindow:
Exactly:
My QMainWindow contains a few layouts and widget. Also a QGraphicsView (class graphicsView inherits from QGraphicsView).
In a "second" step i create a QGraphicsScene and add a openglwidget (class OpenGLControl inherits from QOpenGLWidget). In OpenGLControl I only set the backround for testing my application (glClearColor ...).
Now my request, when I change the size of mainWindow, i want to change the size of QGraphicsView, QGraphicsScene and QOpenGLWidget, too. I try a resizeEvent in graphicsView, but it didn't work.
Implementation of mainwindow (.cpp):
#include "mainwindow.h"
#include "openglcontrol.h"
#include "graphicsview.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createLayout();
createScene();
}
void MainWindow::createLayout()
{
view = new graphicsView;
kosGrid = new QGridLayout;
xKoord = new QLineEdit;
yKoord = new QLineEdit;
zKoord = new QLineEdit;
drawButton = new QPushButton("Draw Point");
clearButton = new QPushButton("Clear");
KoordLabel = new QLabel("Koordinaten");
xLabel = new QLabel("X:");
yLabel = new QLabel("Y: ");
zLabel = new QLabel("Z: ");
controlBox = new QVBoxLayout;
controlBox->addStretch(1);
controlBox->addWidget(KoordLabel);
xhBox = new QHBoxLayout;
xhBox->addWidget(xLabel);
xhBox->addWidget(xKoord);
controlBox->addLayout(xhBox);
yhBox = new QHBoxLayout;
yhBox->addWidget(yLabel);
yhBox->addWidget(yKoord);
controlBox->addLayout(yhBox);
zhBox = new QHBoxLayout;
zhBox->addWidget(zLabel);
zhBox->addWidget(zKoord);
controlBox->addLayout(zhBox);
controlBox->addWidget(drawButton);
controlBox->addWidget(clearButton);
xKoord->setFixedWidth(50);
yKoord->setFixedWidth(50);
zKoord->setFixedWidth(50);
drawButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
xKoord->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
yKoord->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
zKoord->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
hBox = new QHBoxLayout;
hBox->addLayout(controlBox);
hBox->addWidget(view);
mainLayout = new QWidget;
mainLayout->setLayout(hBox);
setCentralWidget(mainLayout);
}
void MainWindow::createScene()
{
scene = new QGraphicsScene(this);
qsurface = new QSurfaceFormat;
qsurface->setRenderableType(QSurfaceFormat::OpenGL);
qsurface->setProfile(QSurfaceFormat::CoreProfile);
qsurface->setVersion(3,3);
oglwidget = new OpenGLControl;
oglwidget->setFormat(*qsurface);
oglwidget->
//oglwidget->resize(10,10);
scene->addWidget(oglwidget);
view->setScene(scene);
}
and my attempt for the resizeEvent:
void graphicsView::resizeEvent(QResizeEvent *event)
{
if(scene())
scene()->setSceneRect(QRect(QPoint(0,0), event->size()));
QGraphicsView::resizeEvent(event);
}
at last a screenshot of the (resized) mainWindow (the QOpenGLWidget (blue background) is smaller than the QGraphicsView)
mainWindow with QOpenGLWidget
Is there a SIGNAL I can use ? Or can anybody help me on another way ?
Thank You

Reducing the size of a QMdiSubWindow holding a QLabel with QPixmap

The question statement says it all. I am able to enlarge the QMdiSubWindow (and the pixmap scales UP, i.e. enlarges, appropriately) but I am not able to reduce its size (i.e. scale the image DOWN or shrink). When trying to scale down though, the border handles of the QMdiSubWindow remain rigidly fixed.
The code I am using is as follows:
class PixmapWidget : public QLabel
{
Q_OBJECT
public:
PixmapWidget():
QLabel()
{
_pixmap = QPixmap("path\\to\\image.jpg");
setPixmap(_pixmap);
_layout = new QHBoxLayout();
_layout->setSizeConstraint(QLayout::SetNoConstraint);
setLayout(_layout);
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
}
void resizeEvent(QResizeEvent * event)
{
int width = event->size().width();
int height = event->size().height();
setPixmap(_pixmap.scaled(width,height));
QLabel::resizeEvent(event);
}
protected:
QPixmap _pixmap;
QHBoxLayout* _layout;
};
int main()
{
QMainWindow mainWindow;
QHBoxLayout* layout = new QHBoxLayout();
QMdiArea* mdiArea = new QMdiArea();
mainWindow.setCentralWidget(mdiArea);
mainWindow.centralWidget()->setLayout(layout);
layout->setSizeConstraint(QLayout::SetNoConstraint);
QMdiSubWindow* mdiSubWindow = new QMdiSubWindow();
layout->addWidget(mdiSubWindow);
mdiSubWindow->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
mdiSubWindow->layout()->setSizeConstraint(QLayout::SetNoConstraint);
DMSQt::PixmapWidget pixmapWidget;
mdiSubWindow->layout()->addWidget(&pixmapWidget);
mainWindow.show();
qapp.exec();
}
Alright, I found the problem. Apparently, I need to set a minimum size on my text label:
Adding:
setMinimumSize(_pixmap.width(), _pixmap.height());
solved the problem.

Paint a QGraphicsItem to a QImage without needing a scene/view

So here's what I'm trying to do - Using a custom QGraphicsItem, I have my QPainter setup to paint into a QImage, which I then save to a file (or just keep the QImage in memory until I need it).
The issue I've found is that QGraphicsItem::paint() is only called if the QGraphcsItem belongs to a scene, the scene belongs to a view, AND the view and scene are not hidden.
Here's the code outside my project for testing purposes:
MyQGfx Class
void MyQGfx::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
qDebug() << "begin of paint function";
QRectF rec = boundingRect();
QImage image(boundingRect().size().toSize(),
QImage::Format_ARGB32_Premultiplied);
image.fill(0);
// construct a dedicated offline painter for this image
QPainter imagePainter(&image);
imagePainter.translate(-boundingRect().topLeft());
// paint the item using imagePainter
imagePainter.setPen(Qt::blue);
imagePainter.setBrush(Qt::green);
imagePainter.drawEllipse(-50, -50, 100, 100);
imagePainter.end();
if(image.save("C://plot.jpg"))
{
qDebug() << "written";
}
else {
qDebug() << "not written";
}
}
MainWindow Class
....
QGraphicsView* view = new QGraphicsView(this);
QGraphicsScene* scene = new QGraphicsScene(this);
view->setScene(scene);
MyQGfx* gfx = new MyQGfx();
scene->addItem(gfx);
gfx->update();
....
This all works fine, but I don't want a view/scene necessary, as it would be displayed on the mainwindow - is there any way around this?
Can't you just create a custom method accepting a QPainter, one painting on a QImage and one on your item?

QLabel does not display in QWidget

I have the following hierarchy in my Qt Application:
QMainWindow > QWidget (centralWidget) > QWidget (subclassed) > QLabel
Initialization code in my QMainWindow code:
centralWidget = new QWidget();
centralWidget->setGeometry(0,0,width,height);
chatWidget=new ChatWidget(this); // the subclassed QWidget
setCentralWidget(centralWidget);
In my subclassed QWidget initialization (which happens at the same time than the Qt App initialization) I have the following code:
ChatWidget::ChatWidget(QWidget *parent):QWidget(parent)
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 1"); <-- Is properly Display
}
void ChatWidget::displayChatAfterButtonPressed()
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 2"); <-- Does NOT appear
}
When the QLabel is added from the class initialization then the message is well displayed in the widget.
However when I launch the same code after a button pressed (via a function in the same QWidget subclass), then the text does not appear on screen.
I don't want to use layouts as I need to exactly position my labels.
Tried to repaint, but didn't help neither.
How can I properly and dynamically display a label after the initialization is done ?
Widgets when they are visible for the first time call to be visible to their children, but since you are creating it afterwards they probably are not calling that method, a possible solution is to call the show method.
void ChatWidget::displayChatAfterButtonPressed()
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 2");
lbl->show();
}
comment: it seems strange to me that the QMainWindow you set a central widget and then create the chatWidget as a parent to the QMainWindow, it is generally not recommended to add children to the QMainWindow because it has a given structure, what should be done is to place it inside the centralwidget.
We need to show the label created by button click, cause centralwidget was already painted.
Here is a working example, I added this as answer also I noticed better adding chatWidget as child to centralWidget where in your original code its added to the UI .. this is your choice.
Mainwindow:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//
ui->setupUi(this);
centralWidget = new QWidget();
centralWidget->setGeometry(width,height);
chatWidget=new ChatWidget(centralWidget); // the subclassed QWidget
setCentralWidget(centralWidget);
// added testing
QPushButton *btn = new QPushButton("MyButton",centralWidget);
btn->setGeometry(100,100,100,100);
btn->setMaximumSize(100,100);
connect(btn,&QPushButton::clicked, chatWidget, &ChatWidget::displayChatAfterButtonPressed);
}
and chatWidget:
ChatWidget::ChatWidget(QWidget *parent):QWidget(parent)
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 1");
}
void ChatWidget::displayChatAfterButtonPressed()
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 2");
lbl->show();
}

Resources