Double buffering in Jython - jython

recently I started learning Jython and now I have rather simply problem. I would like to improve quality of my animation. Unfortunately I don't know how to add double buffering to my applet . Could you help me?
Best regards!
from javax.swing import JToolBar
from javax.swing import JButton
from javax.swing import JFrame
import time
from java import awt
from java.awt import BorderLayout
class Canvas(awt.Canvas):
u"Canvas - drawing area"
def __init__(self,winSize = 400):
self.play = False
self.background=awt.Color.black
self.winSize = winSize
self.l = 0
def playSim(self, play):
if play == True:
self.play = True
self.repaint()
else: self.play = False
def paint(self, g):
g.fillRect(50, int(self.winSize/4), self.l, int(self.winSize/2))
if self.l < self.winSize: self.l += 1
else: self.l = 0
time.sleep(0.02)
if self.play == True: self.repaint()
class Example(JFrame):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
winSize = 600
toolbar = JToolBar()
self.playButton = JButton("Start", actionPerformed=self.playButtonPress )
toolbar.add(self.playButton)
self.add(toolbar, BorderLayout.NORTH)
self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
self.setSize(winSize, winSize)
self.setResizable(False)
self.setLocationRelativeTo(None)
self.setVisible(True)
self.canvas = Canvas(winSize)
self.getContentPane().add(self.canvas)
self.setTitle("TEST")
self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
def playButtonPress(self, e):
if self.playButton.getLabel() == "Start":
self.canvas.playSim(True)
self.playButton.setLabel("Stop")
else:
self.playButton.setLabel("Start")
self.canvas.playSim(False)
if __name__ == '__main__':
Example()

I solved my recent problem:
from javax.swing import JToolBar
from javax.swing import JButton
from javax.swing import JFrame
import time
from java import awt
from java.awt import BorderLayout
class Canvas(awt.Canvas):
u"Canvas - drawing area"
def __init__(self,winSize = 400):
self.play = False
self.background=awt.Color.black
self.winSize = winSize
self.l = 0
self.bi = BufferedImage(winSize, winSize, BufferedImage.TYPE_INT_RGB)
self.offScreenGraphics = self.bi.getGraphics()
def playSim(self, play):
if play == True:
self.play = True
self.repaint()
else: self.play = False
def paint(self, g):
self.offScreenGraphics.fillRect(50, int(self.winSize/4), self.l, int(self.winSize/2))
if self.l < self.winSize: self.l += 1
else: self.l = 0
g.drawImage(self.bi, 0, 0, None)
time.sleep(0.02)
if self.play == True: self.repaint()
class Example(JFrame):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
winSize = 600
toolbar = JToolBar()
self.playButton = JButton("Start", actionPerformed=self.playButtonPress )
toolbar.add(self.playButton)
self.add(toolbar, BorderLayout.NORTH)
self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
self.setSize(winSize, winSize)
self.setResizable(False)
self.setLocationRelativeTo(None)
self.setVisible(True)
self.canvas = Canvas(winSize)
self.getContentPane().add(self.canvas)
self.setTitle("TEST")
self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
def playButtonPress(self, e):
if self.playButton.getLabel() == "Start":
self.canvas.playSim(True)
self.playButton.setLabel("Stop")
else:
self.playButton.setLabel("Start")
self.canvas.playSim(False)
if __name__ == '__main__':
Example()
Now I've another(rather trivial) problem:
How can I make from this python file *the class file* which would be ready to publish it on website as an applet?

Related

PYQT5 imported MDI Subwindow Scaled Up

