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.
Related
I don't understand from the documentation (http://doc.qt.io/qt-5/qtwidgets-mainwindows-dockwidgets-example.html) whether a QDockWidget can be attached to a QWidget.
I m trying to create a new additional window (a QWidget) and I would like to attach to it a Dock, but when I pass the QWidget as a parent, the Dock shows up in the QMainWindow
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);
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 ).
In Qt5, I have a QDialog window on which I have drawn a circle as follows:
void MyDialog::paintEvent(QPaintEvent *pe)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
QPen pen(Qt::blue,2);
painter.setPen(pen);
QRect r=QRect(0,0,100,100);
painter.drawEllipse(r);
}
If I draw a larger circle, for example by using QRect(0,0,500,500);, the circle being greater than the dialog window is clipped. So I dragged a QScrollArea onto the the dialog window and decide to draw onto that so that scroll bars are automatically added. The QScrollArea can be accessed using ui->scrollArea.
I changed the above code by setting QPainter painter(ui->scrollArea);. However, nothings appears in the QScrollArea. I read that I need to override the paintEvent of QScrollArea. But I don't know how to do this.
Any help of drawing on the QScrollArea?
Drawing on the QScrollArea is not what you want either because the QScrollArea actually has a viewport widget.
Create another class which inherits QWidget. Override the paintEvent() method and to the painting you mention. Then, add the widget to the scroll area in your dialog.
MyDialog::MyDialog()
{
QScrollArea *pScrl = new QScrollArea(this);
pScrl->setWidget(new MyWidget(pScrl));
... // use a layout to put the scroll area in the dialog
}
To really make it useful you will need to resize the MyWidget instance to the size of the circle that you want to draw.
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)