Joeyos's Blog Software Engineer

Qt窗口在屏幕顶端自动隐藏

2016-07-03
Quan Zhang
Qt

GithubPage:https://zhangquan1995.github.io

头文件声明

    /* 记录窗口当前的位置 */
    void enterEvent(QEvent *);
    void leaveEvent(QEvent *);
    QPoint StartPos;
    QPoint EndPos;
    QPoint mousePos;

获取屏幕信息

    QDesktopWidget *desktopwidget = QApplication::desktop();
    QRect deskrect = desktopwidget->availableGeometry();
    //QRect screenrect = desktopwidget->screenGeometry();
    //打印屏幕信息
    //int scrcount = desktopwidget->screenCount();//屏幕个数
    //qCritical("deskrect.w==%s\t",qPrintable(QString::number(deskrect.width())));
    //qCritical("deskrect.h==%s\t",qPrintable(QString::number(deskrect.height())));
    //qCritical("screenCount==%s\t",qPrintable(QString::number(scrcount)));

边界隐藏函数

//边界隐藏
void Widget::enterEvent(QEvent *)
{//鼠标进入
    QRect rc;
    QRect rect;
    rect = this->geometry();
    rc.setRect(rect.x(),rect.y(),rect.width(),rect.height());
    if(rect.top()<0)
    {
        int i;
        //rect.setX(rc.x());
        //rect.setY(0);
        for(i=0;i<400;i++)//小于500-2即可,否则不断出现
        {
            move(rc.x(),-2+i-400);
            Sleep(1);
        }
        //move(rc.x(),-2);
    }
    else if(rect.right()>deskrect.width())
    {
        int i;
        for(i=0;i<700;i++)//小于750-2
        {
            move(deskrect.width()-750+2-i+700,rc.y());//只显示一点边
            Sleep(1);
        }
    }
    //===============左边上边同时小于零出错,有待解决=============
}
void Widget::leaveEvent(QEvent *)
{//鼠标离开
    QRect rc;
    QRect rect;
    rect = this->geometry();
    rc.setRect(rect.x(),rect.y(),rect.width(),rect.height());//左上角x,左上角y,宽,高
    if(rect.top()<0)
    {
        int i;
        for(i=0;i<rc.height();i++)
        {
            //move(rc.x(),-rc.height()+2);
            move(rc.x(),5-i);//只显示一点边
            Sleep(1);
        }
    }
    else if(rect.right()>deskrect.width())
    {
        int i;
        for(i=0;i<750;i++)
        {
            move(deskrect.width()+i-750-5,rc.y());//只显示一点边
            Sleep(1);
        }
    }
}

此博客均属原创或译文,欢迎转载但请注明出处 GithubPage:https://zhangquan1995.github.io


Comments

Content