I'm working on a gui tool-- I'm building it with Pyqt5. I'm speifically NOT using QT designer. I'm using an MDI widget to keep everyhing a bit tidier.
Furthermore, so that my code is more crisp and less redundant, I'm building out each child window in a separate window in the same directory and then just importing the appropriate class from the individual files.
The problem is, whenver I import the subwindows, the are scaled up in the MDI subwindow. I am at a loss as to how I can address this. Has anyone expierenced something similar? I've added simplied code for my MDI subwindow below, followed by the code for one of the subwindows thta I'm importing. Any assistance would be greatly appreciated.
Import sys
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtGui import QWindow
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QMdiArea, QAction, QMdiSubWindow, QTextEdit
from Admission_Tool import *
from COPD_tool import *
from Discharge_Template import DischargeWindow
from Note_Template import *
class MDIWindow(QMainWindow):
count = 0
htntoolcount = 0
copdcount = 0
def __init__(self):
super().__init__()
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
self.setStyleSheet('font-size: 10pt; font-family: Times;')
self.setStyleSheet("QPushButton{font-size: 10pt;}")
self.setStyleSheet("QLabel{font-size: 10pt;}")
self.mdi.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.mdi.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
#####Setting up main Menu Labels and Buttons#####
self.mainMenuWidget = QWidget(self)
self.mmWidgetLayout = QVBoxLayout()
self.mainMenuWidget.setLayout(self.mmWidgetLayout)
self.mainMenuWidget.setWindowTitle("Main Menu")
self.mmButton1 = QPushButton("Note Setup Tool", self)
self.mmButton2 = QPushButton("Lab Entry Tool", self)
self.mmButton3 = QPushButton("Follow Up Tools", self)
self.mmButton4 = QPushButton("ROS Generator", self)
self.mmButton5 = QPushButton("Physical Exam Generator", self)
self.mmButton6 = QPushButton("Cardon Matrix", self)
self.mmButton7 = QPushButton("Trilogy Matrix", self)
self.mmButton8 = QPushButton("ASC Matrix", self)
self.mmButton9 = QPushButton("Proactive Email", self)
self.mmWidgetLayout.addWidget(self.mmButton1)
self.mmWidgetLayout.addWidget(self.mmButton2)
self.mmWidgetLayout.addWidget(self.mmButton3)
self.mmWidgetLayout.addWidget(self.mmButton4)
self.mmWidgetLayout.addWidget(self.mmButton5)
self.mmWidgetLayout.addWidget(self.mmButton6)
self.mmWidgetLayout.addWidget(self.mmButton7)
self.mmWidgetLayout.addWidget(self.mmButton8)
self.mmWidgetLayout.addWidget(self.mmButton9)
self.mdi.addSubWindow(self.mainMenuWidget)
self.mainMenuWidget.show()
##adding actions to main menu buttons##
self.mmButton1.clicked.connect(self.noteSetupFunc)
self.mmButton2.clicked.connect(self.admissionTool)
self.mmButton3.clicked.connect(self.COPDToolFunc)
self.setWindowTitle("Proactive Charting Tool")
def noteSetupFunc(self):
self.noteSUButtFuncWidget = NOTEWindow()
self.mdi.addSubWindow(self.noteSUButtFuncWidget)
self.noteSUButtFuncWidget.show()
# Setting MAin Menu Widget for NOtes#
# self.NOTEmainMenuWidget = QWidget(self)
# self.noteMMWidgetLayout = QVBoxLayout()
# self.NOTEmainMenuWidget.setLayout(self.noteMMWidgetLayout)
# self.NOTEmainMenuWidget.setWindowTitle("Note Menu")
#
# self.NotemmButton1 = QPushButton("Admission", self)
# self.NotemmButton2 = QPushButton("Discharge", self)
# self.NotemmButton3 = QPushButton("Diag Testing", self)
# self.NotemmButton4 = QPushButton("Acute Visit", self)
# self.NotemmButton5 = QPushButton("Fall", self)
# self.NotemmButton6 = QPushButton("Med Review / Comp", self)
# self.NotemmButton7 = QPushButton("Coumadin", self)
# self.NotemmButton8 = QPushButton("Noncompliance", self)
# self.NotemmButton9 = QPushButton("Simple Note", self)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton1)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton2)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton3)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton4)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton5)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton6)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton7)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton8)
# self.noteMMWidgetLayout.addWidget(self.NotemmButton9)
# self.mdi.addSubWindow(self.NOTEmainMenuWidget)
# self.NOTEmainMenuWidget.show()
# self.NOTEMMButton2.clicked.connect(self.dcwindowFunc)
def admissionTool(self):
self.admitToolWidget = admitWindow()
self.mdi.addSubWindow(self.admitToolWidget)
self.admitToolWidget.show()
def COPDToolFunc(self):
self.copdToolWidget = COPDWindow()
self.mdi.addSubWindow(self.copdToolWidget)
self.copdToolWidget.show()
MDIWindow.copdcount = MDIWindow.copdcount + 1
def dcwindowFunc(self):
self.dcwindowWidget = DischargeWindow()
self.mdi.addSubWindow(self.dcwindowWidget)
self.dcwindowWidget.show()
def htnButton(self):
self.htnsubwidgets = QWidget(self)
self.htnMAinLayout = QHBoxLayout()
self.htnLeftLayout = QVBoxLayout()
self.htnRightLayout = QVBoxLayout()
##Adding secondary layours to main layouts##
self.htnMAinLayout.addLayout(self.htnLeftLayout)
self.htnMAinLayout.addLayout(self.htnRightLayout)
##Adding htnRightLayout widgets##
self.htnLabel1 = QLabel("Date of last HTN Med change", self)
self.htnDateEdit = QDateEdit(self)
self.htnDateEdit.setGeometry(QtCore.QRect(10, 30, 120, 45))
self.htnDateEdit.setObjectName("Med_change_dateEdit")
self.htnDateEdit.setDate(QtCore.QDate.currentDate())
self.htnDateEdit.setCalendarPopup(True)
self.htnQ2Label = QLabel("Med most recently changed?", self)
self.htnQ2LineEdit = QLineEdit()
self.htnQ3Label = QLabel("Most Recent bP Measurement", self)
self.htnQ3LineEdit = QLineEdit()
self.htnQ4Label = QLabel("Overall Bp Control?", self)
self.htnQ4Combobox = QComboBox()
self.htnQ4ComboboxList = ["Hypotension", "BP Well Controlled", "Some Hypertensive Episodes",
"Regular Hypertension"]
self.htnQ4Combobox.addItems(self.htnQ4ComboboxList)
self.htnq5Label = QLabel("Resident's current meds?", self)
self.htnq5LineEdit = QLineEdit()
self.htnCompleteButton = QPushButton("Complete", self)
self.htnCompleteButton.setMaximumWidth(75)
##Adding Widgets to Left Layout##
self.htnLeftLayout.addWidget(self.htnLabel1)
self.htnLeftLayout.addWidget(self.htnDateEdit)
self.htnLeftLayout.addWidget(self.htnQ2Label)
self.htnLeftLayout.addWidget(self.htnQ2LineEdit)
self.htnLeftLayout.addWidget(self.htnQ3Label)
self.htnLeftLayout.addWidget(self.htnQ3LineEdit)
self.htnLeftLayout.addWidget(self.htnQ4Label)
self.htnLeftLayout.addWidget(self.htnQ4Combobox)
self.htnLeftLayout.addWidget(self.htnq5Label)
self.htnLeftLayout.addWidget(self.htnq5LineEdit)
self.htnLeftLayout.addWidget(self.htnCompleteButton)
## Adding right widgets##
self.htntextedit = QTextEdit()
self.htntextedit.setMinimumWidth(200)
##Adding right widgets##
self.htnRightLayout.addWidget(self.htntextedit)
self.htnCompleteButton.clicked.connect(self.htnCompleteFunc)
self.htnsubwidgets.setLayout(self.htnMAinLayout)
self.mdi.addSubWindow(self.htnsubwidgets)
self.htnsubwidgets.show()
def htnCompleteFunc(self):
self.htnTotalList = [self.htnDateEdit.text(),self.htnQ2LineEdit.text(),self.htnQ3LineEdit.text(),self.htnQ4Combobox.currentText(),self.htnq5LineEdit.text()]
self.htntextedit.setText(f"The resient's most recent blood pressure med change ({self.htnTotalList[0]}) change was {self.htnTotalList[1]}. The residnet's most recent BP is {self.htnTotalList[2]}. The resdient is currently receiving {self.htnTotalList[4]}. Overall hypertension status: {self.htnTotalList[3]}")
def main():
app = QApplication(sys.argv)
font = QFont('Times', 10)
app.setFont(font)
mdiwindow = MDIWindow()
mdiwindow.show()
app.exec_()
if __name__ == '__main__':
main()ere
An example of one of my subwindowss is as follows:
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QMdiArea, QAction, QMdiSubWindow, QTextEdit
from Admission_Tool import *
from COPD_tool import *
class NOTEWindow(QWidget):
def __init__(self):
super().__init__()
self.notemdi = QWidget()
self.setStyleSheet('font-size: 10pt; font-family: Times;')
self.setStyleSheet("QPushButton{font-size: 10pt;}")
self.setStyleSheet("QLabel{font-size: 10pt;}")
self.notemdi.setMaximumSize(500,500)
#Setting MAin Menu Widget for this nested MDI#
self.NOTEmainMenuWidget = QWidget(self)
self.noteMMWidgetLayout = QVBoxLayout()
self.NOTEmainMenuWidget.setLayout(self.noteMMWidgetLayout)
self.NOTEmainMenuWidget.setWindowTitle("Note Menu")
self.mmButton1 = QPushButton("Admission", self)
self.mmButton2 = QPushButton("Discharge", self)
self.mmButton3 = QPushButton("Diagnostic Testing", self)
self.mmButton4 = QPushButton("Acute Visit", self)
self.mmButton5 = QPushButton("Fall", self)
self.mmButton6 = QPushButton("Med Review/Comp", self)
self.mmButton7 = QPushButton("Coumadin", self)
self.mmButton8 = QPushButton("Noncompliance", self)
self.mmButton9 = QPushButton("Weight Loss", self)
self.mmButton10 = QPushButton("Simple Note",self)
self.noteMMWidgetLayout.addWidget(self.mmButton1)
self.noteMMWidgetLayout.addWidget(self.mmButton2)
self.noteMMWidgetLayout.addWidget(self.mmButton3)
self.noteMMWidgetLayout.addWidget(self.mmButton4)
self.noteMMWidgetLayout.addWidget(self.mmButton5)
self.noteMMWidgetLayout.addWidget(self.mmButton6)
self.noteMMWidgetLayout.addWidget(self.mmButton7)
self.noteMMWidgetLayout.addWidget(self.mmButton8)
self.noteMMWidgetLayout.addWidget(self.mmButton9)
self.noteMMWidgetLayout.addWidget(self.mmButton10)
self.NOTEmainMenuWidget.show()
def main():
app = QApplication(sys.argv)
font = QFont('Times', 10)
app.setFont(font)
mdiwindow = NOTEWindow()
mdiwindow.show()
app.exec_()
if __name__ == '__main__':
main()

