QGraphicsScene::clear doesn't change sceneRect - c++

I have a QGraphicsScene "scene" and QGraphicsView "graphicsView".
I have a drawing method. When I need redraw all the graphics, I call this method. Everything is OK. But I realized that scene->clear() doesn't change the sceneRect.
Also I tried:
graphicsView->items().clear();
scene->clear();
graphicsView->viewport()->update();
After that, if I get the sceneRect by
QRectF bound = scene->sceneRect();
qDebug() << bound.width();
qDebug() << bound.height();
I expect the bound.width and bound.height to be '0'. But they aren't. I see the previous values everytime. How to clear sceneRect when I clear the scene itself?
It gives some problems that sceneRect remains the same, while using graphicsView->fitInView() method.I use following code:
QRectF bounds = scene->sceneRect();
bounds.setWidth(bounds.width()*1.007); // to give some margins
bounds.setHeight(bounds.height()); // same as above
graphicsView->fitInView(bounds);
Although I completely cleared the scene and added only one rather small rectangle, the rectangle didn't fit into view because of sceneRect remains too big.
I hope I could explain my problem.

From the Qt Docs (emphasis mine):
This property holds the scene rectangle; the bounding rectangle of the scene
The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.
If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).
Therefore, the only way to shrink the sceneRect is to use setSceneRect.

The better question is why do you need to set scene rectangle? In case you have a smaller scene don't set it. Instead add items to the scene and fit in view based on items bounding rectangle as in my example below:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsRectItem>
#include <QPointF>
#include <QDebug>
#include <qglobal.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
_scene = new QGraphicsScene(this);
ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->graphicsView->setScene(_scene);
connect(ui->button, SIGNAL(released()), this, SLOT(_handleRelease()));
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::_random(int min, int max)
{
return qrand() % ((max + 1) - min) + min;
}
void MainWindow::_handleRelease()
{
_scene->clear();
QGraphicsRectItem* pRect1 = _scene->addRect(0, 0, _random(50,100), _random(50,100));
QGraphicsRectItem* pRect2 = _scene->addRect(0, 0, _random(20,50), _random(20,50));
pRect1->setPos(QPoint(40,40));
pRect2->setPos(QPoint(20,20));
ui->graphicsView->fitInView(_scene->itemsBoundingRect(),Qt::KeepAspectRatio);
}
In case you have a large scene with hundreds of items this approach will be slow because:
If the scene rect is unset, QGraphicsScene will use the bounding area
of all items, as returned by itemsBoundingRect(), as the scene rect.
However, itemsBoundingRect() is a relatively time consuming function,
as it operates by collecting positional information for every item on
the scene. Because of this, you should always set the scene rect when
operating on large scenes.

Related

Setting minimum width of QPushButton while keeping default minimumSizeHint behavior

