QT筆記之自訂視窗拖拽移動

來源:互聯網
上載者:User

標籤:

1.QT自訂標題列,拖拽標題列移動視窗(只能拖拽標題,其他位置無法拖拽)

方法一:

轉載:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html

 .h檔案中

 1 //自己重新實現拖動操作 2 protected: 3  4 void mouseMoveEvent ( QMouseEvent * event ); 5  6 void mousePressEvent ( QMouseEvent * event ); 7  8 void mouseReleaseEvent(QMouseEvent *); 9 10 private:11 //自己重新實現拖動操作12 QPoint mousePosition;13 bool isMousePressed;

.cpp檔案中

//標題列的長度const static int pos_min_x = 0;const static int pos_max_x = 800-40;const static int pos_min_y = 0;const static int pos_max_y = 20;//自己實現的視窗拖動操作void MainWindow::mousePressEvent(QMouseEvent *event){mousePosition = event->pos();//只對標題列範圍內的滑鼠事件進行處理if (mousePosition.x()<=pos_min_x)return;if ( mousePosition.x()>=pos_max_x)return;if (mousePosition.y()<=pos_min_y )return;if (mousePosition.y()>=pos_max_y)return;isMousePressed = true;}void MainWindow::mouseMoveEvent(QMouseEvent *event){if ( isMousePressed==true ){QPoint movePot = event->globalPos() - mousePosition;move(movePot);}}void MainWindow::mouseReleaseEvent(QMouseEvent *event){isMousePressed=false;}

方法二:(可以拖拽視窗任意位置)

轉載:http://blog.sina.com.cn/s/blog_a6fb6cc90101au8r.html

自訂介面步驟:

1.設定標題列隱藏

//設定標題列隱藏並設定位於頂層

setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

//可擷取滑鼠跟蹤效果

setMouseTracking(true);

//注意:Qt::WindowStaysOnTopHint這個很重要,如果沒有這句話即使是自訂介面成功了,介面可以拖動,但也還存在問題,那就是介面能夠拖動到工作列之下!

 

2、 聲明變數與滑鼠事件    QPoint move_point; //移動的距離    bool mouse_press; //滑鼠按下    //滑鼠按下事件    void mousePressEvent(QMouseEvent *event);    //滑鼠釋放事件    void mouseReleaseEvent(QMouseEvent *event);    //滑鼠移動事件    void mouseMoveEvent(QMouseEvent *event);
3、定義滑鼠事件        void LoginDialog::mousePressEvent(QMouseEvent *event)    {          if(event->button() == Qt::LeftButton)        {            mouse_press = true;            //滑鼠相對於表單的位置(或者使用event->globalPos() - this->pos())            move_point = event->pos();;        }    }    void LoginDialog::mouseMoveEvent(QMouseEvent *event)    {        //若滑鼠左鍵被按下        if(mouse_press)        {            //滑鼠相對於螢幕的位置            QPoint move_pos = event->globalPos();            //移動主表單位置            this->move(move_pos - move_point);        }    }    void LoginDialog::mouseReleaseEvent(QMouseEvent *event)    {          //設定滑鼠為未被按下        mouse_press = false;    }

方法三:

 拖拽自訂視窗任意位置移動

轉載:http://twyok.blog.163.com/blog/static/81229303201321545850433/

.h檔案中

protected:void mousePressEvent(QMouseEvent *)void mouseMoveEvent(QMouseEvent* );private:QPoint last;//儲存滑鼠按下的位置

.cpp檔案中

void xxxDialog::mousePressEvent(QMouseEvent *e) {     last = e->globalPos(); } void xxxDialog::mouseMoveEvent(QMouseEvent *e) {     if(e->buttons()== Qt::LeftButton)     {         QPoint newpos = e->globalPos();         QPoint upleft = mapToParent(newpos - last); //計算距原位置的位移        move(upleft);         last = newpos; //更新原位置到最新的位置    } } 這時已經可以用滑鼠手動視窗到任意位置。                    

 

QT筆記之自訂視窗拖拽移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.