Created
May 31, 2018 08:22
-
-
Save mortbauer/86486a8f37164bb199388141dbdbeaa4 to your computer and use it in GitHub Desktop.
Qt MVC concept example in a Jupyter Notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Example of a simple QTableView and QAbstractTableModel \n", | |
"\n", | |
"awesome explained at http://doc.qt.io/qt-5/modelview.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import sys\n", | |
"import logging\n", | |
"\n", | |
"logger = logging.getLogger(__name__)\n", | |
"handler = logging.StreamHandler(sys.stdout)\n", | |
"formatter = logging.Formatter('%(asctime)s - %(name)s - %(funcName)s - %(levelname)s - %(message)s')\n", | |
"handler.setFormatter(formatter)\n", | |
"logger.addHandler(handler)\n", | |
"logger.setLevel(logging.DEBUG)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2018-05-31 09:58:14,576 - __main__ - <module> - INFO - Test\n" | |
] | |
} | |
], | |
"source": [ | |
"logger.info('Test')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%gui qt5" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from PyQt5.QtGui import QBrush\n", | |
"from PyQt5.QtWidgets import QTableView\n", | |
"from PyQt5.QtCore import QAbstractTableModel, Qt, QModelIndex" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class MyModel(QAbstractTableModel):\n", | |
" def __init__(self,data,parent=None):\n", | |
" super(MyModel,self).__init__(parent)\n", | |
" self._data = data\n", | |
" self._data_handlers = {\n", | |
" Qt.DisplayRole:self._handle_data_display_role,\n", | |
" Qt.BackgroundRole:self._handle_data_background_role,\n", | |
" }\n", | |
" \n", | |
" def rowCount(self,index):\n", | |
" logger.debug('index %s:%s',index.row(),index.column())\n", | |
" return len(self._data)\n", | |
" \n", | |
" def columnCount(self,index):\n", | |
" logger.debug('index %s:%s',index.row(),index.column())\n", | |
" try:\n", | |
" return len(self._data[0])\n", | |
" except: \n", | |
" return 0\n", | |
" \n", | |
" def data(self,index,role):\n", | |
" if role in self._data_handlers:\n", | |
" logger.debug('%s index %s:%s',role,index.row(),index.column())\n", | |
" return self._data_handlers[role](index)\n", | |
" \n", | |
" def _handle_data_display_role(self,index):\n", | |
" return self._data[index.row()][index.column()]\n", | |
" \n", | |
" def _handle_data_background_role(self,index):\n", | |
" if index.column() == 1:\n", | |
" return QBrush(Qt.red)\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data = [['hallo']]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2018-05-31 10:16:46,052 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,054 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,055 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,055 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,056 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,058 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,083 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,084 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,084 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:46,085 - __main__ - data - DEBUG - 0 index 0:0\n", | |
"2018-05-31 10:16:46,086 - __main__ - data - DEBUG - 8 index 0:0\n" | |
] | |
} | |
], | |
"source": [ | |
"tableview = QTableView()\n", | |
"mymodel = MyModel(data)\n", | |
"tableview.setModel(mymodel)\n", | |
"tableview.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Update the contents of a Cell" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2018-05-31 10:16:48,146 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:48,147 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:48,147 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:48,148 - __main__ - data - DEBUG - 0 index 0:0\n", | |
"2018-05-31 10:16:48,149 - __main__ - data - DEBUG - 8 index 0:0\n" | |
] | |
} | |
], | |
"source": [ | |
"mymodel._data[0][0] = 'Hallo'\n", | |
"index = mymodel.createIndex(0,0)\n", | |
"mymodel.dataChanged.emit(index,index,[Qt.DisplayRole])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Insert an additional column in the first row" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2018-05-31 10:16:49,717 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:49,722 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:49,723 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:49,724 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:49,725 - __main__ - data - DEBUG - 0 index 0:0\n", | |
"2018-05-31 10:16:49,725 - __main__ - data - DEBUG - 8 index 0:0\n", | |
"2018-05-31 10:16:49,726 - __main__ - rowCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:49,727 - __main__ - columnCount - DEBUG - index -1:-1\n", | |
"2018-05-31 10:16:49,728 - __main__ - data - DEBUG - 0 index 0:1\n", | |
"2018-05-31 10:16:49,728 - __main__ - data - DEBUG - 8 index 0:1\n" | |
] | |
} | |
], | |
"source": [ | |
"i = len(mymodel._data)\n", | |
"mymodel.beginInsertColumns(QModelIndex(), i, i)# a table has no childs so the parent must be an invalid index\n", | |
"data[0].append('Martin')\n", | |
"mymodel.endInsertColumns()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "venv36", | |
"language": "python", | |
"name": "venv36" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment