Blur effect over a QWidget in Qt - c++

Is there any way to blur a widget in Qt? For instance, supose I want to create a 'Loading...' dialog and blur the background (not active window).

This answer is in a series of my overlay-related answers: first, second, third.
It requires some care if you wish for it to work on all platforms. You can't apply effects directly to top-level windows. The hierarchy needs to look as follows:
ContainerWidget
|
+----------+
| |
**Target** Overlay
You apply the effect to the Target widget (say, a QMainWindow). The ContainerWidget is a helper class that keeps the children occupying the full size of the widget. This obviates the need for an explicit zero-margin layout.
The below works, even on a Mac. It wouldn't, had you foregone the ContainerWidget. This works portably on Qt 5 only, unfortunately. On Qt 4, your "cross platform" support excludes Mac :( It works OK on Windows using either Qt 4 (4.8.5) or Qt 5.
// https://github.com/KubaO/stackoverflown/tree/master/questions/overlay-blur-19383427
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class OverlayWidget : public QWidget {
void newParent() {
if (!parent()) return;
parent()->installEventFilter(this);
raise();
}
public:
explicit OverlayWidget(QWidget *parent = {}) : QWidget(parent) {
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
newParent();
}
protected:
//! Catches resize and child events from the parent widget
bool eventFilter(QObject *obj, QEvent *ev) override {
if (obj == parent()) {
if (ev->type() == QEvent::Resize)
resize(static_cast<QResizeEvent*>(ev)->size());
else if (ev->type() == QEvent::ChildAdded)
raise();
}
return QWidget::eventFilter(obj, ev);
}
//! Tracks parent widget changes
bool event(QEvent *ev) override {
if (ev->type() == QEvent::ParentAboutToChange) {
if (parent()) parent()->removeEventFilter(this);
}
else if (ev->type() == QEvent::ParentChange)
newParent();
return QWidget::event(ev);
}
};
class ContainerWidget : public QWidget
{
public:
explicit ContainerWidget(QWidget *parent = {}) : QWidget(parent) {}
void setSize(QObject *obj) {
if (obj->isWidgetType()) static_cast<QWidget*>(obj)->setGeometry(rect());
}
protected:
//! Resizes children to fill the extent of this widget
bool event(QEvent *ev) override {
if (ev->type() == QEvent::ChildAdded) {
setSize(static_cast<QChildEvent*>(ev)->child());
}
return QWidget::event(ev);
}
//! Keeps the children appropriately sized
void resizeEvent(QResizeEvent *) override {
for(auto obj : children()) setSize(obj);
}
};
class LoadingOverlay : public OverlayWidget
{
public:
LoadingOverlay(QWidget *parent = {}) : OverlayWidget{parent} {
setAttribute(Qt::WA_TranslucentBackground);
}
protected:
void paintEvent(QPaintEvent *) override {
QPainter p{this};
p.fillRect(rect(), {100, 100, 100, 128});
p.setPen({200, 200, 255});
p.setFont({"arial,helvetica", 48});
p.drawText(rect(), "Loading...", Qt::AlignHCenter | Qt::AlignTop);
}
};
namespace compat {
#if QT_VERSION >= QT_VERSION_CHECK(5,4,0)
using QT_PREPEND_NAMESPACE(QTimer);
#else
using Q_QTimer = QT_PREPEND_NAMESPACE(QTimer);
class QTimer : public Q_QTimer {
public:
QTimer(QTimer *parent = nullptr) : Q_QTimer(parent) {}
template <typename F> static void singleShot(int period, F &&fun) {
struct Helper : public QObject {
F fun;
QBasicTimer timer;
void timerEvent(QTimerEvent *event) override {
if (event->timerId() != timer.timerId()) return;
fun();
deleteLater();
}
Helper(int period, F &&fun) : fun(std::forward<F>(fun)) {
timer.start(period, this);
}
};
new Helper(period, std::forward<F>(fun));
}
};
#endif
}
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
ContainerWidget base;
QLabel label("Dewey, Cheatem and Howe, LLC.", &base);
label.setFont({"times,times new roman", 32});
label.setAlignment(Qt::AlignCenter);
label.setGraphicsEffect(new QGraphicsBlurEffect);
LoadingOverlay overlay(&base);
base.show();
compat::QTimer::singleShot(2000, [&]{
overlay.hide();
label.setGraphicsEffect({});
});
return a.exec();
}

