注册 登录
编程论坛 QT论坛

Qt 中拖动无边框窗口遇到了问题,求大神帮忙

小码农 发布于 2014-11-19 13:29, 3004 次点击
我做了一个窗口,把它设为了无边框,这样就无法实现拖动了。我网上看了好久,是要重写
void mousePressEvent (QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
这三个函数,我重写了,但拖动窗口的时候程序就崩掉了,不知道为什么?
源码如下:
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMouseEvent>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
protected:
    void mousePressEvent (QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

private:
    Ui::MainWindow *ui;
    bool shouldMove;        //是否要改变窗口位置
    QPoint *formerMainPos;      //移动前窗口的位置
    QPoint *formerMousePos;  //第一次按下左键时鼠标的位置

};

#endif // MAINWINDOW_H

CPP文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setMouseTracking(true);
    this->shouldMove=false;
    this->setWindowFlags(Qt::FramelessWindowHint);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mousePressEvent (QMouseEvent *event)
    {
        if (event->button()==Qt::LeftButton){
            shouldMove = true;
            *formerMainPos =this->pos();
            *formerMousePos = event->globalPos();
            event->accept();
        }
    }

void MainWindow::mouseReleaseEvent(QMouseEvent *event){
        if (event->button()==Qt::LeftButton){
            shouldMove = false;
        }
    }

void MainWindow::mouseMoveEvent ( QMouseEvent * event )
    {
        QPoint point = event->globalPos();

        if (shouldMove&&(event->button()==Qt::LeftButton))
        {
            int x = point.x()-formerMousePos->x();
            int y = point.y()-formerMousePos->y();
            this->move(formerMainPos->x()+x,formerMainPos->y()+y);
            event->accept();
        }
    }

没多少分了,大神莫嫌弃。
3 回复
#2
小码农2014-11-20 23:49
没有大神帮忙吗?
#3
qq5423671842018-09-10 15:43
回复 楼主 小码农
.h


#ifndef CODEBOX_H

#define CODEBOX_H

#include<QWidget>

#include<QGroupBox>

#include<QLabel>

#include<QPushButton>

#include<QMouseEvent>

#include<QPoint>

#include<QRect>

#include<QDesktopWidget>

class CodeBox:public QWidget

{

      Q_OBJECT

public:

   explicit CodeBox(QWidget *parent=0);

    QDesktopWidget* m_desktopWidget;   //屏幕

    QRect m_screenRect;

    short  m_nActScreenX ;

    short  m_nActScreenY ;



    QGroupBox * m_LoginBox;

    QLabel *m_LoginLabek;  //窗口信息

    QLabel *m_Hint;    //提示信息

    QLabel *m_CodeHint;//二维码提示信息

    QLabel *m_LoginCode;//二维码

    QPushButton *m_CloseLogin;


    void m_InitLogin(); //二维码窗口



protected:

    QPoint mMousePoint;

    bool mDragWindow;

    void mousePressEvent(QMouseEvent *e);

    void mouseReleaseEvent(QMouseEvent *e);

    void mouseMoveEvent(QMouseEvent *e);

};


#endif // CODEBOX_H


.cpp


#include "codebox.h"

#include<QApplication>

CodeBox::CodeBox(QWidget *parent):QWidget(parent)

{

    m_desktopWidget = QApplication::desktop();

    m_screenRect=m_desktopWidget->screenGeometry();

    m_nActScreenX = m_screenRect.width();

    m_nActScreenY = m_screenRect.height();

    this->setGeometry(0,0,m_nActScreenX,m_nActScreenY);

    this->setWindowFlags(Qt::FramelessWindowHint|Qt::Tool);

    this->setWindowOpacity(1);

    this->setAttribute(Qt::WA_TranslucentBackground);


    m_InitLogin();

   this->setAttribute(Qt::WA_DeleteOnClose);

}


void CodeBox::m_InitLogin()

{

    m_LoginBox=new QGroupBox(this);


    m_LoginBox->setStyleSheet(QString::fromUtf8("border-image: url(:/image/WindowCode.png);"));

    m_LoginBox->setGeometry(m_nActScreenX/2-250,m_nActScreenY/2-150,500,450);


    short x=m_LoginBox->width();

    short y=m_LoginBox->height();


    m_LoginLabek=new QLabel(m_LoginBox);

    m_LoginLabek->setStyleSheet(QString::fromUtf8("border-image: url(:/image/touming.png);"));

    QPalette pa;

    pa.setColor(QPalette::WindowText,Qt::white);

    m_LoginLabek->setPalette(pa);

    QFont ft;

    ft.setPointSize(14);

    m_LoginLabek->setFont(ft);

    m_LoginLabek->setText(QString::fromLocal8Bit("注册会员"));

    m_LoginLabek->setGeometry(x/2-40,0,80,40);


    m_Hint=new QLabel(m_LoginBox);   //提示信息

    ft.setPointSize(12);

    m_Hint->setFont(ft);

    m_Hint->setStyleSheet(QString::fromUtf8("border-image: url(:/image/touming.png);"));

    m_Hint->setText(QString::fromLocal8Bit("您当前不是会员,请按照提示注册会员"));

    m_Hint->setGeometry(x/2-120,45,300,40);


    m_CodeHint=new QLabel(m_LoginBox);   //注册

   // m_CodeHint->setFont(ft);

    m_CodeHint->setStyleSheet(QString::fromUtf8("border-image: url(:/Code/CodeHint.png);"));

  //  m_CodeHint->setText(QString::fromLocal8Bit(""));

    m_CodeHint->setGeometry(x/2-80,y/2+100,160,60);


    m_CloseLogin=new QPushButton(m_LoginBox);   //关闭键

    m_CloseLogin->setCursor(Qt::PointingHandCursor);

    m_CloseLogin->setGeometry(x-40,15,20,20);

    m_CloseLogin->setStyleSheet(QString::fromUtf8("border-image: url(:/image/close.png);"));




   m_LoginCode=new QLabel(m_LoginBox);         //注册二维码


   m_LoginCode->setStyleSheet(QString::fromLocal8Bit("border-image: url(:/Code/文化路店.jpg);"));


  m_LoginCode->setGeometry(x/2-100,y/2-100-20,200,200);


   m_LoginBox->hide();

   connect(m_CloseLogin,SIGNAL(clicked(bool)),m_LoginBox,SLOT(close()));

}


void CodeBox::mousePressEvent(QMouseEvent *e)

{

    if(e->button()==Qt::LeftButton)

        {

            mMousePoint=e->globalPos()-m_LoginBox->pos();

            e->accept();

            mDragWindow=true;

         }

}


void CodeBox::mouseReleaseEvent(QMouseEvent *e)

{

     mDragWindow=false;

}


void CodeBox::mouseMoveEvent(QMouseEvent *e)

{

    if(mDragWindow && (e->buttons() && Qt::LeftButton))

       {

          m_LoginBox->move(e->globalPos()-mMousePoint);

           e->accept();

       }

}

#4
qq5423671842018-09-10 15:45
回复 2楼 小码农
没看你的代码  这是我写的  我也是一枚小白    你可以参考一下

1