Drag and Drop not working in `QFileModelSystem` - pyqt5

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_()

Related

How to open context menu for an object that is empty, but has a size?

I am creating a GUI for a dependency graphing software... And am not able to figure out how to get a context menu to open for my lines.
What I want to do, right click on/near a MyLine widget and open a context menu... What is happening right clicks are not detected.
It is currently not detecting right clicks on the line widgets location to open a context menu (Purpose of this is to allow the user to delete/edit lines by right clicking on them).
What am I doing wrong here?
class MyLine(QWidget):
def __init__(self, destination: Node, source: Node, parent=None):
super().__init__(parent)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showMenu)
self.destination = destination
self.source = source
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.red)
self.setPalette(p)
def update_line_size(self):
origin = self.source.get_line_draw_pos(self.destination.pos())
destination = self.destination.get_line_draw_pos(self.source.pos())
leftcornerX = origin.x() if origin.x() < destination.x() else destination.x()
leftcornerY = origin.y() if origin.y() < destination.y() else destination.y()
sizeX = abs(origin.x() - destination.x())
sizeY = abs(origin.y() - destination.y())
self.setGeometry(leftcornerX, leftcornerY, sizeX, sizeY)
def showMenu(self, _):
menu = QMenu()
menu.addAction("Delete", self.remove)
menu.exec_(self.cursor().pos())
def draw(self, painter: QPainter):
origin = self.source.get_line_draw_pos(self.destination.pos())
destination = self.destination.get_line_draw_pos(self.source.pos())
painter.drawLine(origin, destination)
# DRAW ARROW HEAD
ARROW_SIZE = 10 # Might change
line_angle = calculate_line_angle(destination, origin)
draw_arrow_head(destination, painter, line_angle, ARROW_SIZE)
def remove(self):
self.parent().delete_line(self)
self.deleteLater()
Edit:
required types for reproducibility
class Node(QLabel):
def __init__(self, text: str, parent=None):
super().__init__(text, parent)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showMenu)
def get_line_draw_pos(self, other_side: QPoint):
x = self.pos().x() if other_side.x() < self.pos().x() else (self.pos().x() + self.width())
y = self.pos().y() if other_side.y() < self.pos().y() else (self.pos().y() + self.height())
return QPoint(x, y)
def showMenu(self, _):
pass #purposefully left as a stub
def calculate_line_angle(destination: QPoint, origin: QPoint):
return math.atan2(destination.y() - origin.y(), destination.x() - origin.x())
def draw_arrow_head(destination: QPoint, painter: QPainter, line_angle: float, arrow_size: float = 10):
angle1 = math.radians(22.5) + line_angle
angle2 = math.radians(-22.5) + line_angle
arrow1 = QPoint( int(destination.x() - arrow_size * math.cos(angle1)), int(destination.y() - arrow_size * math.sin(angle1)))
arrow2 = QPoint( int(destination.x() - arrow_size * math.cos(angle2)), int(destination.y() - arrow_size * math.sin(angle2)))
painter.drawLine(destination, arrow1)
painter.drawLine(destination, arrow2)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setAcceptDrops(True) # add a drop rule
self.setMouseTracking(True)
self.track_origin = None
self.track_mouse = QPoint(0,0)
self.lines = []
def paintEvent(self, event):
painter = QPainter(self)
for line in self.lines:
line.draw(painter)
line.update_line_size()
def connectNodes(self, destination: Node, source: Node):
self.lines.append(MyLine(destination, source))
self.update()
def delete_line(self, line: MyLine):
self.lines.remove(line)
self.update()
app = QApplication([])
window = MainWindow()
window.setWindowTitle("Right Click to remove label")
window.setGeometry(100, 100, 400, 200)
window.move(60,15)
nodes = []
for index, node_name in enumerate(["hello.txt", "not_a_villain.txt", "nope.txt"]):
node = Node(node_name, window)
node.move(50 + index*100, 50 + (index%2) * 50)
nodes.append(node)
window.connectNodes(nodes[0], nodes[1])
window.connectNodes(nodes[0], nodes[2])
window.connectNodes(nodes[1], nodes[2])
window.show()
sys.exit(app.exec_())

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()

PyQt5 - How to calculate corner points of a QGraphicsRectItem after rotation by its center point?

