<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QGraphicsView and some related problems]]></title><description><![CDATA[<p dir="auto">In my program i am getting image data from a device when i clicked a button. I hold image as cv::Mat(Opencv) object. After it i changed it to <code>qimage -&gt; pixmap -&gt; scene -&gt; QGraphicsPixmapItem -&gt; ui-&gt;graphicsview</code>. And I have 2 sliders to change <code>brightness</code>&amp;<code>contrast</code> of image and 2 buttons <code>fit</code> for fit image to ui via scaling image, <code>1:1</code> to show image at original size and it should activate scrollbars if needed.</p>
<p dir="auto">I show image firstly as scaled after i read it. My codes:</p>
<pre><code>/*HEADER*/
    QGraphicsScene *scene = new QGraphicsScene;
    QImage qimg;
    QImage scaledQimg;
    QPixmap pixmap;
    cv::Mat img;
    cv::Mat imgProcessed;
    double brightness = 0;
    double contrast = 1;
    bool isImageScaled;
    QGraphicsPixmapItem *pix;

/*SOURCE*/
//When image readed from device, signal emitted and that slot runs
void MainWidget::ImageSlot(cv::Mat *image)
{
    img = *image;
    isImageScaled = true;

    DrawImage();
}

/*Other slots*/
void MainWidget::sliderBrightness_valueChanged(int value)
{
    brightness = value;
    DrawImage();
}

void MainWidget::sliderContrast_valueChanged(int value)
{
    contrast = value;
    DrawImage();
}

void MainWidget::buttonFIT_clicked()
{
    isImageScaled = true;
    DrawImage();
}

void MainWidget::button11_clicked()
{
    isImageScaled = false;
    DrawImage();
}

void MainWidget::DrawImage()
{
    scene-&gt;clear();

    if(!img.empty()){
        img.convertTo(imgProcessed, -1, contrast, brightness);

        qimg = QImage(imgProcessed.data, imgProcessed.cols, imgProcessed.rows, imgProcessed.step, QImage::Format_Grayscale8);
        scaledQimg = qimg.scaled(ui-&gt;graphicsView-&gt;width() - 10, ui-&gt;graphicsView-&gt;height() - 10, Qt::KeepAspectRatio);

        if(isImageScaled)
            pixmap = QPixmap::fromImage(scaledQimg);
        else
            pixmap = QPixmap::fromImage(qimg);

        pix = scene-&gt;addPixmap(pixmap);
        ui-&gt;graphicsView-&gt;setScene(scene);
    }
}
</code></pre>
<p dir="auto">Now I have multiple problems.<br />
1-When i read another image from device, the old image in <code>graphicsview</code> still stays there and new image overwrited to this, so sometimes i can see old image's part at left side of graphicsview. I added <code>scene-&gt;clear();</code> to fix issue but nothing changed. How to solve this issue?<br />
2-<code>1:1</code> button show image at original size and activate scrollbars, but after i click <code>FIT</code>, scrollbars are still there. But I dont want any scrollbars in FIT mode because image is already scaled to available ui in this mode. How to do this?<br />
3- Dealing with multiple qimage and isImageScaled variable are making program compilacted, are there any better approach for it? So it automatically activate scrollbars etc.<br />
BONUS: Any advice for creating zoom in/out functionality to graphicsview? On touchscren with 2 fingers, on pc with mouse whell or maybe with buttons.</p>
<p dir="auto">I hope someone will read and give some solutions, thanks in advance.</p>
]]></description><link>https://forum.qt.io/topic/143506/qgraphicsview-and-some-related-problems</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 02:53:37 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/143506.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 09 Mar 2023 06:02:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QGraphicsView and some related problems on Tue, 14 Mar 2023 20:57:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/masa4">@<bdi>masa4</bdi></a> said in <a href="/post/750480">QGraphicsView and some related problems</a>:</p>
<blockquote>
<p dir="auto">void MainWidget::DrawImage()<br />
{<br />
scene-&gt;clear();</p>
<pre><code>if(!img.empty()){
    img.convertTo(imgProcessed, -1, contrast, brightness);

    qimg = QImage(imgProcessed.data, imgProcessed.cols, imgProcessed.rows, imgProcessed.step, QImage::Format_Grayscale8);
    scaledQimg = qimg.scaled(ui-&gt;graphicsView-&gt;width() - 10, ui-&gt;graphicsView-&gt;height() - 10, Qt::KeepAspectRatio);

    if(isImageScaled)
        pixmap = QPixmap::fromImage(scaledQimg);
    else
        pixmap = QPixmap::fromImage(qimg);

    pix = scene-&gt;addPixmap(pixmap);
    ui-&gt;graphicsView-&gt;setScene(scene);
}
</code></pre>
<p dir="auto">}</p>
<pre><code></code></pre>
</blockquote>
<p dir="auto">There in the line where you call addPixmap. This create a new object every time.</p>
<p dir="auto">This answers question 1<br />
For question 2, retry when you stopped adding all these objects.<br />
For question 3, do you mean you find complicated to manage two QImage objects ? Why are you creating two images and then selecting one of the two ? Move all the additional logic where it actually make sense then there's no need for multiple QImages. This will also avoid wasting CPU and RAM for nothing.<br />
For the bonus question, check the <a href="https://doc.qt.io/qt-6/gestures-overview.html" target="_blank" rel="noopener noreferrer nofollow ugc">Gesture Overview in Qt's documentation</a></p>
]]></description><link>https://forum.qt.io/post/751054</link><guid isPermaLink="true">https://forum.qt.io/post/751054</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 14 Mar 2023 20:57:08 GMT</pubDate></item><item><title><![CDATA[Reply to QGraphicsView and some related problems on Mon, 13 Mar 2023 06:15:43 GMT]]></title><description><![CDATA[<p dir="auto">ı@SGaist said in <a href="/post/750730">QGraphicsView and some related problems</a>:</p>
<blockquote>
<p dir="auto">Not the QPixmap object, the QGraphicsPixmapItem object</p>
</blockquote>
<p dir="auto">Im sorry, can you show it with code? I dont see where i am creating it every time.</p>
<p dir="auto">I have 3 problems + one Bonus, any answer for any of them are welcome.</p>
]]></description><link>https://forum.qt.io/post/750785</link><guid isPermaLink="true">https://forum.qt.io/post/750785</guid><dc:creator><![CDATA[masa4]]></dc:creator><pubDate>Mon, 13 Mar 2023 06:15:43 GMT</pubDate></item><item><title><![CDATA[Reply to QGraphicsView and some related problems on Sat, 11 Mar 2023 20:33:16 GMT]]></title><description><![CDATA[<p dir="auto">Not the QPixmap object, the QGraphicsPixmapItem object.</p>
<p dir="auto">Which question ?</p>
]]></description><link>https://forum.qt.io/post/750730</link><guid isPermaLink="true">https://forum.qt.io/post/750730</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 11 Mar 2023 20:33:16 GMT</pubDate></item><item><title><![CDATA[Reply to QGraphicsView and some related problems on Fri, 10 Mar 2023 06:41:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> You mean convert this</p>
<pre><code>if(isImageScaled)
  pixmap = QPixmap::fromImage(scaledQimg);
else
  pixmap = QPixmap::fromImage(qimg);
</code></pre>
<p dir="auto">to</p>
<pre><code>if(isImageScaled)
  pixmap.convertFromImage(scaledQimg);
else
  pixmap.convertFromImage(qimg);
</code></pre>
<p dir="auto">this, right?<br />
And do you have any idea for my questions?</p>
]]></description><link>https://forum.qt.io/post/750599</link><guid isPermaLink="true">https://forum.qt.io/post/750599</guid><dc:creator><![CDATA[masa4]]></dc:creator><pubDate>Fri, 10 Mar 2023 06:41:40 GMT</pubDate></item><item><title><![CDATA[Reply to QGraphicsView and some related problems on Thu, 09 Mar 2023 20:28:03 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">There's no need to recreate the pixmap item every time, just set the image on the item when you want to change it.</p>
]]></description><link>https://forum.qt.io/post/750566</link><guid isPermaLink="true">https://forum.qt.io/post/750566</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 09 Mar 2023 20:28:03 GMT</pubDate></item></channel></rss>