How to anchor QGraphicsWidget/Item (make them static) on QGraphicsView - qt

I want to make something like a hud. I have a QGraphicsScene with a huge QPixmap, that I added to a much smaller QGraphicsView. Now I need to add some control elements, like QLabel, QPushButton and QGraphicsEllipseItem. That's, I think, is not a problem. I found this helpful page Graphics View Classes. But how do I anchor this control elements to the View or maybe better said how do I not anchor them? When I drag the QPixmap, then I don't want that the hud is moving with the QPixmap out of the QGraphicsView.
What do I need to do, to solve this problem. Hope someone can help me.

You should just have to create your widgets and specify the QGraphicsView in the constructor like this:
QPushButton *button1 = new QPushButton(view);

Related

Tooltips for QDockWidget close button & float button?

Is there a way to set a tool tip text for the close button & float button in a QDockWidget ?
As ixSci mentioned, using setTitleBarWidget() is a potential way of solving this problem. Having said that I was looking for a much simpler solution, ideally using QSS (Qt Style Sheets). So after digging into the source code of QDockWidget I found an alternative way which suits my requirement better.
I wanted to find the place these float and close buttons are created. That is inside QDockWidgetPrivate::init() method in QDockWidget.cpp.
As for an example, the float button is created like this:
QAbstractButton *button = new QDockWidgetTitleButton(q);
button->setObjectName(QLatin1String("qt_dockwidget_floatbutton"));
QObject::connect(button, SIGNAL(clicked()), q, SLOT(_q_toggleTopLevel()));
layout->setWidgetForRole(QDockWidgetLayout::FloatButton, button);
Now all I need is to use the flexibility of Qt Style Sheets, for that I need only the Object Name, in this case it's "qt_dockwidget_floatbutton"
So all you need to do, to set tooltips for Close and Float buttons of a QDockWidget, is to add following two lines of styles in your application style sheet
QAbstractButton#qt_dockwidget_closebutton{qproperty-toolTip: "Close";}
QAbstractButton#qt_dockwidget_floatbutton{qproperty-toolTip: "Restore";}
You can implement whatever title widget you want and set it with setTitleBarWidget(). In that widget you can add whatever buttons with tooltips you need.

QGraphics Scene Zooming in and out

Here I am again worrying about how to Zoom in and out a QGraphicsPixmapItem in a graphics scene. I looked for a direct method for this but could not find any in graphics scene or in pix map. Can someone help me with this. Do I have to extend QGraphicsPixmapItem and implement methods for this.
Thanks again for help and I really do appreciate it.
~Tharanga
setScale() changes the size of the item, not the view scale of the scene. For a one item scene, it's effectively the same. But if you have more than one item in the scene, it changes the relation between items.
QGraphicsView::setTransform() should be used if you want to keep the item's relationship to the scene and other items.
QGraphicsPixmapItem inherits from QGraphicsItem, so it has all of that class's methods.
In particular, there's setScale that will change the item's scale factor (i.e. "zoom" it).
Look at the Transformations Example page for how this is done, and other transformations you can do.

Qt Flickable Widgets

I've been playing around with the Flickable class from Qt Labs. I understand pretty well how the example works, but I'm having trouble understanding how I can use it to display something other than something dynamically painted inside the paintEvent.
If I have a list of buttons that I want to be Flickable, is there a way to reimplement the Flickable paintEvent to call all the button's paintEvents?
It seems like the Flickable class pretty much figures out which buttons would be visible so all I'd have to do for each button is set its y position and tell it to paint itself at that position.
Any pointers would be appreciated...
In your subclass, you need to reimplement setScrollOffset. There you can do various things. The included ColorList just triggers a repainting update, but theoretically you could also e.g. move widgets around. In fact, there is no need to reimplement paintEvent in any subclass of Flickable.
Note: I wrote that Flickable class.

How to enable dragging in QGraphicsScene?

I'm trying to put two pictures in the QGraphicsScene. One is fixed and the other one is movable. But I succeed only in putting those pictures in the QGraphicsScene, I can't move them. How can I achieve this?
You have to make sure the item is movable. Have a look at QGraphicsItem::setFlag.
You'll have to do something like this :
myImageItem->setFlag(QGraphicsItem::ItemIsMovable, true);

How does QWidget size when used as a window?

Several examples on trolltech use QWidget rather than a QMainWindow as the main window for simple Qt applications.
Now I need just a simple window with a single content widget (QGlWidget) that covers the entire client area.
However, when I show a QWidget window with a single content window it automatically resizes itself to be tiny.
If I create the QWidget parent window without a child It is a nice large default size.
I don't want to resort to using Layouts for a single child window :/
What I understand is that you use a QWidget to display your QGIWidget. Try calling the show method of your QGIWidget directly (if your QGIWidget class inherits QWidget), Qt will create the window decoration for you.
Otherwise if you really need your widget to be inside one another, and fit its size, you'll have to use a layout.
Either follow gregseth's advice or you can simply resize the widget yourself.
(though this way you'll loose nice auto-resizing which is provided by Qt when you use layouts)
In your case you can basically do something like:
QGlWidget* myWidget = new QGlWidget;
myWidget->resize(QApplication::desktopWidget()->availableGeometry().size());
// or maybe instead of resizing show it in fullscreen:
myWidget->showFullScreen();
(actually I don't remember if showFullScreen() will do resizing for you, maybe you'll need both resize+showFullScreen :))
Cheers :)
P.S. Using layout is actually an option. It's not expensive and it's flexible. All it gets: "layout = new QVBoxLayout(myWidget); layout->addWidget(myWidget);" and you're done :)
Not always.
I've found that a QMainWindow will not work as the parent widget when using the QVBoxLayout and QHBoxLayout to arrange child widgets.
If you create a QWidget and use that in place of the QMainWindow then the layouts will work correctly.
QWidget* centralWidget = new QWidget( MainWindow );
MainWindow->setCentralWidget( centralWidget );
If you use QtCreator and look at the code it creates you can see it creating a 'hidden' widget if you try to use the layouts directly at the top level.
It's not obvious, intuitive, or documented anywhere that I've found.

Resources