Drag and Drop not working in `QFileModelSystem`

I'm trying to make a drag and drop behavior in QFileSystemModel but because I have no experience in making a drag and drop before, I tried it first on QTreeView. (I attached the video of the behavior)
Now that I'm fine with the behavior I want, I then just changed the model to QFileSystemModel but sadly It's not working. So I tried to read the QFileSystemModel, QTreeView, and Drag and Drop from Qt and I ended up with the code below:
The code I ended up with:
import os
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MQTreeView(QTreeView):
def __init__(self, model):
super().__init__()
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
# self.setDragDropMode(QAbstractItemView.InternalMove)
self.setModel(model)
self.setDragDropMode(QAbstractItemView.DragDrop)
self.setRootIndex(model.index(os.path.dirname(os.path.abspath("__file__"))))
self.setDefaultDropAction(Qt.MoveAction)
self.viewport().setAcceptDrops(True)
def dragEnterEvent(self, event):
m = event.mimeData()
if m.hasUrls():
event.accept()
return
event.ignore()
# return super().dragEnterEvent(event)
def dropEvent(self, event):
print("[drop event] - dropped")
if event.source():
QTreeView.dropEvent(self, event)
else:
ix = self.indexAt(event.pos())
model = self.model()
if ix.isValid():
if not model.isDir(ix):
ix = ix.parent() # In case of folder/Dir
pathDir = model.filePath(ix)
else:
# for empty drag and drop
pathDir = model.rootPath()
m = event.mimeData()
if m.hasUrls():
urlLocals = [url for url in m.urls() if url.isLocalFile()]
accepted = False
for urlLocal in urlLocals:
path = urlLocal.toLocalFile()
info = QFileInfo(path)
n_path = QDir(pathDir).filePath(info.fileName())
o_path = info.absoluteFilePath()
if n_path == o_path:
continue
if info.isDir():
QDir().rename(o_path, n_path)
else:
qfile = QFile(o_path)
if QFile(n_path).exists():
n_path += "(copy)"
qfile.rename(n_path)
print(f"added -> {info.fileName()}")
accepted = True
if accepted:
event.acceptProposedAction()
# return super().dropEvent(event)
class AppDemo(QWidget):
def __init__(self):
super().__init__()
# -- right -- #
self.model1 = QFileSystemModel()
self.model1.setRootPath(os.path.dirname(os.path.abspath("__file__")))
self.view1 = MQTreeView(self.model1)
# -- left -- #
self.model2 = QFileSystemModel()
self.model2.setRootPath(os.path.dirname(os.path.abspath("__file__")))
self.view2 = MQTreeView(self.model2)
# -- layout -- #
layout = QHBoxLayout(self)
layout.addWidget(self.view1)
layout.addWidget(self.view2)
app = QApplication(sys.argv)
main = AppDemo()
main.show()
app.exec_()
The code above is still not doing the behavior I want but I'm pretty sure that something else is wrong and it is not with the overridden function (dragEnterEvent and dropEvent). My best guess is that I didn't set properly the correct way QTreeView accepts the drops although I'm not really sure.
My Question: What is wrong with my Implementation? Is it the way I accept drops or it is something else?
Found what's wrong! I didn't override the dragMoveEvent method. I need to override the dragMoveEvent to make sure that the drag will not be forbidden.
I need to accept all drag event in the dragEnterEvent:
def dragEnterEvent(self, event):
event.accept()
Then I need to filter the events in the dragMoveEvent:
def dragMoveEvent(self, event):
m = event.mimeData()
if m.hasUrls():
event.accept()
print("[dropEnterEvent] - event accepted")
return
event.ignore()
I attached the video and code of the working behavior below.
The final implementation:
import os
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MQTreeView(QTreeView):
def __init__(self, model, path):
super().__init__()
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setModel(model)
self.setDragDropMode(QAbstractItemView.DragDrop)
self.setRootIndex(model.index(path))
self.setDefaultDropAction(Qt.MoveAction)
self.viewport().setAcceptDrops(True)
def dragEnterEvent(self, event):
event.accept()
def dragMoveEvent(self, event):
m = event.mimeData()
if m.hasUrls():
event.accept()
print("[dropEnterEvent] - event accepted")
return
event.ignore()
def dropEvent(self, event):
print("[drop event] - dropped")
if event.source():
ix = self.indexAt(event.pos())
model = self.model()
if ix.isValid():
if not model.isDir(ix):
ix = ix.parent()
pathDir = model.filePath(ix)
else:
# for empty drag and drop
pathDir = model.rootPath()
m = event.mimeData()
if m.hasUrls():
urlLocals = [url for url in m.urls() if url.isLocalFile()]
accepted = False
for urlLocal in urlLocals:
path = urlLocal.toLocalFile()
info = QFileInfo(path)
destination = QDir(pathDir).filePath(info.fileName())
source = info.absoluteFilePath()
if destination == source:
continue # means they are in the same folder
if info.isDir():
QDir().rename(source, destination)
else:
qfile = QFile(source)
if QFile(destination).exists():
n_info = QFileInfo(destination)
destination = n_info.canonicalPath() + QDir.separator() + n_info.completeBaseName() + " (copy)"
if n_info.completeSuffix(): # for moving files without suffix
destination += "." + n_info.completeSuffix()
qfile.rename(destination)
print(f"added -> {info.fileName()}") # for debugging
accepted = True
if accepted:
event.acceptProposedAction()
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
cwd = "test/"
nw = "test copy/"
# -- right -- #
self.model1 = QFileSystemModel()
self.model1.setRootPath(os.path.dirname(cwd))
self.view1 = MQTreeView(self.model1, cwd)
# -- left -- #
self.model2 = QFileSystemModel()
self.model2.setRootPath(os.path.dirname(nw))
self.view2 = MQTreeView(self.model2, nw)
# -- layout -- #
layout = QHBoxLayout(self)
layout.addWidget(self.view1)
layout.addWidget(self.view2)
app = QApplication(sys.argv)
main = AppDemo()
main.show()
app.exec_()

