您的位置:首页 > 编程语言 > Qt开发

Qt模拟全屏播放鼠标隐藏显示效果

2018-02-23 15:45 232 查看
.h文件

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include <QTimer>

class MyWidget : public QWidget
{
Q_OBJECT

public:
MyWidget(QWidget *parent = 0);
~MyWidget();

protected:
void paintEvent(QPaintEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void resizeEvent(QResizeEvent *event);
private:
QWidget* dlg1;
QWidget* dlg2;
QTimer m_timer;

public slots:
void OnTimerOut();

};

#endif // MYWIDGET_H


.cpp文件

#include "MyWidget.h"
#include <QPainter>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QImage>
#include <QApplication>
#include <QPushButton>

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setStyleSheet("background-color:rgba(60, 60, 60);");
dlg1 = new QWidget(this);
dlg1->setGeometry(0,0,this->rect().width(),40);
dlg1->setStyleSheet("background-color:rgba(75, 75, 75,200);");
dlg2 = new QWidget(this);
dlg2->setGeometry(0, this->rect().height() - 100, this->rect().width(), 100);
dlg2->setStyleSheet("background-color:rgba(75, 75, 75,200); ");

QHBoxLayout *lay = new QHBoxLayout(dlg2);
QPushButton* btn = new QPushButton("pushbutton",dlg2);
btn->setFixedSize(80, 30);
QSpacerItem *spce1 = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
QSpacerItem *spce2 = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
lay->addSpacerItem(spce1);
lay->addWidget(btn);
lay->addSpacerItem(spce2);
dlg2->setLayout(lay);

this->setMouseTracking(true);
//this->setWindowFlags(Qt::FramelessWindowHint);

//用于隐藏鼠标
m_timer.setInterval(2000);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(OnTimerOut()));

}

MyWidget::~MyWidget()
{
}

void MyWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

QPainter p(this);
p.setPen(Qt::NoPen);
p.setBrush(Qt::white);
p.drawRect(rect());

//QImage img("C:\\Users\\conglin\\Desktop\\7.png");
//p.drawImage((rect().width() - img.width())*0.5, (rect().height() - img.height())*0.5, img);
//p.drawImage(rect(), img);
}

void MyWidget::OnTimerOut()
{
QApplication::setOverrideCursor(Qt::BlankCursor);
}

void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
QApplication::setOverrideCursor(Qt::ArrowCursor);
if (dlg1->geometry().contains(event->pos()))
{
dlg1->setVisible(true);
m_timer.stop();
}
else
{
dlg1->setVisible(false);
if (!m_timer.isActive() && !dlg2->isVisible())
{
m_timer.start();
}
}

if (dlg2->geometry().contains(event->pos()))
{
dlg2->setVisible(true);
m_timer.stop();
}
else
{
dlg2->setVisible(false);
if (!m_timer.isActive() && !dlg1->isVisible())
{
m_timer.start();
}
}
}

void MyWidget::resizeEvent(QResizeEvent *event)
{
dlg1->setGeometry(0, 0, this->rect().width(), 40);
dlg2->setGeometry(0, this->rect().height()-100, this->rect().width(), 100);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  全屏效果 模拟