In my application, I need all QPushButtons to have a width of at least 150px, but bigger if needed.
To do so, I am using a global stylesheet (which contains many other properties) with this simple constraint :
QPushButton {
min-width: 150px;
}
The thing is, I also want buttons with a text that doesn't fit inside 150px to be unable to shrink to a width below which the whole text wouldn't be displayed.
This is supposed to be the normal behavior for a QPushButton, but the problem is that, as explained in the documentation for minimumSizeHint() :
QLayout will never resize a widget to a size smaller than the minimum size hint unless minimumSize() is set or the size policy is set to QSizePolicy::Ignore. If minimumSize() is set, the minimum size hint will be ignored.
This leads to some cases where buttons with long texts are displayed at the right size at startup, but when shrinking the window, the button gets too small to display all the text.
I have prepared a simple example that shows this behavior :
#include <QWidget>
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *w = new QWidget();
QLayout* layout = new QHBoxLayout(w);
QPushButton* buttonA = new QPushButton("A", w);
QPushButton* buttonB = new QPushButton("B", w);
buttonB->setStyleSheet("min-width: 150px");
QPushButton* buttonC = new QPushButton("Long long text that doesn't fit in 150px", w);
QPushButton* buttonD = new QPushButton("Very very very long text that doesn't fit in 150px", w);
buttonD->setStyleSheet("min-width: 150px");
layout->addWidget(buttonA);
layout->addWidget(buttonB);
layout->addWidget(buttonC);
layout->addWidget(buttonD);
w->show();
return a.exec();
}
I thought about using dynamic properties to set the minimum width constraint only to buttons that have a base width < 150px, but this doesn't seem to be doable.
Is there a way to do what I want with stylesheets, or do I have to subclass QPushButton and override the minimumSizeHint() method (which I'd like to avoid as I would have to replace a lot of buttons in my app)?
It would be better if you sub-class QPushButton. But there are 2 work around for this problem:
You can strip the long text to specific numbers of characters and use setToolTip(QString) function to show full text when mouse will enter the button.
You can override parent widget resizeEvent and can check the widths and go for approach number 1 if the size is getting really small.
Use Elide Text example to get idea about elide for QPushButton. Not sure if this will work.
I haven't been able to find a way to do this using only stylesheets, but here is the solution I came up with by subclassing QPushButton
MyPushButton.h
#include <QPushButton>
class MyPushButton
: public QPushButton
{
Q_OBJECT
Q_PROPERTY(int minWidth READ minWidth WRITE setMinWidth)
public:
MyPushButton(QWidget* parent = nullptr);
virtual ~MyPushButton();
int minWidth() const;
void setMinWidth(int width);
virtual QSize minimumSizeHint() const override;
private:
int _minWidth;
};
MyPushButton.cpp
#include "MyPushButton.h"
MyPushButton::MyPushButton(QWidget* parent)
: QPushButton(parent)
, _minWidth(0)
{
}
MyPushButton::~MyPushButton()
{
}
int MyPushButton::minWidth() const
{
return _minWidth;
}
void MyPushButton::setMinWidth(int width)
{
_minWidth = width;
}
QSize MyPushButton::minimumSizeHint() const
{
// if the minimum width is less than minWidth, set it to minWidth
// else return the default minimumSizeHint
return QSize(qMax(QPushButton::minimumSizeHint().width(), _minWidth),
QPushButton::minimumSizeHint().height());
}
stylesheet.qss
MyPushButton {
qproperty-minWidth: 150;
}
Buttons with a minimumSizeHint width lower than 150px will be forced to that size, bigger ones will keep the default behavior of QPushButton.

Qt setGeometry to negative value doesn't work

Im working with QT and I have a form with a QLabel in a QFrame. I want to set the QLabel's geometry so the bottom part of the QLabel is in the same place of the bottom of the frame. Since the label is longer than the frame, it's y coordinate should be negative.
int pos = ui->imageFrame->height() - ui->imageLabel->pixmap()->height();
ui->imageLabel->setGeometry(0, pos, ui->imageFrame->width(), p.height());
Although when printing the QLabel's geometry, the y coordinate is correct, the label is showing on the upper part of the frame.
Help is much appreciated.
You can set the label's alignment with setAlignment. Here's a working example:
#include <QtWidgets>
#include "MyWidget.h"
MyWidget::MyWidget()
{
setFixedSize(200,200);
QLabel *label = new QLabel;
label->setPixmap(QPixmap("/some/image/file.jpg"));
label->setAlignment(Qt::AlignBottom);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(label);
hbox->setContentsMargins(0,0,0,0);
setLayout(hbox);
}

Fastest way to draw QGraphicItems on an "equivalent" QPixmap

I want to draw colored tiles as background for a QGraphicsscene and provide pan and zoom functionality for the scene using a QGraphicsView. First I used QGraphicItems to draw each tile. Since I have many tiles this was quite a performance problem when panning or zooming but since I do not need to modify any part of the tiles afterwards I switched to generating a QPixmap using the following code:
void plotGrid(){
Plotable::GraphicItems items;
append(items,mParticleFilter.createGridGraphics());
append(items,mParticleFilter.getRoi().mRectangle.createGraphics(greenPen()));
scaleItems(items,1.0,-1.0);
QGraphicsScene scene;
showItemsOnScene(items,&scene);
QRectF boundingRect = scene.itemsBoundingRect();
double cScale = ceil(1920.0/boundingRect.size().width());
QSize size(boundingRect.size().toSize()*cScale);
QPixmap pixmap(size);
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
//p.setRenderHint(QPainter::Antialiasing);
scene.render(&p);
p.end();
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap);
item->setOffset(boundingRect.topLeft()*cScale);
item->scale(1/cScale,1/cScale);
mpView->showOnScene(item);
}
While this solves the zoom and pan problem, the time to generate the pixmap introduces some significant delay, probably because I first create a scene and then render it. Is there a faster way of producing a QPixmap on the fly starting from QGraphicItems ?
Just for completeness an image of the tiles:
So I finally got at least past using an intermediary scene. The following code only relies on a QPainter for rendering the pixmap. My main problem was to get all transformations right. Otherwise it is quite straight forward....
This version halves the processing time to 500ms in my scenario. 450ms are spent on painting the items. If someone has more suggestions for more improvement it would be most welcome (cahnging the resolution does not help much by the way)
void SceneWidget::showAsPixmap(Plotable::GraphicItems const& items){
QRectF boundingRect;
boostForeach(QGraphicsItem* pItem,items) {
boundingRect = boundingRect.united(pItem->boundingRect());
}
QSize size(boundingRect.size().toSize());
double const cMaxRes =1920;
double const scale = cMaxRes/boundingRect.size().width();
QPixmap pixmap(size*scale);
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
//p.setCompositionMode( QPainter::CompositionMode_Source );
p.translate(-boundingRect.topLeft()*scale);
p.scale(scale,scale);
QStyleOptionGraphicsItem opt;
boostForeach(QGraphicsItem* item,items) {
item->paint(&p, &opt, 0);
}
p.end();
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap);
item->scale(1.0/scale,-1.0/scale);
item->setOffset(boundingRect.topLeft()*scale);
showOnScene(item);
}

