Can't move items in qgraphicsscene - qt

I'm trying to add items to qgraphicsscene, and move them later. I am using QMainWindow and QFrames to organize content of window and I can add items, but can't move the items. In tutorials I have found authors use QDialog and QgraphicsView as the only widget. Is that what makes the difference ? QMainWindow does not pass mouse events to GraphicsView or QGraphicsScene ? Any help appreciated.
def _add_area(self):
item = QGraphicsRectItem(0, 0, 100, 100)
brush = QBrush()
brush.setStyle(Qt.Dense7Pattern)
brush.setColor(Qt.darkBlue)
item.setBrush(brush)
item.setFlag(QGraphicsItem.ItemIsMovable)
item.setFlag(QGraphicsItem.ItemIsSelectable)
self._scene.addItem(item)

Ok, it seems I've found solution. When I removed QGraphicsView from QFrame it started working ( now QWidget is the parent of graphicsview ).

Related

QGraphics View layout

Can somebody help me understand how the QGraphicsView works within a QVBoxlayout?
I inherit a class from QWidget and the following code works as expected when it gets displayed:
m_mainVBoxLayout=new QVBoxLayout;
this->setLayout(m_mainVBoxLayout);
QWidget *testWidget=new QWidget();
testWidget->setAutoFillBackground(true);
m_mainVBoxLayout->addWidget(testWidget);
QWidget *testWidget2=new QWidget();
testWidget2->setAutoFillBackground(true);
m_mainVBoxLayout->addWidget(testWidget2);
When I run it the screen is divided vertically and there are two shaded boxes for the test Widgets as expected.
But if I remove testWidget2 code and replace it by:
QGraphicsView *view=new QGraphicsView();
m_mainVBoxLayout->addWidget(view);
The layout is messed up in that I don't see the shaded box for testWidget.
I even tried creating a scene and adding it to the view to no avail.
Any ideas why this happens?
I need to learn how to read better (Qt Documentation)
You have to use the viewport() method of QGraphicsView to get the default QWidget associated with it.
m_mainVBoxLayout->addWidget(view->viewport());
fixes the problem.

Graphics errors with QWidgets in a QGraphicsScene

I am trying to have a widget live freely inside a large scrollable panel. When I scroll around the QGraphics view, the child widget does not draw properly— its sub-rectangles will blank out, and the borders (which are rounded) are not blended with the canvas. I have:
QApplication qapp(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
// a simple QWidget with a gridlayout and some child objects:
MyQWidget mywidget(NULL);
scene.addWidget(&mywidget);
view.setScene(&scene);
view.setSceneRect(0,0,1280,1280);
view.show();
view.resize(512,512);
qapp.exec();
I get the same bogus rendering if I use a QGraphicsProxyWidget and scene.addItem() instead.
If instead I parent my custom widget to the QGraphicsView, it renders properly, but then the widget does not seem to be part of the QGraphicsScene, because it doesn't scroll around anymore and remains fixed to the parent window.
What's going on?
Edit: Images of the blanking problem, with MyWidget replaced by QPushButton:
Before scrolling:
Scrolling a little down and to the right:
Second edit: I have a Retina display. Could this be related? Is there special setup for Retina with Qt?
You should be getting an error when you try to add a widget to the scene which already has a parent:
QGraphicsProxyWidget::setWidget: cannot embed widget which is
not a toplevel widget, and is not a child of an embedded widget
QGraphicsScene::addWidget() is a wrapper around QGraphicsProxyWidget::setWidget which documentation says:
widget must be a top-level widget whose parent is 0.
To have borders blended you need to set widget to be transparent:
mywidget.setAttribute(Qt::WA_TranslucentBackground, true);

How to remove the focus of a QGraphicsTextItem from a main QWidget

I have a number of QGraphicsTextItem and QGraphicsItem painted inside a QGraphicsView. This QGraphicsView has been added to the main Qwidget.
I have written the "FocusOutEvent" for this QGraphicsTextItem and the focus is getting removed only when the "MousePressEvent" is called within the QGraphicsView.
Now my concern here is, How to remove the focus of this QGraphicsTextItem when the MousePressEvent is called Outside the QGraphicsView?
In my MainWindow.cpp, I wrote a mousePressEvent function :
void EyGuiMainWindow::mousePressEvent(QMouseEvent *e)
{
QWidget *w = QApplication::focusWidget();
if(w)
w->clearFocus();
}
But this is not clearing the QGraphicsTextItem.
Expecting a positive response.
A QGraphicsTextItem is not a widget, but a QGraphicsItem. Graphics items are added to a QGraphicsScene and viewed by one or more QGraphicsView widgets.
The code presented is only calling clear focus on the currently focussed widget, but since the QGraphicsTextItem is not a widget, it won't be cleared.
In order to clear the focus on the QGraphicsTextItem, call its clearFocus function. If you don't have a pointer to the item, you can get a list of all the items in the scene with the items() function and iterate through them.

Qt : event invisible widget?

For some reasons, I need to draw a widget onto one another.
The structure is the following (see the image) :
I have an original QTableWigetItem
On the QTableWigetItem, I create a QWidget at the foreground with the same geometry
This QWidget contains a QBoxLayout
This QBoxLayout contains a QPixmap and a QComboBox
I want to do the following things :
The QWidget is just a "container" for my QBoxLayout and I would like to set him completely "invisible" for the user. If the user click or move at the position of the widget, I want the event of the QTableWigetItem in the background to be trigerred. But the problem is that I want the QPixmap and the QComboBox to be at the foreground, visible and "normal". For me it's just a trick to be able to put children widgets in a QTableWidget of a HeaderView.
How to make the QWidget "completely invisible" (from the event/signals point of view) ?
Thank you very much.
Try QWidget::setWindowOpacity(0)

How to add menu to a widget derived from Qwidget

i want to know how to add menu to a widget, which is derived from Qwidget in QT symbian,
i know how to add menu to window derived from Qwindow, but i am not getting for widget
derived from Qwidget
pls help me..
Thanks
QMainWindow provides you convenience function to add and manage a QMenuBar.
With a window that inherits from QWidget (instead of QMainWindow) you need to achieve this by yourself. You can add the menu bar in the window layout like any other QWidget using the function add. By playing with the layouts you can place QMenuBar at the top of the window.
An other way can be to use a QToolBar.

Resources