1

I'm trying to creat a Dicom GUI Toolkit where the user selects some dicom images and the image of first dicom image from the selected ones will be shown. Then the user clicks on the image and the image pops out with bigger image window. In this shown bigger image, the image will consist of a red colored rectangle that contains necessary regions of the Dicom image while the unnecessary region is outside the rectangle. The user should then have the option to change the rectangle by mouse.

Until now, I have been able to show the big dicom image with the rectangle in it using QLabel which is by the following code snippets.

void MainWindow::showBigImage()
{
    QPixmap bigimage;
    bigimage.load(imageName.c_str());
    QPainter painter(&bigimage);
    painter.setPen(Qt::red);
    QRectF rect(xmin, ymin, xmax, ymax);
    painter.drawRect(rect);
    QSize bigsize = ui->bigImageLabel->size();
    ui->bigImageLabel->setPixmap(bigimage.scaled(bigsize, Qt::IgnoreAspectRatio, Qt::FastTransformation));
    ui->bigImageLabel->show();
}

and the big image on the app looks like the following:

enter image description here

Can you please suggest me how I should now make the rectangle editable by the user where the user can set the existing red rectangle as per his or her wish?

I also tried similar thing using QGraphicsView and QGraphicsScene with the following code:

void MainWindow::showBigImage()
{
    QGraphicsScene* scene = new QGraphicsScene;
    scene->addPixmap(bigimage);
    ui->bigImageView->setScene(scene);
    ui->bigImageView->show();
}

And this code gives me the following look:

enter image description here

As you can see, I could not fit the image to the boundaries of QGraphicsView, could you suggest me how to do it? Could you also suggest me how to add the red rectangle(that I showed in the example using QLabel) on the QGraphicsView without adding the rectangle on the QPixmap?

the_naive
  • 2,936
  • 6
  • 39
  • 68
  • To fit a window you may want to see: http://stackoverflow.com/questions/9654222/how-to-fit-in-view-the-pixmaps-in-qgraphicsview-qgraphicsscene-without-changing – user2244507 Aug 28 '13 at 13:17
  • I actually saw that question earlier, I just could not understand how to reimplement the resizeEvent. I must mention I'm a newbie in Qt and million miles away from being efficient in C++. – the_naive Aug 28 '13 at 13:30

1 Answers1

2

In order to get the red selection rectangle, Qt provides the class QRubberBand. The docs state:

The QRubberBand class provides a rectangle or line that can indicate a selection or a boundary.

By subclassing the image object and implementing the mouse handling functions, to create the rubber band on mousePressEvent, update its position on mouseMoveEvent and grab its final rect on mouseReleaseEvent, the QRubberBand will simplify the problem.

If you want the QRubberBand to show all the time, just create it when you display the enlarged image and don't hide it on releasing the mouse button.

As for displaying the image in the QGraphicsView, the code you displayed doesn't set the geometry of the QGraphicsScene and QGraphicsView, so you're seeing a border. If you don't want that, you should set them accordingly. Also note that QGraphicsView has a function fitInView, which you could use, after having retrieved an area from the QRubberBand, in order to zoom into the selected area.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • But what about the red rectangle that exists even before the user uses any mouse movement to change rectangle? How can I make it editable in such a way that the rectangle will only change when the user touches it with the mouse cursor? Is that possible to do that with QRubberBand? – the_naive Aug 29 '13 at 06:14
  • What do you mean by "the red rectangle that exists"? Do you have one there already, from another widget? – TheDarkKnight Aug 29 '13 at 07:33
  • Anyways I managed it :). No I was drawing it on QPixmap as I showed it on the image. But now I managed it :). Thanks. Great advice :). – the_naive Aug 29 '13 at 07:40
  • Could you suggest me how to change the color or the thickness of QRubberBand? I've seen many threads but could find a solution. – the_naive Aug 29 '13 at 10:53