Different tooltips at each header in QTableView - python

I can add a single tooltip to all headers using
tableview = QTableView()
tableview.horizontalHeader().setToolTip("headers")
but can I add different tooltips to each header, i.e. I need to access the QWidgets that contains the headers, e.g. (not working):
tableview.horizontalHeader().Item[0].setToolTip("header 0")

I'm pretty new to this stuff too, but I think you'll need to subclass QTableView and reimplement the headerData function. Here is a working example. Hopefully you can extract what you need from it:
from PyQt4 import QtGui, QtCore
import sys
class PaletteListModel(QtCore.QAbstractListModel):
def __init__(self, colors = [], parent = None):
QtCore.QAbstractListModel.__init__(self,parent)
self.__colors = colors
# required method for Model class
def rowCount(self, parent):
return len(self.__colors)
# optional method for Model class
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return QtCore.QString("Palette")
else:
return QtCore.QString("Color %1").arg(section)
if role == QtCore.Qt.ToolTipRole:
if orientation == QtCore.Qt.Horizontal:
return QtCore.QString("Horizontal Header %s Tooltip" % str(section))
else:
return QtCore.QString("Vertical Header %s Tooltip" % str(section))
# required method for Model class
def data(self, index, role):
# index contains a QIndexClass object. The object has the following
# methods: row(), column(), parent()
row = index.row()
value = self.__colors[row]
# keep the existing value in the edit box
if role == QtCore.Qt.EditRole:
return self.__colors[row].name()
# add a tooltip
if role == QtCore.Qt.ToolTipRole:
return "Hex code: " + value.name()
if role == QtCore.Qt.DecorationRole:
pixmap = QtGui.QPixmap(26,26)
pixmap.fill(value)
icon = QtGui.QIcon(pixmap)
return icon
if role == QtCore.Qt.DisplayRole:
return value.name()
def setData(self, index, value, role = QtCore.Qt.EditRole):
row = index.row()
if role == QtCore.Qt.EditRole:
color = QtGui.QColor(value)
if color.isValid():
self.__colors[row] = color
self.dataChanged.emit(index, index)
return True
return False
# implment flags() method
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
app.setStyle("plastique")
data = QtCore.QStringList()
data << "one" << "two" << "three" << "four" << "five"
tableView = QtGui.QTableView()
tableView.show()
red = QtGui.QColor(255,0,0)
green = QtGui.QColor(0,255,0)
blue = QtGui.QColor(0,0,255)
model = PaletteListModel([red, green, blue])
tableView.setModel(model)
sys.exit(app.exec_())

Here's what worked for me:
headerView = self._table.horizontalHeader()
for i in range(headerView.count()):
key = headerView.model().headerData(i, QtCore.Qt.Horizontal)
toolTip = myDictOfToolTips.get(key, None)
self._table.horizontalHeaderItem(i).setToolTip(toolTip)

QTableWidget (which inherits QTableView) has a method horizontalHeaderItem(int) which can be used to get the header items, so you maybe could swich to use that instead of QTableView?

If you use QTableView, you can set tooltip by QStandardItemModel:
QStandardItemModel myModel;
myModel.horizontalHeaderItem(1)->setToolTip("");

Related

qtreewidget itemChanged is called twice for renaming