What is the fastest way to get QWidget pixel color under mouse?

I need to get the color of pixel under mouse, inside mouseMoveEvent of a QWidget (Breadboard). Currently I have this code->
void Breadboard::mouseMoveEvent(QMouseEvent *e)
{
QPixmap pixmap = QPixmap::grabWindow(winId());
QRgb color = pixmap.toImage().pixel(e->x(), e->y());
if (QColor(color) == terminalColor)
QMessageBox::information(this, "Ter", "minal");
}
Take a look at (scaled down) screenshot below-
When user moves his mouse on breadboard, the hole should get highlighted with some different color (like in red circle). And when the mouse exits, the previous color (grey) should be restored. So I need to do following steps-
Get color under mouse
According to color, floodfill the hole. (Different holes are distinguished using color)
On mouse out, restore the color. There would be wires going over holes, so I can't update the small rectangle (hole) only.
What is the fastest way of doing this? My attempt to extract color is not working i.e the Message box in my above code never displays. Moreover I doubt if my existing code is fast enough for my purpose. Remember, how fast you will be moving your mouse on breadboard.
Note - I was able to do this using wxWidgets framework. But due to some issues that project got stalled. And I am rewriting it using Qt now.
You are invited to look at code https://github.com/vinayak-garg/dic-sim
The "idiomatic" way of doing this in Qt is completely different from what you're describing. You'd use the Graphics View Framework for this type of thing.
Graphics View provides a surface for managing and interacting with a large number of custom-made 2D graphical items, and a view widget for visualizing the items, with support for zooming and rotation.
You'd define your own QGraphicsItem type for the "cells" in the breadboard that would react to hover enter/leave events by changing their color. The connections between the cells (wires, resistors, whatever) would also have their own graphics item types with the features you need for those.
Here's a quick and dirty example for you. It produces a 50x50 grid of green cells that become red when the mouse is over them.
#include <QtGui>
class MyRect: public QGraphicsRectItem
{
public:
MyRect(qreal x, qreal y, qreal w, qreal h)
: QGraphicsRectItem(x,y,w,h) {
setAcceptHoverEvents(true);
setBrush(Qt::green);
}
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *) {
setBrush(Qt::red);
update();
}
void hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
setBrush(Qt::green);
update();
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
for (int i=0; i<50; i++)
for (int j=0; j<50; j++)
scene.addItem(new MyRect(10*i, 10*j, 8, 8));
QGraphicsView view(&scene);
view.show();
return app.exec();
}
You could modify the hover event handlers to talk to your "main window" or "controller" indicating what's currently under the mouse so you can update your caption, legend box or tool palette.
For best speed, render only the portion of the widget you're interested in into a QPaintDevice (like a QPixmap). Try something like this:
void Breadboard::mouseMoveEvent(QMouseEvent *e)
{
// Just 1 pixel.
QPixmap pixmap(1, 1);
// Target coordinates inside the pixmap where drawing should start.
QPoint targetPos(0, 0);
// Source area inside the widget that should be rendered.
QRegion sourceArea( /* use appropriate coordinates from the mouse event */ );
// Render it.
this->render(&pixmap, targetPos, sourceArea, /* look into what flags you need */);
// Do whatever else you need to extract the color from the 1 pixel pixmap.
}
Mat's answer is better if you're willing to refactor your application to use the graphics view API.

QWidget paint even not being called even with non-zero width and height?

I am trying to make a scroll area hold a widget which will serve as a drawing area for a drag-and-drop editor I am trying to build. However, I can't seem to get it to draw.
Here is a picture: http://i.imgur.com/rTBjg.png
On the right the black space is what is supposed to be my scrollarea
Here is the constructor for my the window class (I am using Qt-Creator):
ModelWindow::ModelWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ModelWindow)
{
ui->setupUi(this);
editor = new ModelEditorWidget(this);
ui->scrollArea->setWidget(editor);
}
The model editor widget looks like so:
//header
class ModelEditorWidget : public QWidget
{
Q_OBJECT
public:
explicit ModelEditorWidget(QWidget *parent = 0);
signals:
public slots:
protected:
virtual void paintEvent(QPaintEvent *e);
};
//.cpp file:
ModelEditorWidget::ModelEditorWidget(QWidget *parent) :
QWidget(parent)
{
this->setAcceptDrops(true);
this->resize(1000, 1000);
cout << this->rect().x() << " " << this->rect().width() << endl;
this->update();
}
void ModelEditorWidget::paintEvent(QPaintEvent *e)
{
cout << "painting";
QWidget::paintEvent(e);
QPainter painter(this);
painter.setBrush(QBrush(Qt::green));
painter.setPen(QPen(Qt::red));
painter.drawRect(400, 400, 50, 50);
painter.fillRect(e->rect(), Qt::SolidPattern);
}
I would think that this would set the modeleditorwidget's size to be 1000x1000 and then draw a green or red rectangle on the widget. However, the lack of a "painting" message in the command line from the cout at the beginning of the paintEvent shows that it isn't being executed. I first suspected this is because there was 0 width and 0 height on the widget. However, the cout in the constructor tells me that the widget is positioned at x = 0 and width = 1000 and so I assume that since that matches my resize statement that the height is also 1000 as was specified.
EDIT: By calling cout.flush() I got the "painting" output. However, that only deepens the mystery since the paint event doesn't look like it is actually painting. I am now calling show on both the scroll area and the widget as well.
Does anyone see what I could be doing wrong here? Perhaps I am not adding the ModelEditorWidget to the scrollarea properly?
Btw, I am very new to Qt and this is my first major GUI project using it. Most of my other GUI stuff was done using .NET stuff in C#, but since I want this to be cross platform I decided to stay away from C#.NET and mono and use Qt.
Documentation of QScrollArea::setWidget() says:
If the scroll area is visible when the widget is added, you must
show() it explicitly.
Note that You must add the layout of widget before you call this
function; if you add it later, the widget will not be visible -
regardless of when you show() the scroll area. In this case, you can
also not show() the widget later.
Have you tried that?