2016-06-09 66 views
2

我找到了一個在無框窗口上設置邊框的例子,但它不可拖動。我怎樣才能使無框窗口可拖動?特別是如果我能看到一個例子,它會很棒。這是我的示例代碼(通常代碼更長,這就是爲什麼有許多庫只是不介意它們);PyQt5可拖動無框窗口

from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton, 
          QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout, 
          QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar, 
          QTextEdit,QDialog,QFrame,QProgressBar 
          ) 
from PyQt5 import QtCore, QtWidgets, QtGui 
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette 
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer 

import sys 

class cssden(QMainWindow): 
    def __init__(self): 
     super().__init__() 


     self.mwidget = QMainWindow(self) 
     self.setWindowFlags(QtCore.Qt.FramelessWindowHint) 


     #size 
     self.setFixedSize(320, 450) 
     self.center 


     #label 
     self.lbl = QLabel(self) 
     self.lbl.setText("test") 
     self.lbl.setStyleSheet("background-color: rgb(0,0,0);" 
           "border: 1px solid red;" 
           "color: rgb(255,255,255);" 
           "font: bold italic 20pt 'Times New Roman';") 
     self.lbl.setGeometry(5,5,60,40) 

     self.show() 

    #center 
    def center(self): 
     qr = self.frameGeometry() 
     cp = QDesktopWidget().availableGeometry().center() 
     qr.moveCenter(cp) 
     self.move(qr.topLeft()) 

app = QApplication(sys.argv) 
app.setStyleSheet("QMainWindow{background-color: darkgray;border: 1px solid black}") 

ex = cssden() 
sys.exit(app.exec_()) 

回答

4

您需要自己處理鼠標事件。

  • 我們需要在mousePressEvent添加事件,這將讓我們最後的窗口中點擊
  • 然後的地方,我們將添加一個mouseMoveEvent,這將計算最後單擊點之間的距離,當前的鼠標位置。我們將根據這個距離移動窗口。

這是固定的代碼:

from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton, 
          QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout, 
          QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar, 
          QTextEdit,QDialog,QFrame,QProgressBar 
          ) 
from PyQt5 import QtCore, QtWidgets, QtGui 
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette 
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer, QPoint 

import sys 

class cssden(QMainWindow): 
    def __init__(self): 
     super().__init__() 


     self.mwidget = QMainWindow(self) 
     self.setWindowFlags(QtCore.Qt.FramelessWindowHint) 


     #size 
     self.setFixedSize(320, 450) 
     self.center() 


     #label 
     self.lbl = QLabel(self) 
     self.lbl.setText("test") 
     self.lbl.setStyleSheet("background-color: rgb(0,0,0);" 
           "border: 1px solid red;" 
           "color: rgb(255,255,255);" 
           "font: bold italic 20pt 'Times New Roman';") 
     self.lbl.setGeometry(5,5,60,40) 

     self.oldPos = self.pos() 

     self.show() 

    #center 
    def center(self): 
     qr = self.frameGeometry() 
     cp = QDesktopWidget().availableGeometry().center() 
     qr.moveCenter(cp) 
     self.move(qr.topLeft()) 

    def mousePressEvent(self, event): 
     self.oldPos = event.globalPos() 

    def mouseMoveEvent(self, event): 
     delta = QPoint (event.globalPos() - self.oldPos) 
     #print(delta) 
     self.move(self.x() + delta.x(), self.y() + delta.y()) 
     self.oldPos = event.globalPos() 

app = QApplication(sys.argv) 
app.setStyleSheet("QMainWindow{background-color: darkgray;border: 1px solid black}") 

ex = cssden() 
sys.exit(app.exec_()) 
+0

的偉大工程。請問有沒有鼠標事件的例子,如懸停鼠標等?只有關於C++的例子,我很難將它們轉換成Python。 – GLHF

+0

是的,我知道有很多Python例子。我認爲最好的方法是瞭解如何將Qt C++代碼轉換爲Python。它不像開始時那樣複雜。 –

+0

這是一個相關的[代碼示例](https://gist.github.com/zed/77e705caa840bab117057952f9ca6191) – jfs