Rewritten QLabel class seems wrong in PyQt5

I rewrite the QLabel class to select ROI on the QLabel, aimed to acheive the cv2.selectROI function. But still wrong......
This is the code:
The QMainWindows Code:
class MyGui(QMainWindow):
def __init__(self, parent=None):
super(MyGui, self).__init__(parent)
self.init_rect = [0, 0, 0, 0]
self.tracker = None
self.first_frame = True
self.video_name = r'G:\dataset\example_Trim.mp4'
self.setupUi()
self.cap = None
self.status = self.statusBar() # 实例化创建状态栏
self.status.showMessage('Author', 10000) # 显示状态栏信息
self.timer_camera = QTimer()
self.show_windows = roiLabel()
self.show_windows.setFixedSize(1500, 800)
self.show_windows.move(50, 10)
self.show_windows.setStyleSheet("QLabel{background-color:gray;}")
def setupUi(self):
self.setObjectName("MainWindow")
self.setFixedSize(1600, 900) # 设置窗口的大小
self.setWindowFlag(QtCore.Qt.WindowMinimizeButtonHint) # 去掉最小化按钮
self.setWindowTitle('基于Transformer的视频目标跟踪方法研究') # 设置窗口标题
self.setWindowIcon(QIcon(r'F:\PythonDoc\trans_tt_backup\gui\pic\Windows_icon.png')) # 设置图标
self.move_center()
And the rewritten QLabel class is :
class roiLabel(QLabel): # 利用QLabel绘制ROI
def __init__(self):
super().__init__()
self.open_keyboard_flag = False
self.open_mouse_flag = False
self.draw_roi_flag = False
self.select_roi_flag = False
self.clear_flag = False
self.x0 = 0
self.y0 = 0
self.x1 = 0
self.y1 = 0
self.rect = QRect()
self.label_show = None
# 按下鼠标时,记录左上角坐标
def mousePressEvent(self, event):
if self.open_mouse_flag is True:
self.select_roi_flag = True
self.x0 = event.x()
self.y0 = event.y()
The problem is:
I want the 'python' windows is embed in mainwindows

