70945


[Pyqt5 draggable QPushButton - Stack
Overflow](https://stackoverflow.com/questions/59116811/pyqt5-draggable-
qpushbutton)

上記を見てボタンやテキストボックス、画像等をドラッグ＆ドロップで移動可能にできたのですが、移動したオブジェクト情報（位置やテキスト内容）の保存／読み込みはどうすれば行えるのでしょうか？


CC BY-SA 4.0
2020-10-05T21:57:11.360
70940
2020-10-06T00:46:37.053
2020-10-06T00:44:00.400
3060
42199
question
python python3 pyqt5
ドラッグ＆ドロップで移動したオブジェクトを保存するには？


[QSettings](https://doc.qt.io/qtforpython/PySide2/QtCore/QSettings.html)を使う方法が手ごろではないでしょうか。

**サンプルコード**

[code]

    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit
    from PyQt5.QtCore import QSettings, QPoint, QSize, Qt
    
    class myApp(QWidget):
        def __init__(self):
            super(myApp, self).__init__()
    
        def initUI(self):
            self.settings = QSettings("my_settings.ini", QSettings.IniFormat)
    
            self.button = self.findChild(DragButton)
            self.edit = self.findChild(QLineEdit)
    
            # 設定ファイルからボタンの位置とテキストを書き出す(なければ第二引数の既定値を使う)
            self.button.move(self.settings.value("button_pos", QPoint(0, 50)))
            self.edit.setText(self.settings.value("edit_text", "Hello World!"))
    
        def closeEvent(self, e):
            # 設定ファイルにボタンの位置とテキストを書き出す
            self.settings.setValue("button_pos", self.button.pos())
            self.settings.setValue("edit_text", self.edit.text())
    
            e.accept()
    
    class DragButton(QPushButton):
    
        def mousePressEvent(self, event):
            self.__mousePressPos = None
            self.__mouseMovePos = None
            if event.button() == Qt.LeftButton:
                self.__mousePressPos = event.globalPos()
                self.__mouseMovePos = event.globalPos()
    
            super(DragButton, self).mousePressEvent(event)
    
        def mouseMoveEvent(self, event):
            if event.buttons() == Qt.LeftButton:
                # adjust offset from clicked point to origin of widget
                currPos = self.mapToGlobal(self.pos())
                globalPos = event.globalPos()
                diff = globalPos - self.__mouseMovePos
                newPos = self.mapFromGlobal(currPos + diff)
                self.move(newPos)
    
                self.__mouseMovePos = globalPos
    
            super(DragButton, self).mouseMoveEvent(event)
    
        def mouseReleaseEvent(self, event):
            if self.__mousePressPos is not None:
                moved = event.globalPos() - self.__mousePressPos 
                if moved.manhattanLength() > 3:
                    event.ignore()
    
            super(DragButton, self).mouseReleaseEvent(event)
    
    def clicked():
        print ("click as normal!")
    
    if __name__ == "__main__":
        app = QApplication([])
        w   = myApp()
        w.resize(800,600)
    
        button = DragButton("Drag", w)
        button.clicked.connect(clicked)
    
        edit = QLineEdit(w)
    
        w.initUI() # 設定ファイル読み出し
        w.show()
        app.exec_()
    
[/code]

**参考にした本家SOの質問**

[How to remember last geometry of PyQt
application?](https://stackoverflow.com/q/33869721)  
[QSettings(): How to save to current working
directory](https://stackoverflow.com/q/7962292)


CC BY-SA 4.0
2020-10-06T00:46:37.053
70945
2020-10-06T00:46:37.053
9820
70940
answer