I am making a subclass of the QTreeWidget.
While trying to capture renaming which involves its default signals - itemChanged and itemDoubleClicked, the methods does works but I came to notice that the itemChanged are called twice instead of once.
Could not really figure out where or what is triggered the cause of the second 'extra' signal.
Appreciate if anyone could shed some light.
IsNewItemRole = QtCore.Qt.UserRole + 1000
class CustomTreeDelegate(QtGui.QStyledItemDelegate):
#property
def text_color(self):
if not hasattr(self, "_text_color"):
self._text_color = QtGui.QColor()
return self._text_color
#text_color.setter
def text_color(self, color):
"""Sets QColor towards object.
Args:
color (QtGui.QColor): RGB color values.
"""
self._text_color = color
def initStyleOption(self, option, index):
"""Change the font color only if item is a new added item.
Args:
option ():
index (QModelIndex?)
"""
super(CustomTreeDelegate, self).initStyleOption(option, index)
if self.text_color.isValid() and index.data(IsNewItemRole):
option.palette.setBrush(QtGui.QPalette.Text, self.text_color)
class CustomTreeWidgetItem(QtGui.QTreeWidgetItem):
"""Initialization class for QTreeWidgetItem creation.
Args:
widget (QtGui.QTreeWidget): To append items into.
text (str): Input name for QTreeWidgetItem.
is_tristate (bool): Should it be a tri-state checkbox. False by default.
"""
def __init__(self, parent=None, text=None, is_tristate=False, is_new_item=False):
super(CustomTreeWidgetItem, self).__init__(parent)
self.setText(0, text)
# flags = QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsUserCheckable
if is_tristate:
# flags |= QtCore.Qt.ItemIsTristate
# Solely for the Parent item
self.setFlags(
self.flags()
| QtCore.Qt.ItemIsTristate
| QtCore.Qt.ItemIsEditable
| QtCore.Qt.ItemIsUserCheckable
)
else:
self.setFlags(
self.flags()
| QtCore.Qt.ItemIsEditable
| QtCore.Qt.ItemIsUserCheckable
)
self.setCheckState(0, QtCore.Qt.Unchecked)
self.setData(0, IsNewItemRole, is_new_item)
def setData(self, column, role, value):
"""Override QTreeWidgetItem setData function.
QTreeWidget does not have a signal that defines when an item has been
checked/ unchecked. And so, this method will emits the signal as a
means to handle this.
Args:
column (int): Column value of item.
role (int): Value of Qt.ItemDataRole. It will be Qt.DisplayRole or
Qt.CheckStateRole
value (int or unicode):
"""
state = self.checkState(column)
QtGui.QTreeWidgetItem.setData(self, column, role, value)
if (role == QtCore.Qt.CheckStateRole and
state != self.checkState(column)):
tree_widget = self.treeWidget()
if isinstance(tree_widget, CustomTreeWidget):
tree_widget.itemToggled.emit(self, column)
class CustomTreeWidget(QtGui.QTreeWidget):
"""Initialization class for QTreeWidget creation.
Args:
widget ():
"""
itemToggled = QtCore.pyqtSignal(QtGui.QTreeWidgetItem, bool)
selectionItemChanged = QtCore.pyqtSignal(bool)
contentUpdates = QtCore.pyqtSignal()
def __init__(self, widget=None):
super(CustomTreeWidget, self).__init__(widget)
self.rename_counter = False
self.currentItemChanged.connect(self.selection_item_changed)
self.itemChanged.connect(self.tree_item_changed)
self.itemDoubleClicked.connect(self.tree_item_double_clicked)
def selection_item_changed(self, current, previous):
"""Overrides widget's default signal.
Emiited when current item selection is changed. This will also toggles
the state of `self.add_child_btn`.
If a child item is selected, the "Add Child" button will be disabled.
Args:
current (CustomTreeWidgetItem): Currently selected item.
previous (CustomTreeWidgetItem or None): Previous selected item.
"""
state = True
if not current:
# print '>>> nothing is selected'
state = False
return
if current.parent():
state = False
self.selectionItemChanged.emit(state)
def tree_item_changed(self, item, column):
"""Overrides widget's default signal.
Emitted when the contents of the selected item in the column changes.
Args:
item (CustomTreeWidgetItem): Selected item.
column (int): Column value of the selected item.
"""
print '>>> selection item is changed!'
if self.rename_counter and self.prev_name != item.text(column):
item.setData(0, IsNewItemRole, True)
self.rename_counter = False
self.contentUpdates.emit()
elif item.data(column, IsNewItemRole):
print 'item is already an newitemrole'
return
def tree_item_double_clicked(self, item, column):
"""Overrides widget's default signal.
Emitted when User performs double clicks inside the widget.
Args:
item (CustomTreeWidgetItem): Selected item.
column (int): Column value of the selected item.
"""
self.prev_name = item.text(column)
self.rename_counter = True
class MainApp(QtGui.QWidget):
def __init__(self, parent=None):
super(MainApp, self).__init__(parent)
test_dict = {
"menuA": ["a101", "a102"],
"menuC": ["c101", "c102", "c103"],
"menuB": ["b101"],
}
self._tree = CustomTreeWidget()
self._tree.header().hide()
self._tree_delegate = CustomTreeDelegate(self._tree)
self._tree.setItemDelegate(self._tree_delegate)
for pk, pv in sorted(test_dict.items()):
parent = CustomTreeWidgetItem(self._tree, pk, is_tristate=True)
for c in pv:
child = CustomTreeWidgetItem(parent, c)
self._tree.expandAll()
main_layout = QtGui.QHBoxLayout()
main_layout.addWidget(self._tree)
self.setLayout(main_layout)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainApp()
w.show()
sys.exit(app.exec_())

Derive and set color to the index of newly-added children

I have created subclass of QTreeWidget and QTreeWidgetItem where I am trying to highlight the new added items (where the text will be colored red).
The tree hierarchy that I have adopted is as follows:
|-- parent
|--|-- child
|--|-- child
The tree widget is initially populated from a dictionary.
To get the difference, I did it by converting the current hierarchy in the tree widget into a dictionary and have it compared against the initial dictionary that it was populated with.
However if I add in a new child to an existing parent in which the name of the new child already existed in another parent, the same method as mentioned above does not works, as it will colored the first result that it find.
To replicate:
select menuB
right mouse click > add new sub item
input name: a101
hit "Highlight Diff." button
a101 child item under menuA is highlighted instead of the one in menuB
What would be the best way to go in getting the index of newly added child(ren)?
Thank you for any replies.
P.S: If anyone has better suggestion for the parent highlighting, please feel free to chip in.
class CustomTreeWidgetItem(QtGui.QTreeWidgetItem):
def __init__(self, widget=None, text=None, is_tristate=False):
super(CustomTreeWidgetItem, self).__init__(widget)
self.setText(0, text)
if is_tristate:
# Solely for the Parent item
self.setFlags(
self.flags()
| QtCore.Qt.ItemIsTristate
| QtCore.Qt.ItemIsEditable
| QtCore.Qt.ItemIsUserCheckable
)
else:
self.setFlags(
self.flags()
| QtCore.Qt.ItemIsEditable
| QtCore.Qt.ItemIsUserCheckable
)
self.setCheckState(0, QtCore.Qt.Unchecked)
def setData(self, column, role, value):
state = self.checkState(column)
QtGui.QTreeWidgetItem.setData(self, column, role, value)
if (role == QtCore.Qt.CheckStateRole and
state != self.checkState(column)):
tree_widget = CustomTreeWidget()
if tree_widget is not None:
tree_widget.itemToggled.emit(self, column)
class CustomTreeWidget(QtGui.QTreeWidget):
def __init__(self, widget=None):
super(CustomTreeWidget, self).__init__(widget)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_custom_menu)
def show_custom_menu(self):
base_node = self.selectedItems()[0]
qmenu = QtGui.QMenu(self)
remove_action = QtGui.QAction("Remove item", self)
remove_action.triggered.connect(self.remove_selected_item)
qmenu.addAction(remove_action)
# The following options are only effected for top-level items
# top-level items do not have `parent()`
if not base_node.parent():
add_new_child_action = QtGui.QAction("Add new sub item", self)
add_new_child_action.triggered.connect(
partial(self.adds_new_child_item, base_node)
)
# qmenu.addAction(add_new_child_action)
qmenu.insertAction(remove_action, add_new_child_action)
qmenu.exec_(QtGui.QCursor.pos())
def add_item_dialog(self, title):
text, ok = QtGui.QInputDialog.getText(
self,
"Add {0} Item".format(title),
"Enter name for {0}-Item:".format(title)
)
if ok and text != "":
return text
def add_new_parent_item(self):
input_text = self.add_item_dialog("Parent")
if input_text:
CustomTreeWidgetItem(self, input_text, is_tristate=True)
def adds_new_child_item(self, base_node):
input_text = self.add_item_dialog("Sub")
if input_text:
CustomTreeWidgetItem(base_node, input_text)
self.setItemExpanded(base_node, True)
def remove_selected_item(self):
root = self.invisibleRootItem()
for item in self.selectedItems():
(item.parent() or root).removeChild(item)
def derive_tree_items(self, mode="all"):
all_items = defaultdict(list)
root_item = self.invisibleRootItem()
top_level_count = root_item.childCount()
for i in range(top_level_count):
top_level_item = root_item.child(i)
top_level_item_name = str(top_level_item.text(0))
child_num = top_level_item.childCount()
all_items[top_level_item_name] = []
for n in range(child_num):
child_item = top_level_item.child(n)
child_item_name = str(child_item.text(0)) or ""
if mode == "all":
all_items[top_level_item_name].append(child_item_name)
elif mode == "checked":
if child_item.checkState(0) == QtCore.Qt.Checked:
all_items[top_level_item_name].append(child_item_name)
elif mode == "unchecked":
if child_item.checkState(0) == QtCore.Qt.Unchecked:
all_items[top_level_item_name].append(child_item_name)
return all_items
class MainApp(QtGui.QWidget):
def __init__(self):
super(MainApp, self).__init__()
# initial dictionary that is populated into the tree widget
test_dict = {
"menuA": ["a101", "a102"],
"menuC": ["c101", "c102", "c103"],
"menuB": ["b101"],
}
self._tree = CustomTreeWidget()
self._tree.header().hide()
for pk, pv in sorted(test_dict.items()):
parent = CustomTreeWidgetItem(self._tree, pk, is_tristate=True)
for c in pv:
child = CustomTreeWidgetItem(parent, c)
self.orig_dict = self._tree.derive_tree_items()
# Expand the hierarchy by default
self._tree.expandAll()
tree_layout = QtGui.QVBoxLayout()
self.btn1 = QtGui.QPushButton("Add new item")
self.btn2 = QtGui.QPushButton("Highlight Diff.")
tree_layout.addWidget(self._tree)
tree_layout.addWidget(self.btn1)
tree_layout.addWidget(self.btn2)
main_layout = QtGui.QHBoxLayout()
main_layout.addLayout(tree_layout)
self.setLayout(main_layout)
self.setup_connections()
def setup_connections(self):
self.btn1.clicked.connect(self.add_parent_item)
self.btn2.clicked.connect(self.highlight_diff)
def add_parent_item(self):
# Get current selected in list widget
# CustomTreeWidgetItem(self._tree, "test", is_tristate=True)
self._tree.add_new_parent_item()
def highlight_diff(self):
self.current_dict = self._tree.derive_tree_items()
if self.orig_dict != self.current_dict:
# check for key difference
diff = [k for k in self.current_dict if k not in self.orig_dict]
if diff:
# get the difference of top-level items
for d in diff:
top_item = self._tree.findItems(d, QtCore.Qt.MatchExactly|QtCore.Qt.MatchRecursive)
#print aaa[0].childCount()
top_item[0].setTextColor(0, QtGui.QColor(255, 0, 0))
if top_item[0].childCount():
for n in range(top_item[0].childCount()):
top_item[0].child(n).setTextColor(0, QtGui.QColor(255, 0, 0))
# to highlight the child diff. of existing top-items
# issue with this portion if the new added item name already existed
for k, v in self.current_dict.items():
if k in self.orig_dict:
diff = set(v).difference(self.orig_dict.get(k), [])
for d in diff:
child_item = self._tree.findItems(d, QtCore.Qt.MatchExactly|QtCore.Qt.MatchRecursive)
child_item[0].setTextColor(0, QtGui.QColor(255, 0, 0))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainApp()
w.show()
sys.exit(app.exec_())
You can save in a role a flag indicating if it is a new item or not and change the color using a delegate:
import sys
from functools import partial
from PyQt4 import QtCore, QtGui
from collections import defaultdict
IsNewItemRole = QtCore.Qt.UserRole + 1000
class CustomTreeDelegate(QtGui.QStyledItemDelegate):
#property
def text_color(self):
if not hasattr(self, "_text_color"):
self._text_color = QtGui.QColor()
return self._text_color
#text_color.setter
def text_color(self, color):
self._text_color = color
def initStyleOption(self, option, index):
super(CustomTreeDelegate, self).initStyleOption(option, index)
if self.text_color.isValid() and index.data(IsNewItemRole):
option.palette.setBrush(QtGui.QPalette.Text, self.text_color)
class CustomTreeWidgetItem(QtGui.QTreeWidgetItem):
def __init__(self, parent=None, text="", is_tristate=False, is_new_item=False):
super(CustomTreeWidgetItem, self).__init__(parent)
self.setText(0, text)
flags = QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsUserCheckable
if is_tristate:
flags |= QtCore.Qt.ItemIsTristate
else:
self.setCheckState(0, QtCore.Qt.Unchecked)
self.setFlags(self.flags() | flags)
self.setData(0, IsNewItemRole, is_new_item)
def setData(self, column, role, value):
state = self.checkState(column)
QtGui.QTreeWidgetItem.setData(self, column, role, value)
if role == QtCore.Qt.CheckStateRole and state != self.checkState(column):
tree_widget = self.treeWidget()
if isinstance(tree_widget, CustomTreeWidget):
tree_widget.itemToggled.emit(self, column)
class CustomTreeWidget(QtGui.QTreeWidget):
itemToggled = QtCore.pyqtSignal(QtGui.QTreeWidgetItem, int)
def __init__(self, widget=None):
super(CustomTreeWidget, self).__init__(widget)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_custom_menu)
def show_custom_menu(self, pos):
base_node = self.itemAt(pos)
if base_node is None:
return
qmenu = QtGui.QMenu(self)
remove_action = QtGui.QAction("Remove item", self)
remove_action.triggered.connect(self.remove_selected_item)
qmenu.addAction(remove_action)
# The following options are only effected for top-level items
# top-level items do not have `parent()`
if base_node.parent() is None:
add_new_child_action = QtGui.QAction("Add new sub item", self)
add_new_child_action.triggered.connect(
partial(self.adds_new_child_item, base_node)
)
# qmenu.addAction(add_new_child_action)
qmenu.insertAction(remove_action, add_new_child_action)
qmenu.exec_(self.mapToGlobal(pos))
def add_item_dialog(self, title):
text, ok = QtGui.QInputDialog.getText(
self, "Add {0} Item".format(title), "Enter name for {0}-Item:".format(title)
)
if ok:
return text
def add_new_parent_item(self):
input_text = self.add_item_dialog("Parent")
if input_text:
it = CustomTreeWidgetItem(
self, input_text, is_tristate=True, is_new_item=True
)
def adds_new_child_item(self, base_node):
input_text = self.add_item_dialog("Sub")
if input_text:
it = CustomTreeWidgetItem(base_node, input_text, is_new_item=True)
self.setItemExpanded(base_node, True)
it.setData(0, IsNewItemRole, True)
def remove_selected_item(self):
root = self.invisibleRootItem()
for item in self.selectedItems():
(item.parent() or root).removeChild(item)
class MainApp(QtGui.QWidget):
def __init__(self, parent=None):
super(MainApp, self).__init__(parent)
# initial dictionary that is populated into the tree widget
test_dict = {
"menuA": ["a101", "a102"],
"menuC": ["c101", "c102", "c103"],
"menuB": ["b101"],
}
self._tree = CustomTreeWidget()
self._tree.header().hide()
self._tree_delegate = CustomTreeDelegate(self._tree)
self._tree.setItemDelegate(self._tree_delegate)
for pk, pv in sorted(test_dict.items()):
parent = CustomTreeWidgetItem(self._tree, pk, is_tristate=True)
for c in pv:
child = CustomTreeWidgetItem(parent, c)
# Expand the hierarchy by default
self._tree.expandAll()
tree_layout = QtGui.QVBoxLayout()
self.btn1 = QtGui.QPushButton("Add new item")
self.btn2 = QtGui.QPushButton("Highlight Diff.")
tree_layout.addWidget(self._tree)
tree_layout.addWidget(self.btn1)
tree_layout.addWidget(self.btn2)
main_layout = QtGui.QHBoxLayout(self)
main_layout.addLayout(tree_layout)
self.setup_connections()
def setup_connections(self):
self.btn1.clicked.connect(self.add_parent_item)
self.btn2.clicked.connect(self.highlight_diff)
def add_parent_item(self):
# Get current selected in list widget
# CustomTreeWidgetItem(self._tree, "test", is_tristate=True)
self._tree.add_new_parent_item()
def highlight_diff(self):
self._tree_delegate.text_color = QtGui.QColor(255, 0, 0)
self._tree.viewport().update()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainApp()
w.show()
sys.exit(app.exec_())

Is it possible to create MapQuickItems from qml in python?

I'm working on a PyQt5 and Qml application. The application displays 2 QLineEdits, 2 QPushButton and a map that is given from my Qml. One of the functionalities of the application is parsing(in python) through an XML-List with titles and coordinates and adding pins on the coordinates on the map. My problem is now that it only displays the last coordinate. I realized that it's because I only change the property of the pin Item in Qml and not create a new one. My Question is now: Is it possible to create MapQuickItems in python?
As I commented #derM you only have to create a model and expose it to your QML, there is no documentation for PyQt, but we can take as reference to the C ++ documentation since the concepts are similar. As a sample of this I have created in the following model that contains the information of a marker, in this case consider the position and color of the marker.
class MarkerItem(object):
def __init__(self, position, color=QColor("red")):
self._position = position
self._color = color
def position(self):
return self._position
def setPosition(self, value):
self._position = value
def color(self):
return self._color
def setColor(self, value):
self._color = value
class MarkerModel(QAbstractListModel):
PositionRole = Qt.UserRole + 1
ColorRole = Qt.UserRole + 2
_roles = {PositionRole: QByteArray(b"markerPosition"), ColorRole: QByteArray(b"markerColor")}
def __init__(self, parent=None):
QAbstractListModel.__init__(self, parent)
self._markers = []
def rowCount(self, index=QModelIndex()):
return len(self._markers)
def roleNames(self):
return self._roles
def data(self, index, role=Qt.DisplayRole):
if index.row() >= self.rowCount():
return QVariant()
marker = self._markers[index.row()]
if role == MarkerModel.PositionRole:
return marker.position()
elif role == MarkerModel.ColorRole:
return marker.color()
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
marker = self._markers[index.row()]
if role == MarkerModel.PositionRole:
marker.setPosition(value)
if role == MarkerModel.ColorRole:
marker.setColor(value)
self.dataChanged.emit(index, index)
return True
return QAbstractListModel.setData(self, index, value, role)
def addMarker(self, marker):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
self._markers.append(marker)
self.endInsertRows()
def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
return QAbstractListModel.flags(index)|Qt.ItemIsEditable
def moveRandom(self, ix):
ind = self.index(ix, 0)
current_pos = self.data(ind, MarkerModel.PositionRole)
next_pos = current_pos + 0.002*QPointF(random() - 0.5, random() - 0.5)
self.setData(ind, next_pos, MarkerModel.PositionRole)
self.setData(ind, QColor(randint(0, 255), randint(0, 255), randint(0, 255)), MarkerModel.ColorRole)
And then used in the following example:
main.py
if __name__ == "__main__":
import sys
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
model = MarkerModel()
model.addMarker(MarkerItem(QPointF(-12.0464, -77.0428), QColor("black")))
model.addMarker(MarkerItem(QPointF(-12.0340, -77.0428), QColor("red")))
model.addMarker(MarkerItem(QPointF(-12.0440, -77.0280), QColor("#9f5522")))
context = engine.rootContext()
context.setContextProperty('markerModel', model)
engine.load(QUrl.fromLocalFile("main.qml"))
if len(engine.rootObjects()) == 0:
sys.exit(-1)
engine.quit.connect(app.quit)
timer = QTimer(engine)
timer.timeout.connect(lambda: model.moveRandom(0))
timer.start(100)
sys.exit(app.exec_())
main.qml
import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.5
import QtPositioning 5.5
Window {
visible: true
title: "Python OSM"
width: 640
height: 480
property int marker_size: 16
Map {
id: map
anchors.fill: parent
plugin: Plugin {
name: "osm"
}
center: QtPositioning.coordinate(-12.0464, -77.0428)
zoomLevel: 14
MapItemView {
model: markerModel
delegate: MapQuickItem{
anchorPoint: Qt.point(2.5, 2.5)
coordinate: QtPositioning.coordinate(markerPosition.x, markerPosition.y)
zoomLevel: 0
sourceItem: Rectangle{
width: marker_size
height: marker_size
radius: marker_size/2
border.color: "white"
color: markerColor
border.width: 1
}
}
}
}
}
Obtaining what is shown in the following image:
Note: I added the moveRandom method to show you how to update the position of the markers, as you will see when executing the example a marker randomly moves
The complete example can be found in the following link

How can I get right-click context menus for clicks in QTableView header?

The sample code below (heavily influenced from here) has a right-click context menu that will appear as the user clicks the cells in the table. Is it possible to have a different right-click context menu for right-clicks in the header of the table? If so, how can I change the code to incorporate this?
import re
import operator
import os
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.tabledata = [('apple', 'red', 'small'),
('apple', 'red', 'medium'),
('apple', 'green', 'small'),
('banana', 'yellow', 'large')]
self.header = ['fruit', 'color', 'size']
# create table
self.createTable()
# layout
layout = QVBoxLayout()
layout.addWidget(self.tv)
self.setLayout(layout)
def popup(self, pos):
for i in self.tv.selectionModel().selection().indexes():
print i.row(), i.column()
menu = QMenu()
quitAction = menu.addAction("Quit")
action = menu.exec_(self.mapToGlobal(pos))
if action == quitAction:
qApp.quit()
def createTable(self):
# create the view
self.tv = QTableView()
self.tv.setStyleSheet("gridline-color: rgb(191, 191, 191)")
self.tv.setContextMenuPolicy(Qt.CustomContextMenu)
self.tv.customContextMenuRequested.connect(self.popup)
# set the table model
tm = MyTableModel(self.tabledata, self.header, self)
self.tv.setModel(tm)
# set the minimum size
self.tv.setMinimumSize(400, 300)
# hide grid
self.tv.setShowGrid(True)
# set the font
font = QFont("Calibri (Body)", 12)
self.tv.setFont(font)
# hide vertical header
vh = self.tv.verticalHeader()
vh.setVisible(False)
# set horizontal header properties
hh = self.tv.horizontalHeader()
hh.setStretchLastSection(True)
# set column width to fit contents
self.tv.resizeColumnsToContents()
# set row height
nrows = len(self.tabledata)
for row in xrange(nrows):
self.tv.setRowHeight(row, 18)
# enable sorting
self.tv.setSortingEnabled(True)
return self.tv
class MyTableModel(QAbstractTableModel):
def __init__(self, datain, headerdata, parent=None, *args):
""" datain: a list of lists
headerdata: a list of strings
"""
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata = datain
self.headerdata = headerdata
def rowCount(self, parent):
return len(self.arraydata)
def columnCount(self, parent):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return QVariant(self.headerdata[col])
return QVariant()
def sort(self, Ncol, order):
"""Sort table by given column number.
"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
if order == Qt.DescendingOrder:
self.arraydata.reverse()
self.emit(SIGNAL("layoutChanged()"))
if __name__ == "__main__":
main()
Turned out to be simpler than I thought. In the same manner as I add the popup menu for the QTableView widget itself, I can just get the header from table object and then attach a context menu in the same way as I did with the regular context menu.
headers = self.tv.horizontalHeader()
headers.setContextMenuPolicy(Qt.CustomContextMenu)
headers.customContextMenuRequested.connect(self.header_popup)
There's another potentially more powerful way to do this if you take the step and inherit the view instead of simply composing it. Does custom context menu work here? Yes, but why does anything other than the view need to know about it? It also will help better shape your code to deal with other issues properly. Currently the implementation doesn't provide any encapsulation, cohesion or support separation of responsibility. In the end you will have one big blob which is the antithesis of good design.
I mention this because you seem to be placing all of the GUI Logic in this ever growing main function, and its the reason you ended up putting the sort implementation inside your model, which makes no sense to me. (What if you have two views of the model, you are forcing them to be sorted in the same way)
Is it more code? Yes, but it gives you more power which I think is worth mentioning. Below I'm demonstrating how to handle the headers and also any given cell you want. Also note that in my implementation if some OTHER widget exists which also defines a context menu event handler it will potentially get a chance to have crack at handling the event after mine; so that if someone else adds a handler for only certain cases they can do so without complicating my code. Part of doing this is marking if you handled the event or not.
Enough of my rambling and thoughts here's the code:
#Alteration : instead of self.tv = QTableView...
self.tv = MyTableView()
....
# somewhere in your TableView object's __init__ method
# yeah IMHO you should be inheriting and thus extending TableView
class MyTableView(QTableView):
def __init__(self, parent = None):
super(MyTableView, self).__init__()
self.setContextMenuPolicy(Qt.DefaultContextMenu)
## uniform one for the horizontal headers.
self.horizontalHeader().setContextMenuPolicy(Qt.ActionsContextMenu)
''' Build a header action list once instead of every time they click it'''
doSomething = QAction("&DoSomething", self.verticalHeader(),
statusTip = "Do something uniformly for headerss",
triggered = SOME_FUNCTION
self.verticalHeader().addAction(doSomething)
...
return
def contextMenuEvent(self, event)
''' The super function that can handle each cell as you want it'''
handled = False
index = self.indexAt(event.pos())
menu = QMenu()
#an action for everyone
every = QAction("I'm for everyone", menu, triggered = FOO)
if index.column() == N: #treat the Nth column special row...
action_1 = QAction("Something Awesome", menu,
triggered = SOME_FUNCTION_TO_CALL )
action_2 = QAction("Something Else Awesome", menu,
triggered = SOME_OTHER_FUNCTION )
menu.addActions([action_1, action_2])
handled = True
pass
elif index.column() == SOME_OTHER_SPECIAL_COLUMN:
action_1 = QAction("Uh Oh", menu, triggered = YET_ANOTHER_FUNCTION)
menu.addActions([action_1])
handled = True
pass
if handled:
menu.addAction(every)
menu.exec_(event.globalPos())
event.accept() #TELL QT IVE HANDLED THIS THING
pass
else:
event.ignore() #GIVE SOMEONE ELSE A CHANCE TO HANDLE IT
pass
return
pass #end of class

PyQt Paint Custom Date Format

I'm displaying some information from a sql server in a qtableview using a sqlmodel.
I have set up a custom delegate to deal with the editing of the data.
I would like to display my dates in a specific format, when they table is first loaded the dates are displayed as such:
20011-04-30
But when I edit the date and click off the cell to accept the date is then displayed like:
30/04/2011
Which is how its stored in the database and how I would like it to be displayed to start with, I have no idea why it changes format once its been edited.
I'm guessing I have to reimplement the paint method for that column I have done something similar with a progress bar but I have no idea how to do it for text.
Here is my delegate as it stands, note is has some editors set up for other columns but my main question only relates to how to display the date correctly.
import sys
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
PROJECTID, PROJECTTITLE, CLIENTID, DEADLINE, PROGRESS, ROOT = range(6)
class projectsDelegate(QSqlRelationalDelegate):
def __ini__(self, parent = None):
super(projectsDelegate, self).__init__(parent)
def createEditor(self, parent, option, index):
if index.column() == DEADLINE:
editor = QDateEdit(parent)
#editor.setDisplayFormat("yyyy/MM/dd")
editor.setMinimumDate(QDate.currentDate())
editor.setCalendarPopup(True)
return editor
elif index.column() == PROGRESS:
editor = QSpinBox(parent)
editor.setRange(0, 100)
editor.setSingleStep(5)
editor.setSuffix("%")
return editor
elif index.column() == ROOT:
editor = QFileDialog(parent)
editor.setFileMode(QFileDialog.Directory)
editor.setOptions(QFileDialog.ShowDirsOnly)
editor.setFixedSize(400, 400)
editor.setWindowTitle("Select A Root Folder For The Project")
return editor
else:
return QSqlRelationalDelegate.createEditor(self, parent, option, index)
def setEditorData(self, editor, index):
if index.column() == DEADLINE:
text = index.model().data(index, Qt.DisplayRole).toDate()
editor.setDate(text)
elif index.column() == PROGRESS:
prog = index.model().data(index, Qt.DisplayRole).toInt()[0]
editor.setValue(prog)
elif index.column() == ROOT:
root = index.model().data(index, Qt.DisplayRole).toString()
editor.setDirectory(os.path.dirname(str(root)))
screen = QDesktopWidget().screenGeometry()
size = editor.geometry()
editor.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
else:
QSqlRelationalDelegate.setEditorData(self, editor, index)
def setModelData(self, editor, model, index):
if index.column() == DEADLINE:
model.setData(index, QVariant(editor.date()))
elif index.column() == PROGRESS:
model.setData(index, QVariant(editor.value()))
elif index.column() == ROOT:
model.setData(index, QVariant(editor.directory().absolutePath()))
else:
QSqlRelationalDelegate.setModelData(self, editor, model, index)
def paint(self, painter, option, index):
if index.column() == PROGRESS:
bar = QStyleOptionProgressBarV2()
bar.rect = option.rect
bar.minimum = 0
bar.maximum = 100
bar.textVisible = True
percent = index.model().data(index, Qt.DisplayRole).toInt()[0]
bar.progress = percent
bar.text = QString("%d%%" % percent)
QApplication.style().drawControl(QStyle.CE_ProgressBar, bar, painter)
else:
QSqlRelationalDelegate.paint(self, painter, option, index)
def sizeHint(self, options, index):
if index.column() == PROGRESS:
return QSize(150, 30)
elif index.column() == ROOT:
return QSize(400, 800)
else:
return QSqlRelationalDelegate.sizeHint(self, options, index)
Thanks, Tom.
Why is editor.setDisplayFormat("yyyy/MM/dd") commented out? Shouldn't that take care of the formatting?

Categories

Resources