Qt - How to apply a QToolTip on rectangle - c++

I want to inform user when cursor hover on the particular rectangle. I have many 20x20 rectangles. Below you can see how I draw the rectangles.
QRect rec(horizontalScan,verticalScan,20,20);
QPen framepen(Qt::black);
framepen.setWidth(1);
QBrush brush(colors[randColorNo],Qt::SolidPattern);
painter.fillRect(rec,brush);
painter.setPen(framepen);
painter.drawRect(rec);
Please consider that I am not so good on Qt so try to explain exactly what should I do.

If you want interactivity - better use QGraphicsScene. QGraphicsItems has tooltip attribute.
If you want to stick with QPainter - you should do it manually by detecting mouse position with mouseMoveEvent() and showing tooltip with QToolTip::showText().

Related

Detecting areas in an image. Overlapping Widgets in QT is it possible ?

I currently have an image say of a face displayed inside a QLabel and I would like to add two QPushButtons on the eye so that I would know when those buttons are pressed.I would like to have the buttons overlap the face so I would know that the user clicked on the eye. My question is if there is a Layout in QT that would allow overlapping of widgets. ?
You can set the parent of the QPushButtons to the QLabel, and then use setGeometry(x, y, w, h) to specify the QPushButton's location and size within the QLabel.
QPushButton * leftEyeButton = new QPushButton(ui->myLabel);
leftEyeButton ->setGeometry(5,10,20,20);

Qt drawing outside the Painter device

I created a subclass of QToolBar to make a context tool bar associate with QGraphicsItem.
What I want to add is a small triangle point to where my toolbar popped up. I know that QPainter can't draw outside the QToolBar. But is there a way to draw from pic 1 to pic 2
Sorry. My pics are awful. But you could understand.
Maybe you should create a new widget (or a tiny frameless, borderless window), with only the small triangle and put it at the required position (ie: the x coordinate should be the mouseX - width_of_window/2 and the Y coordinate should be mouseX - height_of_window).
Make sure the widget (windows) is hidden when the scrollbar is not shown, and show it at the calculated global coordinates on a mouse action.

Interactively editing an existing rectangle on a QPixmap?

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:
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:
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?
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.

Drag a rectangle over image in Qt

I guess there are lots of ways to achieve this. I have an application in which a video stream is shown over a custom QWidget that I have subclasses from QLabel, and painting frames using QPainter. Given that, is it possible to let the user to drag a rectangle over the image and retrieve the coordinates? The requirement is that the rectangle must be visible during the dragging.
Thanks in advance,
Have a look at QRubberBand. It allows you to place such a rect on top of e.g. a QLabel. The documentation also contains an example how to move and resize the rubberband using the mouse.
the QGraphicsView has the void setRubberBandSelectionMode ( Qt::ItemSelectionMode mode ) but i dont know if the QLabel has some similar feature ...
maybe you have to draw your own rectangle while the user drags the rectangle and catch it on mouserelease
soo long zai
In you widget you could track mouse pressed and released events and track where on the widget the corners of the selection rect are. For drawing the rectangle, I'd take a look at QStyle::drawFocusRect. I think the intent of that is to draw a rect you'd be able to see regardless of what's behind it.
Or perhaps try this:
QStylePainter painter(this);
QStyleOptionFocusRect option;
option.initFrom(this);
option.backgroundColor = palette().color(QPalette::Background);
painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);

Anchoring a QGraphicsView to the corner of the screen

I have a QGraphicsView that renders my game. This view fills the entire screen at all times. In the top right I have another QGraphicsView which I'm using as a mini-map; it sits over the game view. I want this mini-map to be anchored to the top right of the screen, always maintaining its size. This code almost works, except that the left side of the mini-map never changes (which is to be expected).
void MainWindow::resizeEvent(QResizeEvent *event)
{
mainWindow->graphicsView->resize(event->size().width(), event->size().height());
QRect newRect(mainWindow->miniMapGraphicsView->geometry());
newRect.setRight(event->size().width() - 20);
mainWindow->miniMapGraphicsView->setGeometry(newRect);
}
How can I do this?
When you want to position widgets in a certain way, layouts are usually the best way to do it.
Add a QFormLayout to your graphicsView and set its layoutDirection to Qt::RightToLeft. Then add your miniMapGraphicsView to the layout. Editing the properties of the mini map, set its horizontal and vertical sizePolicy to Fixed and set its minimumSize and maximumSize to the dimensions you would like it to be.
An alternative would be to use a QGridLayout and use horizontal and vertical spacers to push the mini map to any corner of the view.
NOTE: Layouts have margins set by default so if you want your widgets to align snugly at the edges, zero them out.