I’m not sure how to make a common utilities file and use the methods within it.
Here is an example utilities file that centres a window on the screen:
class center():
fg = self.frameGeometry()
cs = QtGui.QDesktopWidget().availableGeometry().center()
fg.moveCenter(cs)
self.move(fg.topLeft())
Here is an example UI:
import os, sys
import puppetUtils
from PyQt4 import QtCore, QtGui
class puppetUI(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle('Puppet')
self.setObjectName('PuppetMainWindow')
self.resize(1280, 720)
puppetUtils.center(self)
self.show()
app = QtGui.QApplication(sys.argv)
window = puppetUI()
sys.exit(app.exec_())
What is the correct way to call to puppetUtils.center? I have googled around, tried many ways, but it just doesn’t work. This is an example – one of many, that I’d use to have my application function without copy/pasting functions throughout various classes, etc.
Other code I need to implement:
def mouseMoveEvent(self, event):
#create the mouse 'Grab & Move'
if self.moving: self.move(event.globalPos()-self.offset)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.moving = True; self.offset = event.pos()
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.moving = False
First, change from
to
because you are actually trying to define a utility function, so it should be a
definstead ofclass.Then inside the
centerfunction, changeselftovar(or whatever you want to name the function parameter)