Skip to content

Instantly share code, notes, and snippets.

@Wojtechnology
Created March 21, 2015 22:03
Show Gist options
  • Save Wojtechnology/93067a39131253869c69 to your computer and use it in GitHub Desktop.
Save Wojtechnology/93067a39131253869c69 to your computer and use it in GitHub Desktop.
// Header File
#ifndef BUBBLE_H
#define BUBBLE_H
#include <QWidget>
namespace Ui {
class Bubble;
}
class Bubble : public QWidget
{
Q_OBJECT
public:
explicit Bubble(QWidget *parent = 0);
virtual QSize sizeHint() const;
~Bubble();
static const int WIDTH = 300, HEIGHT = 300;
protected:
virtual void paintEvent(QPaintEvent *paintEvent);
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
void repositionBubble();
private:
Ui::Bubble *ui;
bool mMoving;
QPoint offset;
};
#endif // BUBBLE_H
#include <QWidget>
// C++ File
#include "bubble.h"
#include "ui_bubble.h"
#include <QtGui>
Bubble::Bubble(QWidget *parent) :
QWidget(parent, Qt::FramelessWindowHint),
ui(new Ui::Bubble)
{
setAttribute(Qt::WA_TranslucentBackground);
ui->setupUi(this);
}
Bubble::~Bubble()
{
delete ui;
}
QSize Bubble::sizeHint() const {
return QSize(300, 300);
}
void Bubble::paintEvent(QPaintEvent *paintEvent){
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(255, 255, 255, 255));
painter.drawEllipse(0, 0, width(), height());
}
void Bubble::mousePressEvent(QMouseEvent* event)
{
offset = event->pos();
if(event->button() == Qt::LeftButton)
{
mMoving = true;
}
}
void Bubble::mouseMoveEvent(QMouseEvent* event)
{
if( event->buttons().testFlag(Qt::LeftButton) && mMoving)
{
this->move(mapToParent(event->pos() - offset));
}
}
void Bubble::mouseReleaseEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
mMoving = false;
}
}
void Bubble::repositionBubble(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment