这次实现了滑动窗口了.
刚开始想的用定时器,每隔一段时间往外滑一点,但是发现有些莫名其妙解决不了的问题,貌似与定时器本身的机制有关,无奈只得放弃.选择用windows自带的Sleep函数,也是每隔一段时间repaint一下,这次实现了.
然后设定一个状态值,鼠标每次点击,改变其状态,根据这个判断鼠标的滑动时间是否起作用.
代码:
#include <windows.h>
#include <QMouseEvent>
#include <QMessageBox>
FloatingWindow::FloatingWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ui.pushButton->setFlat(true);
ui.pushButton->setStyleSheet("background-image:url(./pic/2smallsetdown.jpg)");
ui.widget->setGeometry(-ui.widget->width(),ui.widget->y(),ui.widget->width(),ui.widget->height());
//ui.widget->setFeatures(QDockWidget::NoDockWidgetFeatures);
atout=false;
status=true;
this->setMouseTracking(true);
//ui.widget->setMouseTracking(true);
ui.centralWidget->setMouseTracking(true);
ui.axWidget->setMouseTracking(true);
//添加flash
connect(ui.axWidget,SIGNAL(FSCommand(QString,QString)),this,SLOT(flashAction(QString,QString)));
ui.axWidget->dynamicCall("LoadMovie(long,string)",0,"E://MLCN//topo_show.swf");
ui.axWidget->dynamicCall("Play()");
}
FloatingWindow::~FloatingWindow()
{
}
void FloatingWindow::mouseMoveEvent(QMouseEvent *event){
int x=event->x();
int y=event->y();
if (status)
{
if (x<20&&atout==false)
{
movein();
}
else if(atout==true&&x>120)
{
moveout();
}
}
}
void FloatingWindow::movein(){
for (pos=-ui.widget->width();pos<=0;pos=pos+8)
{
ui.widget->setGeometry(QRect(pos, ui.widget->y(), ui.widget->width(), ui.widget->height()));
ui.widget->repaint();
//ui.axWidget->repaint();
Sleep(1);
}
atout=true;
}
void FloatingWindow::moveout(){
for (pos=0;pos>=-ui.widget->width();pos=pos-ui.widget->width())
{
ui.widget->setGeometry(QRect(pos, ui.widget->y(), ui.widget->width(), ui.widget->height()));
ui.widget->repaint();
ui.axWidget->show();
Sleep(1);
}
atout=false;
}
void FloatingWindow::mouseDoubleClickEvent( QMouseEvent * event ){
int x=event->x();
int y=event->y();
int x1=ui.widget->x();
int y1=ui.widget->y();
int wi=ui.widget->width();
int he=ui.widget->height();
if (x>x1&&x<x1+wi&&y>y1&&y<y1+he)
{
status=!status;
}
}
void FloatingWindow::flashAction(QString command,QString args)
{
QMessageBox::information(this,command,args);
}
void FloatingWindow::on_pushButton_clicked()
{
status=!status;
}
void FloatingWindow::on_pushButton_2_clicked()
{
QApplication::exit();
}