{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List widget\n\nThis demo shows how to use the auto-connection feature\nfor automatically connecting signals from widgets to methods.\n\nThe base requirement for this is, that the ui-file is wrapped\nby a class in Python.\n\n.. hint::\n This demo uses specially wrapped methods of QListWidget. For more information see\n section 'Calling slots' in https://itom.bitbucket.io/latest/docs/06_extended_gui/qtdesigner.html)\n \n These methods are indiciated by #-> special method call\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itomUi import (\n ItomUi,\n) # import the base class ItomUi from the module itomUi in the itom-packages subfolder\nfrom itom import ui\n\n\nclass ListWidgetDemo(ItomUi): # ListWidgetDemo is inherited from ItomUi\n def __init__(self): # constructor\n # call constructor of ItomUi like it would be the constructor of the class itom.ui:\n ItomUi.__init__(self, \"listWidgetDemo.ui\", ui.TYPEWINDOW)\n\n # from now on, you can use the member self.gui to access the handle of the user interface\n self.on_btnAddItems_clicked()\n\n @ItomUi.autoslot(\"\") # the signal is clicked()\n def on_btnAddItems_clicked(self):\n count = self.gui.listMain[\"count\"]\n size = 3\n newItemTexts = [\"item %i\" % i for i in range(count, count + size)]\n\n self.gui.listMain.call(\n \"addItems\", newItemTexts\n ) # -> special method call\n\n # define the flags which parameterize every item in the list (individually, if desired)\n # the flag is an OR-combination of the enumeration Qt::ItemFlag\n flagSelectable = 1 # Qt::ItemIsSelectable\n flagCheckable = 16 # Qt::ItemIsUserCheckable\n flagEnabled = 32 # Qt::ItemIsEnabled\n flagTristate = 256 # Qt::ItemIsUserTristate\n flag1 = (\n flagSelectable | flagCheckable | flagEnabled\n ) # only checkable with on/off state\n flag2 = flag1 | flagTristate # checkable with on/off/partially state\n\n # the check state is the state of the checkbox, according Qt::CheckState enumeration\n checked = 2 # checked\n partially = 1 # partially\n unchecked = 0 # unchecked\n\n # set flags of all new items\n for i in range(count, count + size):\n\n if i % 2 == 0:\n self.gui.listMain.call(\n \"setFlags\", i, flag1\n ) # -> special method call\n self.gui.listMain.call(\n \"setCheckState\", i, checked\n ) # -> special method call\n else:\n self.gui.listMain.call(\n \"setFlags\", i, flag2\n ) # -> special method call\n self.gui.listMain.call(\n \"setCheckState\", i, partially\n ) # -> special method call\n\n @ItomUi.autoslot(\"\") # the signal is clicked()\n def on_btnClearAll_clicked(self):\n self.gui.listMain.call(\"clear\")\n\n @ItomUi.autoslot(\"\") # connect to clicked() signal of btnEval\n def on_btnEval_clicked(self):\n count = self.gui.listMain[\"count\"]\n for i in range(count):\n itemText = self.gui.listMain.call(\n \"item\", i\n ) # -> special method call\n itemFlags = self.gui.listMain.call(\n \"flags\", i\n ) # -> special method call\n checkState = self.gui.listMain.call(\n \"checkState\", i\n ) # -> special method call\n checkStateStr = [\"unchecked\", \"partially\", \"checked\"][checkState]\n print(\n \"Item %i: %s, flags: %i, check state: %s\"\n % (i, itemText, itemFlags, checkStateStr)\n )\n\n @ItomUi.autoslot(\"int\") # the signal is currentRowChanged(int)\n def on_listMain_currentRowChanged(self, row):\n print(\"current row changed to row:\", row)\n\n\n# create a first instance of ListWidgetDemo and the gui\nwin1 = ListWidgetDemo()\nwin1.gui.show() # show the gui" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }