Mouse pointer position when a label is activated - c++

When an object of QLabel sub class is actived, how can one find if the mouse pointer is on the label and get its position if it is?
QWidegt::event() can check the event type of QEvent::WindowActivate, but it provides no information about mouse pointer position.
UPDATE
According to comment by #Mathias Schmid , I create the following code. It verifies itself that both focusInEvent and focusOutEvent can happen. However, I still cannot get the mouse pointer position. Maybe I am missing the part of "bind enable/disable of mouse tracking to focus in and out events", or something else.
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
#include <QtCore>
class MyLabel : public QLabel
{
public:
MyLabel(QWidget*parent = nullptr) : QLabel(parent)
{
setMouseTracking(true);
setFocusPolicy(Qt::FocusPolicy::StrongFocus);
}
protected:
virtual void focusInEvent(QFocusEvent *ev) override
{
(void)ev;
this->setText(__PRETTY_FUNCTION__);
}
virtual void focusOutEvent(QFocusEvent *ev) override
{
(void)ev;
this->setText(__PRETTY_FUNCTION__);
}
virtual void mouseMoveEvent(QMouseEvent *ev) override
{
this->setText(QString::number(ev->pos().x()) +", " +QString::number(ev->pos().y()));
QLabel::mouseMoveEvent(ev);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyLabel w;
w.setFixedSize(400, 300);
w.show();
return a.exec();
}
Update 2: solution
Thanks to #Mathias Schmid, the final solution is to use static function QCursor::pos() in focusInEvent().
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
#include <QtCore>
#include <QCursor>
class MyLabel : public QLabel
{
public:
MyLabel(QWidget*parent = nullptr) : QLabel(parent)
{
setMouseTracking(true);
setFocusPolicy(Qt::FocusPolicy::StrongFocus);
}
protected:
virtual void focusInEvent(QFocusEvent *ev) override
{
(void)ev;
QPoint pos = QCursor::pos();
QString msg = QString(__PRETTY_FUNCTION__) + ": \n" + QString::number(pos.x()) + ", " + QString::number(pos.y());
QPoint posLocal = this->mapFromGlobal(pos);
if(this->rect().contains(posLocal))
msg += "\nLocal pos: " + QString::number(posLocal.x()) + ", " + QString::number(posLocal.y());
this->setText(msg);
QLabel::focusInEvent(ev);
}
virtual void focusOutEvent(QFocusEvent *ev) override
{
(void)ev;
this->setText(QString(__PRETTY_FUNCTION__));
QLabel::focusOutEvent(ev);
}
virtual void mouseMoveEvent(QMouseEvent *ev) override
{
this->setText(QString::number(ev->pos().x()) +", " +QString::number(ev->pos().y()));
QLabel::mouseMoveEvent(ev);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyLabel w;
w.setFixedSize(450, 300);
w.show();
return a.exec();
}

To check if mouse cursor is on custom widget derived from QLabel at time when it gets focus just override focusInEvent() and handle mouse cursor position check there.
MyLabel.h
class MyLabel: public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent = nullptr);
~MyLabel();
protected:
virtual void focusInEvent(QFocusEvent *event) override;
};
MyLabel.cpp
MyLabel::MyLabel(QWidget *parent)
: QLabel(parent)
{
setFocusPolicy(Qt::StrongFocus);
}
MyLabel::~MyLabel()
{
}
void MyLabel::focusInEvent(QFocusEvent *event)
{
if (event) {
const QPoint cursorPos = QCursor::pos();
if (rect().contains(mapFromGlobal(cursorPos))) {
// TODO: Add desired action
}
QLabel::focusInEvent(event);
}
}

Related

How can I write a (very) basic custom QAbstractTextDocumentLayout subclass?

I'm trying to create a simple notepad-style Qt application. Since I think I might want a lot of control over exactly how text is rendered, I'm trying to write a custom QAbstractTextDocumentLayout class. The code below compiles and runs, but the text area is blank, no matter how much I type. The debug statements in my draw() function correctly show the entered text, so I know my input is in some form making it all the way to the draw() function, but it's not being rendered.
I just want to tweak this in a minimal way so I can at least see some text being rendered, even if the implementation is incomplete. I also haven't been able to find any documentation whatsoever on writing custom QAbstractTextDocumentLayout, so if anyone had a reference I'd also appreciate that.
main.cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QTextEdit>
#include <QKeyEvent>
#include "./DocumentLayout.cpp"
class MainContent : public QWidget
{
Q_OBJECT
QTextEdit textEdit;
DocumentLayout layout;
public:
MainContent() : textEdit(this), layout(textEdit.document())
{
int innerPadding = 20;
textEdit.document()->setDocumentMargin(innerPadding);
int textColor = 0xD8DEE9;
int backgroundColor = 0x2E3440;
int selectionColor = 0x2E3440;
int selectionBackgroundColor = 0x81A1C1;
QPalette palette = textEdit.palette();
palette.setColor(QPalette::Base, QColor(backgroundColor));
palette.setColor(QPalette::Text, QColor(textColor));
palette.setColor(QPalette::Highlight, QColor(selectionBackgroundColor));
palette.setColor(QPalette::HighlightedText, QColor(selectionColor));
textEdit.setPalette(palette);
textEdit.setFrameStyle(QFrame::NoFrame);
textEdit.document()->setDocumentLayout(&layout);
};
void resizeEvent(QResizeEvent *event)
{
textEdit.setGeometry(0, 0, this->width(), this->height());
}
void keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_W && event->modifiers() == Qt::ControlModifier)
{
qApp->quit();
}
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
window.setWindowTitle("Hello, world!");
window.resize(400, 500);
MainContent content;
window.setCentralWidget(&content);
window.show();
return app.exec();
}
#include "main.moc"
DocumentLayout.cpp
#include <QAbstractTextDocumentLayout>
#include <QPainter>
#include <QTextBlock>
#include <QDebug>
class DocumentLayout : public QAbstractTextDocumentLayout
{
public:
DocumentLayout(QTextDocument *document) : QAbstractTextDocumentLayout(document)
{
}
void draw(QPainter *painter, const PaintContext &context)
{
QTextDocument *document = this->document();
QTextBlock block = document->begin();
QPointF position(20, 20);
painter->setPen(context.palette.color(QPalette::Text));
QTextLayout *layout = block.layout();
layout->draw(painter, position);
position.ry() += layout->boundingRect().height();
qDebug() << "drew" << block.text() << "at" << position;
}
int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const
{
return 0;
}
int pageCount() const
{
return 1;
}
QSizeF documentSize() const
{
return QSizeF(400, 800);
}
QRectF frameBoundingRect(QTextFrame *frame) const
{
return QRectF(0, 0, 400, 800);
}
QRectF blockBoundingRect(const QTextBlock &block) const
{
return QRectF(0, 0, 400, 800);
}
void documentChanged(int from, int charsRemoved, int charsAdded)
{
}
};

Draggable pixmaps inside a QGraphicsScene of graphic items

I have a scene with a 12*4 grid with blocks of QGraphicsItems ,when i right click on the blocks I have a contexmenu that
can add icons inside the blocks my proplem is that
I can't fingure out how can I make those icons draggable to the other blocks inside the graphic scene ,I know there is the "Draggable Icons Example" but how can I implement that code to a graphic scene.
this is the mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsPathItem>
class QGraphicsSceneMouseEvent;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
bool eventFilter(QObject *, QEvent *);
~MainWindow();
private slots:
void showContextMenu(const QPoint&);
void addPixBlock();
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QGraphicsItem *itemAt(const QPointF&);
int x;
int y;
QMenu *Menu;
QMenu *Submenu;
QAction *Picture;
QGraphicsPixmapItem* pix;
};
#endif // MAINWINDOW_H
the mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "block.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include <QPainter>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this) ;
for(int row=-4;row<8;++row)
for(int column=0;column<4;++column)
{
Block *b = new Block;
scene->addItem(b);
b->setPos(row* 95,column*85);
}
ui->graphicsView->setScene(scene);
scene->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
QGraphicsItem* MainWindow::itemAt(const QPointF &pos)
{
QList<QGraphicsItem*> items = scene->items(QRectF(pos - QPointF(1,1),
QSize(3,3)));
foreach(QGraphicsItem *item, items)
if (item->type() > QGraphicsItem::UserType)
return item;
return 0;
}
bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
QGraphicsSceneMouseEvent *me = (QGraphicsSceneMouseEvent*) e;
switch ((int) e->type()){
case QEvent::GraphicsSceneMousePress:{
switch ((int) me->button()){
case Qt::RightButton:{
QGraphicsItem *item = itemAt(me->scenePos());
if (item && item->type() == Block::Type){
x=item->scenePos().x();
y=item->scenePos().y();
showContextMenu(item->scenePos().toPoint());
}
break;
}
}
break;
}
}
return QObject::eventFilter(o, e);
}
void MainWindow::showContextMenu(const QPoint &pos)
{
Menu= new QMenu("Menu");
Submenu=Menu->addMenu(QIcon(":/img/pix.png"),"Pix");
Picture =Submenu->addAction(QIcon(":/img/pix.png"),"Pix");
connect(Picture, SIGNAL(triggered()), this, SLOT(addPixBlock()));
Menu->exec(QCursor::pos());
}
void MainWindow::addPixBlock()
{
QPixmap pixmap(":/img/pix.png");
pix = scene->addPixmap(pixmap.scaled(70,50));
pix->setPos(x,y);
}
the block.h
#ifndef BLOCK_H
#define BLOCK_H
#include <QGraphicsPathItem>
class QGraphicsSceneMouseEvent;
class Block : public QGraphicsPathItem
{
public:
enum { Type = QGraphicsItem::UserType + 3 };
int type() const { return Type; }
Block(QGraphicsItem *parent = 0);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget
*widget);
bool eventFilter(QObject *, QEvent *);
};
#endif // BLOCK_H
the Block.cpp
#include "block.h"
#include <QPainter>
#include <QtWidgets>
class QGraphicsSceneMouseEvent;
Block::Block(QGraphicsItem *parent)
: QGraphicsPathItem(parent)
{
QPainterPath p;
//<->,|,<->,|,roundness
p.addRoundedRect(0,0,80,50, 5, 5);
setPath(p);
setAcceptDrops(true);
setAcceptedMouseButtons(Qt::LeftButton);
}
void Block::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setPen(QPen(QColor(67, 141, 220)));
painter->setBrush(QColor(67, 141, 220,100));
painter->drawPath(path());
}
First of all if you want to place a QGraphicsPixmapItem on top of another item, a better option is to set it as your parentItem.
On the other hand we can use an event filter but a better option in this case is to implement a custom QGraphicsScene, and when pressing with the left key it allows to drag the item, for that we use QDrag and we pass the data of the item, then we overwrite the event dropEvent where we will obtain the item and establish a new parent.
graphicsscene.h
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
class QMenu;
class QAction;
class GraphicsScene : public QGraphicsScene
{
public:
using QGraphicsScene::QGraphicsScene;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void dropEvent(QGraphicsSceneDragDropEvent *event) override;
private:
QGraphicsPixmapItem *findPixmapItem(QGraphicsItem *item);
void createDrag(const QPointF &pos, QWidget *widget, QGraphicsItem *item);
void showContextMenu(const QPointF &pos);
void addPixBlock(QGraphicsItem *item);
QMenu *menu;
QMenu *submenu;
QAction *picture;
QGraphicsPixmapItem *pix;
};
#endif // GRAPHICSSCENE_H
graphicsscene.cpp
#include "graphicsscene.h"
#include <QDrag>
#include <QGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QMenu>
#include <QMimeData>
#include <QWidget>
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
auto its = items(QRectF(event->scenePos() - QPointF(1,1), QSize(3,3)));
auto val = std::find_if(its.constBegin(), its.constEnd(), [](auto const& it){
return it->type() > QGraphicsItem::UserType;
});
if(val == its.constEnd())
return;
if(event->button() == Qt::RightButton){
showContextMenu(event->scenePos());
}
else{
createDrag(event->scenePos(), event->widget(), *val);
}
}
void GraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
QByteArray byteArray = event->mimeData()->data("Item");
QGraphicsPixmapItem * item = *reinterpret_cast<QGraphicsPixmapItem**>(byteArray.data());
QGraphicsItem *item_parent = itemAt(event->scenePos(), QTransform());
item->setParentItem(item_parent);
}
QGraphicsPixmapItem *GraphicsScene::findPixmapItem(QGraphicsItem *item){
auto chs = item->childItems();
auto val = std::find_if(chs.constBegin(), chs.constEnd(), [](auto const& it){
return static_cast<QGraphicsPixmapItem *>(it) != Q_NULLPTR;
});
return val == chs.constEnd() ? Q_NULLPTR : static_cast<QGraphicsPixmapItem *>(*val);
}
void GraphicsScene::createDrag(const QPointF &pos, QWidget *widget, QGraphicsItem *item){
QGraphicsPixmapItem *pix = findPixmapItem(item);
if(pix == Q_NULLPTR)
return;
QByteArray byteArray(reinterpret_cast<char*>(&pix),sizeof(QGraphicsPixmapItem*));
QDrag *drag = new QDrag(widget);
QMimeData * mimeData = new QMimeData;
mimeData->setData("Item",byteArray);
drag->setMimeData(mimeData);
drag->setHotSpot(pos.toPoint()-pix->scenePos().toPoint());
drag->setPixmap(pix->pixmap());
drag->start();
}
void GraphicsScene::showContextMenu(const QPointF &pos)
{
QGraphicsItem *item = itemAt(pos, QTransform());
menu= new QMenu("Menu");
submenu = menu->addMenu(QIcon(":/img/pix.png"),"Pix");
picture = submenu->addAction(QIcon(":/img/pix.png"),"Pix");
connect(picture, &QAction::triggered, [item, this](){
addPixBlock(item);
});
menu->exec(QCursor::pos());
}
void GraphicsScene::addPixBlock(QGraphicsItem *item)
{
if(findPixmapItem(item))
return;
QPixmap pixmap(":/img/pix.png");
pix = addPixmap(pixmap.scaled(70,50));
if(pix->parentItem() != item)
pix->setParentItem(item);
}
Then we establish that new scene and add the Blocks.
The complete example can be found in the following link

QSlider handle and QProxyStyle

By default, QSlider move his thumbtrack by a value belonging to the pageStep() prop on mouse click. To make thumbtrack jump directly at the mouse click point, we need to create a new class inherited by QSlider.
.h
#ifndef QIMPROVEDSLIDER_H
#define QIMPROVEDSLIDER_H
#include <QSlider>
class QImprovedSlider : public QSlider
{
Q_OBJECT
protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
public:
explicit QImprovedSlider(QWidget *parent = 0);
signals:
void clicked(int value) const;
};
#endif // QIMPROVEDSLIDER_H
.cpp
#include <QWidget>
#include <QMouseEvent>
#include <QStyle>
#include <QStyleOptionSlider>
#include <QProxyStyle>
#include "QImprovedSlider.h"
class QImprovedSliderStyle : public QProxyStyle
{
public:
using QProxyStyle::QProxyStyle;
int styleHint(QStyle::StyleHint hint, const QStyleOption* option = 0,
const QWidget* widget = 0, QStyleHintReturn* returnData = 0) const
{
if (hint == QStyle::SH_Slider_AbsoluteSetButtons)
return (Qt::LeftButton | Qt::MidButton | Qt::RightButton);
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
};
QImprovedSlider::QImprovedSlider(QWidget *parent) :
QSlider(parent)
{
setStyle(new QImprovedSliderStyle(this->style()));
}
void QImprovedSlider::mousePressEvent(QMouseEvent *event) {
QStyleOptionSlider opt;
initStyleOption(&opt);
QRect sr = style()->subControlRect(QStyle::CC_Slider,
&opt,
QStyle::SC_SliderHandle,
this);
qDebug() << sr.height() << sr.width();
if (!sr.contains(event->pos()) && event->button() == Qt::LeftButton) {
if (orientation() == Qt::Vertical)
setValue(minimum() + ((maximum()-minimum()) * (height()-event->y())) / height() ) ;
else
setValue(minimum() + ((maximum()-minimum()) * event->x()) / width() ) ;
}
QSlider::mousePressEvent(event);
}
void QImprovedSlider::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
emit clicked(value());
QSlider::mouseReleaseEvent(event);
}
}
QImprovedSliderStyle make the handle drag more fluid, but in this way, the event is fired even when the click falls inside the handle, while the condition
!sr.contains(event->pos())
should avoid this.

Qt mouse tracking not working at all when mouse hovers over QListWidget

I would like to get mouse position inside my QListWidget. The tracking is fine when mouse hovers over all other QWidgets - QMainWindow, QPushButton, CentralWidget, etc., except QListWidget.
c++ file: test_1.cpp
#include "test_1.h"
#include "ui_test_1.h"
test_1::test_1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::test_1)
{
ui->setupUi(this);
this->setMouseTracking(true);
ui->centralWidget->setMouseTracking(true);
ui->listWidget->setMouseTracking(true);
ui->pushButton->setMouseTracking(true);
ui->listWidget->addItem("aaa");
ui->listWidget->addItem("bbb");
ui->listWidget->addItem("ccc");
ui->listWidget->addItem("ddd");
ui->listWidget->addItem("eee");
}
void test_1::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << event->pos();
}
test_1::~test_1()
{
delete ui;
}
Header file: test_1.h
#ifndef TEST_1_H
#define TEST_1_H
#include <QMainWindow>
#include <QDebug>
#include <QMouseEvent>
namespace Ui {
class test_1;
}
class test_1 : public QMainWindow
{
Q_OBJECT
public:
explicit test_1(QWidget *parent = 0);
~test_1();
private:
Ui::test_1 *ui;
void mouseMoveEvent(QMouseEvent*);
};
#endif // TEST_1_H
Main: main.cpp
#include "test_1.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
test_1 w;
w.show();
return a.exec();
}
Output:
QPoint(359,141)
QPoint(358,141)
QPoint(357,140)
QPoint(356,140)
QPoint(355,140)
QPoint(354,139)
QPoint(353,139)
QPoint(352,139)
QPoint(351,139)
void test_2::mouseMoveEvent(QMouseEvent *event)
{
QPoint p = event->pos();
QRect widgetRect = ui->listWidget->rect();
if(widgetRect.contains(p))
{
qDebug() << "Inside";
ui->listWidget->grabMouse();
}
else
{
qDebug() << "Outside";
ui->listWidget->releaseMouse();
}
}
The right way of solving this is inheriting QListWidget and implementing void mouseMoveEvent(QMouseEvent *event)
But you have also another option, like installing an event filter on your QListWidget.
Add this in your contructor:
ui->listWidget->viewport()->installEventFilter(this);
And implement the event filter:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
qDebug() << Q_FUNC_INFO << "QEvent::MouseButtonPress";
}
if(event->type() == QEvent::MouseMove)
{
qDebug() << Q_FUNC_INFO << " pos: " << this->mapFromGlobal(QCursor::pos());
}
return false;
}

QPainter black trace when moving QWidget

I created a small test application with 2 widgets, one inside the other.
I reimplemented the mouse move, press and release events for the inner widget in order to be able to move it inside its bigger parent with drag&drop.
However, when I move it a black trace appears from top and from left. This is how it looks:
Here is my code:
main.cpp:
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPaintEvent>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void paintEvent(QPaintEvent *e);
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "innerwidget.h"
#include <QPainter>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
new InnerWidget(this);
resize(400, 200);
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent* e)
{
QPainter p(this);
p.setBrush(Qt::lightGray);
p.drawRect(e->rect());
}
innerwidget.h:
#ifndef INNERWIDGET_H
#define INNERWIDGET_H
#include <QWidget>
#include <QPaintEvent>
class InnerWidget : public QWidget
{
Q_OBJECT
public:
explicit InnerWidget(QWidget *parent = 0);
~InnerWidget();
protected:
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void paintEvent(QPaintEvent *e);
private:
bool m_leftButtonPressed;
QPoint m_mousePosOnBar;
};
#endif // INNERWIDGET_H
innerwidget.cpp:
#include "innerwidget.h"
#include <QPainter>
#include <QPaintEvent>
#include <QStyleOption>
InnerWidget::InnerWidget(QWidget *parent) : QWidget(parent)
{
setGeometry(10, 10, 100, 100);
setStyleSheet("background-color: red");
}
InnerWidget::~InnerWidget()
{
}
void InnerWidget::mousePressEvent(QMouseEvent* e)
{
if(e->button() == Qt::LeftButton)
{
m_mousePosOnBar = e->pos();
m_leftButtonPressed = true;
}
e->accept();
}
void InnerWidget::mouseReleaseEvent(QMouseEvent* e)
{
if(e->button() == Qt::LeftButton)
{
m_leftButtonPressed = false;
}
e->accept();
}
void InnerWidget::mouseMoveEvent(QMouseEvent* e)
{
if(m_leftButtonPressed)
{
move(e->pos().x() - m_mousePosOnBar.x() + geometry().x(),
e->pos().y() - m_mousePosOnBar.y() + geometry().y());
}
e->accept();
}
void InnerWidget::paintEvent(QPaintEvent* e)
{
Q_UNUSED(e)
QPainter p(this);
QStyleOption opt;
opt.init(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
EDIT:
The trace disappears when I call Widget::repaint but then I would have to install an event filter on InnerWidget and repaint everytime it moves. I would want a cleaner solution without having to use event filters...
Can anyone tell me what is really happening?
Calling QWidget::update() in Widget::paintEvent solved the problem:
void Widget::paintEvent(QPaintEvent* e)
{
QPainter p(this);
p.setBrush(Qt::lightGray);
p.drawRect(e->rect());
update();
}