Here's even a better one. It allows you to drag the transparent widget around inside the main window.
#include "twidget.h"
#include <QPainter>
#include <QMouseEvent>
twidget
::twidget(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
mTranspW = new xwww(this);
mTranspW->setVisible(true);
mTranspW->setFixedSize(200, 200);
mTranspW->move(0, 0);
}
twidget::~twidget()
{
}
{
}
{
if( (e->buttons()&Qt::LeftButton))
{
QPoint topLeft
= mapToGlobal
(mTranspW
->rect
().
topLeft());
mTranspW->move(pos - topLeft);
}
}
:QWidget(p, Qt
::FramelessWindowHint | Qt
::Widget) {
}
xwww::~xwww()
{
}
{
p.setOpacity(0.5);
p.fillRect(rect(), Qt::blue);
}
#include "twidget.h"
#include <QPainter>
#include <QMouseEvent>
twidget::twidget(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
mTranspW = new xwww(this);
QPalette p = mTranspW->palette();
mTranspW->setVisible(true);
mTranspW->setFixedSize(200, 200);
mTranspW->move(0, 0);
}
twidget::~twidget()
{
}
void twidget::mousePressEvent(QMouseEvent *e)
{
}
void twidget::mouseMoveEvent(QMouseEvent *e)
{
if( (e->buttons()&Qt::LeftButton))
{
QPoint pos = e->globalPos();
QPoint topLeft = mapToGlobal(mTranspW->rect().topLeft());
mTranspW->move(pos - topLeft);
}
}
xwww::xwww(QWidget* p)
:QWidget(p, Qt::FramelessWindowHint | Qt::Widget)
{
}
xwww::~xwww()
{
}
void xwww::paintEvent(QPaintEvent* e)
{
QPainter p( this );
p.setOpacity(0.5);
p.fillRect(rect(), Qt::blue);
}
To copy to clipboard, switch view to plain text mode
The header:
#ifndef TWIDGET_H
#define TWIDGET_H
#include <QtGui/QMainWindow>
#include "ui_twidget.h"
{
Q_OBJECT
public:
~xwww();
protected:
};
{
Q_OBJECT
public:
twidget
(QWidget *parent
= 0, Qt
::WFlags flags
= 0);
~twidget();
protected:
private:
Ui::twidgetClass ui;
xwww* mTranspW;
};
#endif // TWIDGET_H
#ifndef TWIDGET_H
#define TWIDGET_H
#include <QtGui/QMainWindow>
#include "ui_twidget.h"
class xwww : public QWidget
{
Q_OBJECT
public:
xwww(QWidget*);
~xwww();
protected:
virtual void paintEvent(QPaintEvent*);
};
class twidget : public QMainWindow
{
Q_OBJECT
public:
twidget(QWidget *parent = 0, Qt::WFlags flags = 0);
~twidget();
protected:
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseMoveEvent(QMouseEvent *e);
private:
Ui::twidgetClass ui;
xwww* mTranspW;
};
#endif // TWIDGET_H
To copy to clipboard, switch view to plain text mode
Regards
Bookmarks