My problem is that I couldn't find the pixel values of each corner points of a HighwayItem (which is a QGraphicsRectItem) after rotation it by angle theta about the center point of it.
I used the Rotation Matrix which explained here and I also looked thisexplanation. But, I cannot find the true values.
Any help will be great. Thanks.
Here is MapViewer() class. A HighwayItem is created in this view.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QPoint, QPointF, QRectF
from PyQt5.QtWidgets import QGraphicsScene, \
QGraphicsView, QGraphicsPixmapItem, \
from class_graphical_items import HighwayItem
class MapViewer(QGraphicsView):
def __init__(self, parent, ui):
super(MapViewer, self).__init__(parent)
self.ui = ui
# Attributes for highway
self.add_highway_control = False
self.current_highway = None
self.start = QPointF()
self.hw_counter = 0
self._scene = QGraphicsScene(self)
self._map = QGraphicsPixmapItem()
self._scene.addItem(self._map)
self.setScene(self._scene)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setFrameShape(QtWidgets.QFrame.NoFrame)
def mousePressEvent(self, event):
if self._map.isUnderMouse():
if self.add_highway_control:
# Create a yellow highway
self.current_highway = HighwayItem(self._scene, self.ui)
self.hw_counter += 1
self.start = self.mapToScene(event.pos()).toPoint()
r = QRectF(self.start, self.start)
self.current_highway.setRect(r)
self._scene.addItem(self.current_highway)
# When adding HW, set drag mode NoDrag
self.setDragMode(QGraphicsView.NoDrag)
super(MapViewer, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.add_highway_control and self.current_highway is not None:
# When adding HW, set drag mode NoDrag
self.setDragMode(QGraphicsView.NoDrag)
r = QRectF(self.start, self.mapToScene(event.pos()).toPoint()).normalized()
self.current_highway.setRect(r)
super(MapViewer, self).mouseReleaseEvent(event)
def mouseReleaseEvent(self, event):
if self.add_highway_control:
if self.current_highway is not None:
# When finish the adding HW, set drag mode ScrollHandDrag
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.update_item_dict(self.current_highway)
self.update_item_table(self.current_highway)
self.current_highway = None
self.add_highway_control = False
super(MapViewer, self).mouseReleaseEvent(event)
This is the HighwayItem class. It has some specs like color, opacity etc.
By doubleclicking on created HighwayItem, I'm activating a spinbox which was in a QTreeWidget in main window (ui).
By changing the spinbox value, the user can rotate the item.
class HighwayItem(QGraphicsRectItem):
def __init__(self, scene, ui):
QGraphicsRectItem.__init__(self)
self.scene = scene
self.ui = ui
self.setBrush(QtCore.Qt.yellow)
self.setOpacity(0.5)
self.setZValue(4.0)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
self.setFlag(QGraphicsItem.ItemIsFocusable, True)
self.setAcceptHoverEvents(True)
# Here, I'm activating the spinbox by double clicking
# on HighwayItem. In spinbox, I'm entering the rotation angle
# of HighwayItem.
def mouseDoubleClickEvent(self, event):
selected_item = self.scene.selectedItems()
if selected_item:
for i in range(self.ui.treeWidget_objects.topLevelItemCount()):
toplevel_item = self.ui.treeWidget_objects.topLevelItem(i)
heading_item = toplevel_item.child(2)
spinbox = self.ui.treeWidget_objects.itemWidget(heading_item, 2)
if str(toplevel_item.text(2)) == str(selected_item[0]):
if 'HighwayItem' in str(selected_item[0]):
spinbox.setEnabled(True)
else:
spinbox.setEnabled(False)
This is the HWHeadingSpinBox() class which sets the rotation angle of HWItem. My problem starts here. In rotate_hw() method, I am transforming the created HighwayItem by its center point and giving it a rotation by its center point.
BUT, when I try to calculate new corners of hw in calc_rotated_coords() method, I'm messing up.
class HWHeadingSpinBox(QSpinBox):
def __init__(self, viewer, selected_hw):
QSpinBox.__init__(self)
self.selected_hw = selected_hw
self.viewer = viewer
# First coords of HW
tl = self.selected_hw.rect().topLeft()
tr = self.selected_hw.rect().topRight()
br = self.selected_hw.rect().bottomRight()
bl = self.selected_hw.rect().bottomLeft()
self.temp_list = [tl, tr, br, bl]
self.setRange(-180, 180)
self.setSuffix('°')
self.setEnabled(False)
self.valueChanged.connect(self.rotate_hw)
def heading_val(self):
return self.value()
def rotate_hw(self):
angle = self.heading_val()
self.selected_hw.prepareGeometryChange()
offset = self.selected_hw.boundingRect().center()
self.selected_hw.sceneBoundingRect().center()
transform = QTransform()
transform.translate(offset.x(), offset.y())
transform.rotate(-angle)
transform.translate(-offset.x(), -offset.y())
self.selected_hw.setTransform(transform)
# br_rect = self.selected_hw.sceneBoundingRect()
# sbr_rect = self.selected_hw.sceneBoundingRect()
# r_rect = self.selected_hw.sceneBoundingRect()
#
# rectitem = QtWidgets.QGraphicsRectItem(br_rect)
# rectitem.setBrush(Qt.red)
# self.viewer._scene.addItem(rectitem)
#
# rectitem = QtWidgets.QGraphicsRectItem(sbr_rect)
# rectitem.setBrush(Qt.green)
# self.viewer._scene.addItem(rectitem)
#
# rectitem = QtWidgets.QGraphicsRectItem(r_rect)
# rectitem.setBrush(Qt.blue)
# self.viewer._scene.addItem(rectitem)
def calc_rotated_coords(self):
# center point
cx = self.selected_hw.rect().center().x()
cy = self.selected_hw.rect().center().y()
# rotation angle
theta = math.radians(angle)
rotated_corners = []
for item in self.temp_list:
x = item.x()
y = item.y()
temp_x = x - cx
temp_y = y - cy
rot_x = temp_x * math.cos(theta) + temp_y * math.sin(theta)
rot_y = -temp_x * math.sin(theta) + temp_y * math.cos(theta)
rotated_corners.append([rot_x, rot_y])
self.temp_list = rotated_corners
print("\nPIXEL VALUES OF HW: \n{}".format(self.temp_list))
Here is the solution:
I added the itemChange(self, change, value) event in to HighwayItem and if change is ItemPositionHasChanged, I calculated all items' corners as such:
def itemChange(self, change, value):
if change == QGraphicsItem.ItemPositionHasChanged:
top_left = self.mapToScene(self.rect().topLeft())
top_right = self.mapToScene(self.rect().topRight())
bottom_left = self.mapToScene(self.rect().bottomLeft())
bottom_right = self.mapToScene(self.rect().bottomRight())
changed_pos = [top_left, top_right, bottom_right, bottom_left]
return super(HighwayItem, self).itemChange(change, value)

PyQt5: detect a new inserted tab with QTabWidget::tabInserted(int index)

I'm new in PyQt5. I made a tabWidget where I was able to connect a button to add tabs dynamically and remove/close them. Before inserting tabs with button.Clicked, there's one open (already inserted) tab saying "no tabs are open". I would like to close this tab after inserting a new tab and reopen the tab after closing all new tabs (when no tabs are open). I couldn't find any example on how to use QTabWidget::tabInserted(int index) from http://doc.qt.io/qt-5/qtabwidget.html. Does anyone know how to use tabInserted and tabRemoved to detect when triggered. I would like to increment each time a tab is inserted and decrement when removed, to know when there are open tabs and no open tabs. Thanks
self.toolButton.clicked.connect(self.button_addtab)
self.tabWidget.tabCloseRequested.connect(self.close_tab)
def button_addtab(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.tabWidget.setCurrentIndex(pages-0)
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Second_tab"))
def close_tab(self, index):
self.tabWidget.removeTab(index)
I tried this:
if self.tabWidget.count() <= 0:
#Add the "no tab open" tab
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.tabWidget.addTab(self.tab_3, "")
_translate = QtCore.QCoreApplication.translate
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Info"))
elif self.tabWidget.count() > 0:
self.tabWidget.removeTab(self.tabWidget.indexOf(self.tab_3))
This work, but it doesn't add after closing and it reopens after one more tab is added. That's why I would like to use tabInserted
See class TabWidget
import sys
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QColor, QPainter
from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow, QAction,
QVBoxLayout, QTabWidget, QFileDialog, QPlainTextEdit, QHBoxLayout)
lineBarColor = QColor(53, 53, 53)
lineHighlightColor = QColor('#00FF04')
class TabWidget(QTabWidget):
def __init__(self, parent=None):
super(TabWidget, self).__init__(parent)
# This virtual handler is called after a tab was removed from position index.
def tabRemoved(self, index):
print("\n tab was removed from position index -> {}".format(index))
# This virtual handler is called after a new tab was added or inserted at position index.
def tabInserted(self, index):
print("\n New tab was added or inserted at position index -> {}".format(index))
class NumberBar(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.editor = parent
layout = QVBoxLayout(self)
self.editor.blockCountChanged.connect(self.update_width)
self.editor.updateRequest.connect(self.update_on_scroll)
self.update_width('001')
def mousePressEvent(self, QMouseEvent):
print("\n - class NumberBar(QWidget): \n\tdef mousePressEvent(self, QMouseEvent):")
def update_on_scroll(self, rect, scroll):
if self.isVisible():
if scroll:
self.scroll(0, scroll)
else:
self.update()
def update_width(self, string):
width = self.fontMetrics().width(str(string)) + 10
if self.width() != width:
self.setFixedWidth(width)
def paintEvent(self, event):
if self.isVisible():
block = self.editor.firstVisibleBlock()
height = self.fontMetrics().height()
number = block.blockNumber()
painter = QPainter(self)
painter.fillRect(event.rect(), lineBarColor)
painter.setPen(Qt.white)
painter.drawRect(0, 0, event.rect().width() - 1, event.rect().height() - 1)
font = painter.font()
current_block = self.editor.textCursor().block().blockNumber() + 1
while block.isValid():
block_geometry = self.editor.blockBoundingGeometry(block)
offset = self.editor.contentOffset()
block_top = block_geometry.translated(offset).top()
number += 1
rect = QRect(0, block_top, self.width() - 5, height)
if number == current_block:
font.setBold(True)
else:
font.setBold(False)
painter.setFont(font)
painter.drawText(rect, Qt.AlignRight, '%i' % number)
if block_top > event.rect().bottom():
break
block = block.next()
painter.end()
class Content(QWidget):
def __init__(self, text):
super(Content, self).__init__()
self.editor = QPlainTextEdit()
self.editor.setPlainText(text)
# Create a layout for the line numbers
self.hbox = QHBoxLayout(self)
self.numbers = NumberBar(self.editor)
self.hbox.addWidget(self.numbers)
self.hbox.addWidget(self.editor)
class MyTableWidget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = TabWidget() #QTabWidget()
self.tabs.resize(300, 200)
# Add tabs
self.tabs.setTabsClosable(True)
self.tabs.tabCloseRequested.connect(self.closeTab)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
def closeTab(self, index):
tab = self.tabs.widget(index)
tab.deleteLater()
self.tabs.removeTab(index)
def addtab(self, content, fileName):
self.tabs.addTab(Content(str(content)), str(fileName))
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.open()
self.tabs = MyTableWidget()
self.setCentralWidget(self.tabs)
self.initUI()
self.show()
def initUI(self):
self.statusBar()
menu = self.menuBar()
fileMenu = menu.addMenu('File')
fileMenu.addAction(self.openAct)
self.resize(800, 600)
def closeTab(self, index):
tab = self.tabs.widget(index)
tab.deleteLater()
self.tabs.removeTab(index)
def buttonClicked(self):
self.tabs.addTab(Content("smalltext2"), "sadsad")
def open(self):
self.openAct = QAction('Open...', self)
self.openAct.setShortcut('Ctrl+O')
self.openAct.setStatusTip('Open a file')
self.is_opened = False
self.openAct.triggered.connect(self.openFile)
def openFile(self):
options = QFileDialog.Options()
filenames, _ = QFileDialog.getOpenFileNames(
self, 'Open a file', '',
'Python Files (*.py);;Text Files (*.txt)',
options=options
)
if filenames:
for filename in filenames:
with open(filename, 'r+') as file_o:
try:
text = file_o.read()
self.tabs.addtab(text, filename)
except Exception as e:
print("Error: filename=`{}`, `{}` ".format( filename, str(e)))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())

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_())