See QGraphicsBlurEffect Class and QWidget::setGraphicsEffect().

You can refer to this article if you want to apply blur effect on an image. After you create your blurred image you can draw it in QWidget::paintEvent() function.

Related

Expand cursor length QLineEdit?

I'd like to take a normal QLineEdit, and change the shape of the cursor. So with a subclass like so:
class myLineEdit : public QLineEdit
{
Q_OBJECT
signals:
public:
explicit myLineEdit(QWidget * parent = 0)
{
}
protected:
};
And make it so that the cursor is several pixels wide, like that of a Linux terminal. By default, the cursor to indicate text position is very slim.
I assume I need to override something in the paintevent()? What exactly in the paintevent would be responsible for drawing the single pixel blinking line QLineEdit() defaults to? I could not find this information in the documentation.
Use a Qproxystyle:
#include <QtWidgets>
class LineEditStyle: public QProxyStyle
{
Q_OBJECT
Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth)
public:
using QProxyStyle::QProxyStyle;
int cursorWidth() const{
if(m_cursor_width < 0)
return baseStyle()->pixelMetric(PM_TextCursorWidth);
return pixelMetric(PM_TextCursorWidth);
}
void setCursorWidth(int cursorWidth){
m_cursor_width = cursorWidth;
}
int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override
{
if(metric == PM_TextCursorWidth)
if(m_cursor_width > 0)
return m_cursor_width;
return QProxyStyle::pixelMetric(metric, option, widget);
}
private:
int m_cursor_width = -1;
};
class LineEdit: public QLineEdit
{
Q_OBJECT
public:
LineEdit(QWidget *parent = nullptr):
QLineEdit(parent)
{
LineEditStyle *new_style = new LineEditStyle(style());
new_style->setCursorWidth(10);
setStyle(new_style);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LineEdit w;
w.show();
return a.exec();
}
#include "main.moc"

How to create a vertical (rotated) button in Qt with C++

I'd like to create a vertical button in Qt (using C++, not Python), with text rotated 90ยบ either clockwise or counterclockwise. It doesn't seem to be possible with a standard QPushButton.
How could I do it?
In order to create a vertical button in Qt, you can subclass QPushButton so that the dimensions reported by the widget are transposed, and also modify the drawing event to paint the button with the proper alignment.
Here's a class called OrientablePushButton that can be used as a drop-in replacement of the traditional QPushButton but also supports vertical orientation through the usage of setOrientation.
Aspect:
Sample usage:
auto anotherButton = new OrientablePushButton("Hello world world world world", this);
anotherButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
anotherButton->setOrientation(OrientablePushButton::VerticalTopToBottom);
Header file:
class OrientablePushButton : public QPushButton
{
Q_OBJECT
public:
enum Orientation {
Horizontal,
VerticalTopToBottom,
VerticalBottomToTop
};
OrientablePushButton(QWidget * parent = nullptr);
OrientablePushButton(const QString & text, QWidget *parent = nullptr);
OrientablePushButton(const QIcon & icon, const QString & text, QWidget *parent = nullptr);
QSize sizeHint() const;
OrientablePushButton::Orientation orientation() const;
void setOrientation(const OrientablePushButton::Orientation &orientation);
protected:
void paintEvent(QPaintEvent *event);
private:
Orientation mOrientation = Horizontal;
};
Source file:
#include <QPainter>
#include <QStyleOptionButton>
#include <QDebug>
#include <QStylePainter>
OrientablePushButton::OrientablePushButton(QWidget *parent)
: QPushButton(parent)
{ }
OrientablePushButton::OrientablePushButton(const QString &text, QWidget *parent)
: QPushButton(text, parent)
{ }
OrientablePushButton::OrientablePushButton(const QIcon &icon, const QString &text, QWidget *parent)
: QPushButton(icon, text, parent)
{ }
QSize OrientablePushButton::sizeHint() const
{
QSize sh = QPushButton::sizeHint();
if (mOrientation != OrientablePushButton::Horizontal)
{
sh.transpose();
}
return sh;
}
void OrientablePushButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStylePainter painter(this);
QStyleOptionButton option;
initStyleOption(&option);
if (mOrientation == OrientablePushButton::VerticalTopToBottom)
{
painter.rotate(90);
painter.translate(0, -1 * width());
option.rect = option.rect.transposed();
}
else if (mOrientation == OrientablePushButton::VerticalBottomToTop)
{
painter.rotate(-90);
painter.translate(-1 * height(), 0);
option.rect = option.rect.transposed();
}
painter.drawControl(QStyle::CE_PushButton, option);
}
OrientablePushButton::Orientation OrientablePushButton::orientation() const
{
return mOrientation;
}
void OrientablePushButton::setOrientation(const OrientablePushButton::Orientation &orientation)
{
mOrientation = orientation;
}

How to mix mouseEnterEvent and mouseMove?

in my application I try to connect nodes with lines. I use a QGraphicsView with a QGraphicsScene and my own QGraphicsItems. Now if I click on an item I want to draw a line to another node. To give a visual feedback, the goal should change color if the mouse hovers over the goal. The basics works so far, but my problem is that if I drag a line with the mouse (via mouseMoveEvent), I do not get any hoverEvents any more. I replicated the behaviour with this code:
Header File:
#pragma once
#include <QtWidgets/Qwidget>
#include <QGraphicsItem>
#include <QGraphicsScene>
class HaggiLearnsQt : public QWidget
{
Q_OBJECT
public:
HaggiLearnsQt(QWidget *parent = Q_NULLPTR);
};
class MyScene : public QGraphicsScene
{
public:
MyScene(QObject* parent = 0);
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
};
class MyItem : public QGraphicsItem
{
public:
MyItem(QGraphicsItem* parent = Q_NULLPTR);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
bool mouseOverItem;
};
Implementation:
#include "HaggiLearnsQt.h"
#include <QMessageBox>
#include <QFrame>
#include <QHBoxLayout>
#include <QGraphicsView>
MyScene::MyScene(QObject* parent)
{}
void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
MyItem::MyItem(QGraphicsItem* parent) : mouseOverItem(false)
{
setAcceptHoverEvents(true);
}
QRectF MyItem::boundingRect() const
{
return QRectF(-50, -50, 50, 50);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush b = QBrush(Qt::black);
if(mouseOverItem)
b = QBrush(Qt::yellow);
painter->setBrush(b);
painter->drawRect(boundingRect());
}
void MyItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
mouseOverItem = true;
QGraphicsItem::hoverEnterEvent(event);
}
void MyItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
mouseOverItem = false;
QGraphicsItem::hoverLeaveEvent(event);
}
HaggiLearnsQt::HaggiLearnsQt(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout* layout = new QHBoxLayout(this);
MyScene* graphicsScene = new MyScene();
QGraphicsView* graphicsView = new QGraphicsView();
graphicsView->setRenderHint(QPainter::RenderHint::Antialiasing, true);
graphicsView->setScene(graphicsScene);
layout->addWidget(graphicsView);
graphicsView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
graphicsView->setMinimumHeight(200);
graphicsView->setMinimumWidth(200);
graphicsView->setStyleSheet("background-color : gray");
MyItem* myitem = new MyItem();
myitem->setPos(50, 50);
graphicsScene->addItem(myitem);
}
And the default main.cpp:
#include "HaggiLearnsQt.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HaggiLearnsQt w;
w.show();
return a.exec();
}
If you run the code, a box appears in the middle of the window. If you hover over the box, it changes color. Now try to klick outside the box and drag wiht pressed button into the box. The box does not receive a hover and does not change color.
So my question is: Can I somehow change the item while I move the mouse with a pressed button?
You can get the hovered item passing mouseEvent->scenePos() to the QGraphicsScene::itemAt method inside the scene mouse move event handler.
Have a pointer to a MyItem instance, in MyScene:
class MyScene : public QGraphicsScene
{
MyItem * hovered;
//...
initialize it to zero in MyScene constructor:
MyScene::MyScene(QObject* parent)
{
hovered = 0;
}
then use it to track the current highlighted item (if there's one):
void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if(mouseEvent->buttons())
{
QGraphicsItem * item = itemAt(mouseEvent->scenePos(), QTransform());
MyItem * my = dynamic_cast<MyItem*>(item);
if(my != 0)
{
qDebug() << mouseEvent->scenePos();
if(!my->mouseOverItem)
{
my->mouseOverItem = true;
my->update();
hovered = my;
}
}
else
{
if(hovered != 0)
{
hovered->mouseOverItem = false;
hovered->update();
hovered = 0;
}
}
}
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
The line if(mouseEvent->buttons()) at the beginning prevents the check to be performed if no mouse button is held.
Don't forget to initialize mouseOverItem to false in MyItem constructor:
MyItem::MyItem(QGraphicsItem* parent) : mouseOverItem(false)
{
setAcceptHoverEvents(true);
mouseOverItem = false;
}

Qt Quick: Cannot render QQuickPaintedItem's children

I'm trying to render some QQuickPaintedItems and their children, but I got only the parents rendered. Please tell me what's wrong with my code.
Graph.h (my custom QQuickPaintedItem):
#include <QQuickPaintedItem>
#include "voltrule.h"
class Graph : public QQuickPaintedItem
{
Q_OBJECT
public:
explicit Graph(QQuickItem *parent = 0);
void paint(QPainter *painter);
private:
VoltRule *_rule;
};
Graph::Graph(QQuickItem *parent) :
QQuickPaintedItem(parent)
{
_rule = new VoltRule(this);
_rule->setParentItem(this);
}
void Graph::paint(QPainter *painter)
{
QRect rect(10, 20, 100, 200);
QPen pen(Qt::green);
painter->setPen(pen);
painter->drawRect(rect);
}
VoltRule.h
#include <QQuickPaintedItem>
class VoltRule : public QQuickPaintedItem
{
Q_OBJECT
public:
explicit VoltRule(QQuickItem *parent = 0);
void paint(QPainter *painter);
signals:
public slots:
};
VoltRule.cpp
#include "voltrule.h"
#include <QPainter>
VoltRule::VoltRule(QQuickItem *parent) :
QQuickPaintedItem(parent)
{
setFlag(QQuickItem::ItemHasContents);
}
void VoltRule::paint(QPainter *painter)
{
QRect rect(10, 20, 100, 200);
QPen pen(Qt::white);
painter->setPen(pen);
painter->drawRect(rect);
}
main.qml
ApplicationWindow {
width: 1367
height: 766
Graph{
anchors.fill:parent
}
}
Thanks in advance
jbh answered the question; just setting a size in the VoltRule constructor solves the problem:
VoltRule::VoltRule(QQuickItem *parent) :
QQuickPaintedItem(parent)
{
setSize(QSizeF(100, 200));
}
It looks a bit strange because the child is drawing white on top of the parent's green lines, erasing just part of the parent's lines.
Here is code that sets the size based on the parent's size. Resize the window and you will see the child updates without an explicit "update()" call:
VoltRule.h
class VoltRule : public QQuickPaintedItem
{
Q_OBJECT
public:
explicit VoltRule(QQuickItem *parent = 0);
void paint(QPainter *painter);
private:
QQuickItem* parentItem();
void onParentChanged();
void onParentWidthChanged();
void onParentHeightChanged();
};
VoltRule.cpp
QQuickItem* VoltRule::parentItem()
{
return qobject_cast<QQuickItem*>(parent());
}
VoltRule::VoltRule(QQuickItem *parent) :
QQuickPaintedItem(parent)
{
connect(this, &QQuickItem::parentChanged, this, &VoltRule::onParentChanged);
onParentChanged();
}
void VoltRule::paint(QPainter *painter)
{
QRect rect(.1*width(), .1*height(), .8*width(), .8*height());
QPen pen(Qt::blue);
painter->setPen(pen);
painter->drawRect(rect);
}
void VoltRule::onParentChanged()
{
// disconnect signals from previous parent, if there was one
disconnect();
if (const auto obj = parentItem()) {
connect(obj, &QQuickItem::widthChanged, this, &VoltRule::onParentWidthChanged);
connect(obj, &QQuickItem::heightChanged, this, &VoltRule::onParentHeightChanged);
onParentWidthChanged();
onParentHeightChanged();
}
}
void VoltRule::onParentWidthChanged()
{
if (const auto obj = parentItem()) {
setWidth(obj->width());
}
}
void VoltRule::onParentHeightChanged()
{
if (const auto obj = parentItem()) {
setHeight(obj->height());
}
}

Catching QSpinBox text field mouseEvents

I have a reimplemented QDoubleSpinBox. I would like to catch mouseDoubleClickEvent to enable the user to change singleStep by way of QInputDialog::getDouble().
My problem is that when I reimplement the mouseDoubleClickEvent I only catch double clicks that occur over the arrow buttons. I actually want to ignore double clicks that occur in the arrows and only catch double clicks that occur in the text field. I have a feeling that I need to reimplement the mouseDoubleClickEvent of a child of the QDoubleSpinBox, but I'm not sure how to reimplement a child event nor how to select the correct child See my attempt at limiting to a child QRect in code: I think I need to specify which child...?
Thanks.
Edit: corrected class declaration/definition name mismatch.
MyQDoubleSpinBox.h
class MyQDoubleSpinBox : public QDoubleSpinBox
{
Q_OBJECT
public:
MyQDoubleSpinBox(QString str, QWidget *parent = 0);
~MyQDoubleSpinBox();
public slots:
void setStepSize(double step);
private:
double stepSize;
QString name;
protected:
void mouseDoubleClickEvent(QMouseEvent *e);
};
MyQDoubleSpinBox.cpp
#include "MyQDoubleSpinBox.h"
MyQDoubleSpinBox::MyQDoubleSpinBox(QString str, QWidget *parent)
: QDoubleSpinBox(parent), stepSize(1.00), name(str)
{
this->setMinimumWidth(150);
this->setSingleStep(stepSize);
this->setMinimum(0.0);
this->setMaximum(100.0);
}
MyQDoubleSpinBox::~MyQDoubleSpinBox()
{
}
void MyQDoubleSpinBox::setStepSize(double step)
{
this->setSingleStep(step);
}
void MyQDoubleSpinBox::mouseDoubleClickEvent(QMouseEvent *e)
{
if( this->childrenRect().contains(e->pos()) )
{
bool ok;
double d = QInputDialog::getDouble(this,
name,
tr("Step Size:"),
this->singleStep(),
0.0,
1000.0,
2,
&ok);
if(ok)
this->setSingleStep(d);
}
}
A bit of the hack getting ref to child, but it works =)
MyQDoubleSpinBox.h:
class MyQDoubleSpinBox : public QDoubleSpinBox
{
Q_OBJECT
public:
MyQDoubleSpinBox(QString str, QWidget *parent = 0);
~MyQDoubleSpinBox();
public slots:
void setStepSize(double step);
private:
double stepSize;
QString name;
protected:
bool eventFilter(QObject *, QEvent *e);
};
MyQDoubleSpinBox.cpp
MyQDoubleSpinBox::MyQDoubleSpinBox(QString str, QWidget *parent)
: QDoubleSpinBox(parent), stepSize(1.00), name(str)
{
this->setMinimumWidth(150);
this->setSingleStep(stepSize);
this->setMinimum(0.0);
this->setMaximum(100.0);
QLineEdit *editor = this->findChild<QLineEdit *>("qt_spinbox_lineedit");
editor->installEventFilter(this);
}
MyQDoubleSpinBox::~MyQDoubleSpinBox()
{
}
void MyQDoubleSpinBox::setStepSize(double step)
{
this->setSingleStep(step);
}
bool MyQDoubleSpinBox::eventFilter(QObject *, QEvent *e)
{
if (e->type() == QMouseEvent::MouseButtonDblClick)
{ bool ok;
double d = QInputDialog::getDouble(this,
name,
tr("Step Size:"),
this->singleStep(),
0.0,
1000.0,
2,
&ok);
if(ok)
this->setSingleStep(d);
}
return false;
}
Instead of overwriting events, i got ref to underlying QLineEdit and assigned event filter to it. In event filter catch only mouse double click.
You have direct access to QLineEdit using this->lineEdit()
MyQDoubleSpinBox::MyQDoubleSpinBox(QString str, QWidget *parent)
: QDoubleSpinBox(parent), stepSize(1.00), name(str)
{
this->setMinimumWidth(150);
this->setSingleStep(stepSize);
this->setMinimum(0.0);
this->setMaximum(100.0);
QLineEdit *editor = this->lineEdit(); // change here
editor->installEventFilter(this);
}