一个父类窗口上 显示一个半透明子类窗口,该子类窗口上有控件,控件不透明,半透明的地方可以透过点击父类窗口
要能实现的代码。
现在用了置顶和去除frame来实现,只能单独实现。求解#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->webView->setUrl(QUrl("qrc:/page/inputtest.html"));
setWindow();
}Widget::~Widget()
{
delete ui;
}
void Widget::setWindow()
{
QWidget *temp = new QWidget(ui->webView);
QLabel *label = new QLabel(temp);
QPushButton *pushButton1 = new QPushButton(temp);
pushButton1->setText("lin");
temp->resize(400, 300);
//ui->horizontalLayout->addWidget(temp);
temp->setWindowFlags(Qt::FramelessWindowHint);
//label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(150, 50, 191, 91)); label->setPixmap(QPixmap(":/page/images/butterfly.png"));
temp->setAttribute(Qt::WA_TranslucentBackground);
temp->setAttribute(Qt::WA_NoSystemBackground, false); QPalette pal = palette();
QColor bg = pal.window().color();
bg.setAlpha(100);
pal.setColor(QPalette::Window, bg);
temp->setPalette(pal);
temp->setAttribute(Qt::WA_StyledBackground);
//temp->setWindowOpacity(0.7); temp->show();}
要能实现的代码。
现在用了置顶和去除frame来实现,只能单独实现。求解#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->webView->setUrl(QUrl("qrc:/page/inputtest.html"));
setWindow();
}Widget::~Widget()
{
delete ui;
}
void Widget::setWindow()
{
QWidget *temp = new QWidget(ui->webView);
QLabel *label = new QLabel(temp);
QPushButton *pushButton1 = new QPushButton(temp);
pushButton1->setText("lin");
temp->resize(400, 300);
//ui->horizontalLayout->addWidget(temp);
temp->setWindowFlags(Qt::FramelessWindowHint);
//label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(150, 50, 191, 91)); label->setPixmap(QPixmap(":/page/images/butterfly.png"));
temp->setAttribute(Qt::WA_TranslucentBackground);
temp->setAttribute(Qt::WA_NoSystemBackground, false); QPalette pal = palette();
QColor bg = pal.window().color();
bg.setAlpha(100);
pal.setColor(QPalette::Window, bg);
temp->setPalette(pal);
temp->setAttribute(Qt::WA_StyledBackground);
//temp->setWindowOpacity(0.7); temp->show();}
唯一的缺点: 主窗体不能一直是活动窗体,当点击透明窗体的时候,主窗体会失去焦点,不过不影响功能
附上代码,希望能帮到楼主(变态的需求)#ifndef TWIDGET_H
#define TWIDGET_H
#include <QWidget>
class QLineEdit;
class QMoveEvent;
class TWidget: public QWidget
{
Q_OBJECT
public:
TWidget(QWidget *parent);
public slots:
void parentMoved(QMoveEvent *event);
private:
QLineEdit *edit;
QWidget *parent;
void mousePressEvent(QMouseEvent *e);
};#endif // TWIDGET_H#include "TWidget.h"
#include <QMoveEvent>
#include <QLineEdit>
#include <QPalette>
#include <QApplication>
TWidget::TWidget(QWidget *parent)
:QWidget(parent),parent(parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground, false);
setWindowFlags(Qt::FramelessWindowHint|Qt::Tool);
setAttribute(Qt::WA_NoMousePropagation, false);
setAttribute(Qt::WA_MouseTracking,true);
setObjectName("tw"); QPalette p = this->palette();
p.setColor(QPalette::Window, QColor(0,255,0,110));
setPalette(p); edit = new QLineEdit(this);
edit->setObjectName("edit"); parentMoved(0);
}void TWidget::parentMoved(QMoveEvent *event)
{
QPoint xy = parent->mapToGlobal(QPoint(10,60));
setGeometry(xy.x(),xy.y(),100,200);
}void TWidget::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)
{
QPoint point = mapToGlobal(e->pos());
point = parent->mapFromGlobal(point);
QPoint p(5,5);
QList<QWidget*> list = parent->findChildren<QWidget*>();
foreach(QWidget *qw, list)
{
if(qw->parent() == parent)
{
QPoint qp = qw->mapToParent(QPoint(0,0));
QRect r(qp, QSize(qw->size()));
if(r.contains(point))
{
QMouseEvent *e1= new QMouseEvent(QEvent::MouseButtonPress,p,Qt::LeftButton,0,0);
QApplication::postEvent(qw, e1);
QMouseEvent *e2= new QMouseEvent(QEvent::MouseButtonRelease,p,Qt::LeftButton,0,0);
QApplication::postEvent(qw, e2);
break;
}
}
}
}
return QWidget::mousePressEvent(e);
}
以上是定义的透明窗体
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>namespace Ui {
class Widget;
}class Widget : public QWidget
{
Q_OBJECTpublic:
explicit Widget(QWidget *parent = 0);
~Widget();protected:
void changeEvent(QEvent *e);
void moveEvent(QMoveEvent *e);
private:
Ui::Widget *ui;
protected slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
signals:
void moved(QMoveEvent *e);
};#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
#include "TWidget.h"
#include <QList>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
TWidget *w = findChild<TWidget*>("tw");
if(w)
return;
w = new TWidget(this);
connect(this,SIGNAL(moved(QMoveEvent*)), w, SLOT(parentMoved(QMoveEvent*)));
w->show();
}
void Widget::moveEvent(QMoveEvent *e)//让透明窗体随父窗体移动
{
emit moved(e);
}
void Widget::on_pushButton_2_clicked()
{
qDebug()<<"ok,pushButton_2 is clicked!";}void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}以上是主窗体UI文件就是往界面上拖了两个按钮,一个pushButton在右下角,用于产生透明窗体, pushButton_2在左上角,刚好让产生的透明窗体挡住, 以便测试在透明窗体上单击鼠标左键时接受转发来的消息是否有效功能出来了,有空我将它完善成一个功能齐全点的透明控件