Synchronize two QGraphicsView with different images

I would like to show two images next to each other, such that when I zoom or pan on one image the other image follows along. My current approach is to emit a viewUpdated event after resolving mouse events. The event contains the viewportTransformation and is used to update the transform in the other view. This sort of works for the zoom part, but panning does not work.
The code below is based on the qt5 version provided in this answer: https://stackoverflow.com/a/35514531/185475
from PyQt5 import QtCore, QtGui, QtWidgets
# Code from https://stackoverflow.com/a/35514531
class PhotoViewer(QtWidgets.QGraphicsView):
photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
viewUpdated = QtCore.pyqtSignal(QtGui.QTransform)
def __init__(self, parent):
super(PhotoViewer, self).__init__(parent)
self._zoom = 0
self._empty = True
self._scene = QtWidgets.QGraphicsScene(self)
self._photo = QtWidgets.QGraphicsPixmapItem()
self._scene.addItem(self._photo)
self.setScene(self._scene)
self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
self.setFrameShape(QtWidgets.QFrame.NoFrame)
def hasPhoto(self):
return not self._empty
def fitInView(self, scale=True):
rect = QtCore.QRectF(self._photo.pixmap().rect())
if not rect.isNull():
self.setSceneRect(rect)
if self.hasPhoto():
unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
self.scale(1 / unity.width(), 1 / unity.height())
viewrect = self.viewport().rect()
scenerect = self.transform().mapRect(rect)
factor = min(viewrect.width() / scenerect.width(),
viewrect.height() / scenerect.height())
self.scale(factor, factor)
self._zoom = 0
def setPhoto(self, pixmap=None):
self._zoom = 0
if pixmap and not pixmap.isNull():
self._empty = False
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
self._photo.setPixmap(pixmap)
else:
self._empty = True
self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
self._photo.setPixmap(QtGui.QPixmap())
self.fitInView()
def wheelEvent(self, event):
if self.hasPhoto():
if event.angleDelta().y() > 0:
factor = 1.25
self._zoom += 1
else:
factor = 0.8
self._zoom -= 1
if self._zoom > 0:
self.scale(factor, factor)
elif self._zoom == 0:
self.fitInView()
else:
self._zoom = 0
self.viewUpdated.emit(self.viewportTransform())
def toggleDragMode(self):
if self.dragMode() == QtWidgets.QGraphicsView.ScrollHandDrag:
self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
elif not self._photo.pixmap().isNull():
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
def mousePressEvent(self, event):
if self._photo.isUnderMouse():
self.photoClicked.emit(self.mapToScene(event.pos()).toPoint())
super(PhotoViewer, self).mousePressEvent(event)
self.viewUpdated.emit(self.viewportTransform())
def set_transform(self, transform):
self.setTransform(transform)
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.viewer = PhotoViewer(self)
self.viewerSecondImage = PhotoViewer(self)
self.viewer.viewUpdated.connect(self.viewerSecondImage.set_transform)
self.viewerSecondImage.viewUpdated.connect(self.viewer.set_transform)
# 'Load image' button
self.btnLoad = QtWidgets.QToolButton(self)
self.btnLoad.setText('Load image')
self.btnLoad.clicked.connect(self.loadImage)
# Button to change from drag/pan to getting pixel info
self.btnPixInfo = QtWidgets.QToolButton(self)
self.btnPixInfo.setText('Enter pixel info mode')
self.btnPixInfo.clicked.connect(self.pixInfo)
self.editPixInfo = QtWidgets.QLineEdit(self)
self.editPixInfo.setReadOnly(True)
self.viewer.photoClicked.connect(self.photoClicked)
# Arrange layout
VBlayout = QtWidgets.QVBoxLayout(self)
HBlayoutImageViewers = QtWidgets.QHBoxLayout()
HBlayoutImageViewers.addWidget(self.viewer)
HBlayoutImageViewers.addWidget(self.viewerSecondImage)
VBlayout.addLayout(HBlayoutImageViewers)
HBlayout = QtWidgets.QHBoxLayout()
HBlayout.setAlignment(QtCore.Qt.AlignLeft)
HBlayout.addWidget(self.btnLoad)
HBlayout.addWidget(self.btnPixInfo)
HBlayout.addWidget(self.editPixInfo)
VBlayout.addLayout(HBlayout)
def loadImage(self):
self.viewer.setPhoto(QtGui.QPixmap('input/490px-Dostojka_adype.jpg'))
self.viewerSecondImage.setPhoto(QtGui.QPixmap('input/490px-Dostojka_adype.jpg'))
def pixInfo(self):
self.viewer.toggleDragMode()
def photoClicked(self, pos):
if self.viewer.dragMode() == QtWidgets.QGraphicsView.NoDrag:
self.editPixInfo.setText('%d, %d' % (pos.x(), pos.y()))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 800, 600)
window.show()
sys.exit(app.exec_())
You only need to syncronize scrollbar values of two QGraphicsViews
def bindScrollBars(scrollBar1, scrollBar2):
# syncronizing scrollbars syncrnonously somehow breaks zooming and doesn't work
# scrollBar1.valueChanged.connect(lambda value: scrollBar2.setValue(value))
# scrollBar2.valueChanged.connect(lambda value: scrollBar1.setValue(value))
# syncronizing scrollbars asyncronously works ok
scrollBar1.valueChanged.connect(lambda _: QtCore.QTimer.singleShot(0, lambda: scrollBar2.setValue(scrollBar1.value())))
scrollBar2.valueChanged.connect(lambda _: QtCore.QTimer.singleShot(0, lambda: scrollBar1.setValue(scrollBar2.value())))
class Window(QtWidgets.QWidget):
def __init__(self):
...
bindScrollBars(self.viewer.horizontalScrollBar(), self.viewerSecondImage.horizontalScrollBar())
bindScrollBars(self.viewer.verticalScrollBar(), self.viewerSecondImage.verticalScrollBar())
Also wheelEvent can be simplified
def wheelEvent(self, event):
if self.hasPhoto():
factor = 1.25
if event.angleDelta().y() > 0:
self.scale(factor, factor)
else:
self.scale(1/factor, 1/factor)
self.viewUpdated.emit(self.transform())

Set different background color in specific rows in QTableview

Hello, how i can change the background color in specific rows in QTableview?
I would like to know which method to use to make.
I have the method seleckRow but click another the color is not maintained in the selected row.
__author__ = 'jordiponsisala'
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtSql import *
from PyQt5 import QtWidgets
from Llistats import modificarLineal
def crearConeccio():
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("localhost")
db.setDatabaseName("ProvaDB")
db.setUserName("root")
db.setPassword("password")
db.open()
print (db.lastError().text())
return True
class clasArticles(QDialog, modificarLineal.Ui_Dialog):
def __init__(self, parent=None):
super(clasArticles, self).__init__(parent)
self.setupUi(self)
self.setWindowTitle('Posu el nom que vulgui')
self.model= QSqlRelationalTableModel(self)
self.model.setTable('preu')
#self.model.setEditStrategy(QSqlRelationalTableModel.OnFieldChange)
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.setRelation(2,QSqlRelation("probeedors", "id", "nomProbeedor"))
self.model.select()
self.tableView.setModel(self.model)
self.tableView.setItemDelegate(QSqlRelationalDelegate(self.model))
self.tableView.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.tableView.selectRow(1)
self.filtrarLineal.clicked.connect(self.filtrarRecords)
self.cancelarLineal.clicked.connect(self.cancelRecords)
self.afegirLineal.clicked.connect(self.insertRecords)
self.eliminarLineal.clicked.connect(self.deleteRecords)
self.seleccionar.clicked.connect(self.seleccionarCodi)
self.tableView.doubleClicked.connect(self.probes_index)
self.tableView.activated.connect(self.actualitzarRecords)# Pres intro method
def probes_index(self):# metod for print index in row
indexes = self.tableView.selectionModel().selectedRows()
for index in sorted(indexes):
self.lblnomSeleccionat.setText(str(index.row()))
def seleccionarCodi(self):
index = self.tableView.selectedIndexes()[0]
id_seleccionat = str(self.tableView.model().data(index))
print ("index : " + str(id_seleccionat))
self.lblnomSeleccionat.setText(str(id_seleccionat))
def actualitzarRecords(self):
index_0 = self.tableView.selectedIndexes()[0]
index_id = str(self.tableView.model().data(index_0))
index_2 = self.tableView.selectedIndexes()[1]
index_probeedor_id = str(self.tableView.model().data(index_2))
index_3 = self.tableView.selectedIndexes()[3]
index_preuTarifa = str(self.tableView.model().data(index_3))
self.model = QSqlQueryModel(self)
self.model.setQuery("UPDATE `ProvaDB`.`preu` SET `preuTarifa`="+index_preuTarifa+" WHERE `id`="+index_id+" and`probeedors_id`="+index_probeedor_id+"")
def cancelRecords(self):
self.model.revertAll()
def insertRecords(self):
self.model.insertRow(self.tableView.currentIndex().row())
def deleteRecords(self):
self.model.removeRow(self.tableView.currentIndex().row())
self.model.submitAll()
def filtrarRecords(self):
self.model.setFilter("preu.id = '"+self.txtEditFiltre.text()+"%'")
if __name__ == '__main__':
app = QApplication(sys.argv)
if not crearConeccio():
sys.exit(1)
formMod = clasArticles()
formMod.show()
sys.exit(app.exec_())