###### tags: `jptw` `thesis` `technology` `python` `pyqt5`
# Notes for PyQt5
## Connect PyQt5 with MySQL
> [1] [Python+PyQt5+MySQL 實現天氣管理系統](https://www.itread01.com/content/1544677217.html)
> [2] [How To Connect PyQt5 Application With Mysql Database](https://codeloop.org/connect-pyqt5-with-mysql-database/)
## Object
> Official Doc:
> [1] [Qt Widgets](https://doc.qt.io/qt-5/qtwidgets-index.html#getting-started)
> [2] [Qt Style Sheets Examples](https://doc.qt.io/qt-5/stylesheet-examples.html)
### QComboBox

```python
### Create object
self.ComboBox = ComboBox()
self.ComboBox.addItem('Item 1')
self.ComboBox.addItems(['Item 1', 'Item 2', 'Item 3', 'Item 4'])
### Get current item
self.ComboBox.currentText()
### Find item and set in list
idx = self.ComboBox.findText(text, Qt.MatchFixedString)
if idx >= 0:
self.ComboBox.setCurrentIndex(idx)
```
> Ref:
> [1] [PyQT5 教學--- QComoBox , 下拉式選單](http://sammypython.blogspot.com/2019/01/pyqt5-qcomobox.html)
> [2] [Get previous and newly selected item on activation in QComboBox](https://stackoverflow.com/questions/52270916/get-previous-and-newly-selected-item-on-activation-in-qcombobox)
> [3] [PyQt5 基本教學 (6) 下拉選單、BoxLayout](https://clay-atlas.com/blog/2019/09/06/python-chinese-tutorial-%E4%B8%8B%E6%8B%89%E5%BC%8F%E9%81%B8%E5%96%AE%E3%80%81%E5%B8%83%E5%B1%80%E5%85%83%E4%BB%B6/)
### QFileSystemModel + QTreeView

```python
### Create object
self.model = QFileSystemModel()
self.model.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot | QDir.AllEntries) # Show only files
self.model.setRootPath(dir_path)
self.tree = QTreeView()
self.tree.setModel(self.model)
self.tree.setRootIndex(self.model.index(dir_path))
### Trigger func with selection changed
self.tree.selectionModel().selectionChanged.connect(self.function)
### Triger func with double clicked
self.tree.doubleClicked.connect(self.function)
### Remove header
self.model.removeRows( 0, self.model.rowCount() )
### Get selected item [5][6]
for idx in self.TreeView.selectedIndexes():
crawler = idx.model().itemData(idx)
print(crawler[0])
```
> Ref:
> [1] [Change the style of a checkbox in a QTreeWidget without affecting the check marks in Maya?](https://stackoverflow.com/questions/54655382/change-the-style-of-a-checkbox-in-a-qtreewidget-without-affecting-the-check-mark)
> [2] [PyQt5 Tutorial | Create an application to view folders and files (code included)](https://www.youtube.com/watch?v=1i-7vlxeiwI)
> [3] [Python: PyQt QTreeview example - selection](https://stackoverflow.com/questions/23993895/python-pyqt-qtreeview-example-selection/24045757)
> [4] [Get the text and index of the current selected QTreeView item](https://stackoverflow.com/questions/47621042/get-the-text-and-index-of-the-current-selected-qtreeview-item?rq=1)
> [5] [Getting the currently selected item in QTreeView](https://stackoverflow.com/questions/7047527/getting-the-currently-selected-item-in-qtreeview)
> [6] [How to show only files using QFileSystemModel and QTreeView?](https://stackoverflow.com/questions/45297192/how-to-show-only-files-using-qfilesystemmodel-and-qtreeview)
> [7] [QItemSelectionModel](https://doc.qt.io/qtforpython/PySide2/QtCore/QItemSelectionModel.html)
> [8] [how to get the pyqt qtreeview item child using double click event?](https://stackoverflow.com/questions/29680105/how-to-get-the-pyqt-qtreeview-item-child-using-double-click-event?fbclid=IwAR0a7NTQmQeXsNoSRzb73OUmUgS0j88zgluRO_AqCeOinRocF4Rqv_Qb74w)
> [9] [How do I delete all items from a QStandardItemModel](https://stackoverflow.com/questions/23269866/how-do-i-delete-all-items-from-a-qstandarditemmodel)
>
> Other:
> [1] [Two QListView box one showing files in a folder and one shows selected files from the first QListview
](https://stackoverflow.com/questions/53270404/two-qlistview-box-one-showing-files-in-a-folder-and-one-shows-selected-files-fro)
### QListWdiget
#### ScrollBar within a QListWidget

```python
self.ListWidget.verticalScrollBar().setStyleSheet("""
QScrollBar:vertical {
border: none;
background:white;
width:3px;
margin: 0px 0px 0px 0px;
}
QScrollBar::handle:vertical {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130), stop:1 rgb(32, 47, 130));
min-height: 0px;
}
QScrollBar::add-line:vertical {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130), stop:1 rgb(32, 47, 130));
height: 0px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}
QScrollBar::sub-line:vertical {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130), stop:1 rgb(32, 47, 130));
height: 0 px;
subcontrol-position: top;
subcontrol-origin: margin;
}
""")
```
> Ref:
> [1] [How to set the style to a ScrollBar within a QListWidget?](https://stackoverflow.com/questions/50556514/how-to-set-the-style-to-a-scrollbar-within-a-qlistwidget)
> [2] [PYQT5中给listwidget的滚动条添加滚动信号](https://blog.csdn.net/z981314046/article/details/84653915)
## Event
### Drag and Drop
```python
### Example with treeview
self.treeView = QTreeView()
self.treeView.setObjectName("testView")
self.treeView.setDragDropMode(QAbstractItemView.InternalMove)
self.treeView.setSelectionMode(QAbstractItemView.ExtendedSelection)
```
> Ref:
> [1] [Preserve QStandardItem subclasses in drag and drop](https://stackoverflow.com/questions/40413221/preserve-qstandarditem-subclasses-in-drag-and-drop)
> [2] [drag-and-drop - 在PyQt中,帶有拖放支持的QTreeView](https://hant-kb.kutu66.com/python/post_1520823)
> [3] [QAbstractItemView Class](https://doc.qt.io/qt-5/qabstractitemview.html#DragDropMode-enum)
### Sorting Element
```python
### Treeview
self.tree = QTreeView()
self.tree.setSortingEnabled(true);
self.tree.sortByColumn(0, Qt.AscendingOrder);
### Listwidget
self.list = QListWidget()
self.list.sortItems() # ascending by default
self.list.sortItems(QtCore.Qt.DescendingOrder)
```
> Ref:
> [1] [Sorting and filtering table data with QSortFilterProxyModel](https://www.walletfox.com/course/qsortfilterproxymodelexample.php)
> [2] [QTreeWidget - excluding top level items from sort](https://stackoverflow.com/questions/39318827/qtreewidget-excluding-top-level-items-from-sort)
> [3] [PyQt: How to get most of QListWidget](https://stackoverflow.com/questions/22489018/pyqt-how-to-get-most-of-qlistwidget)
## UN-Organized
> [1] [How to Get Absolute Path of Currently Selected Item in QTreeView](https://stackoverflow.com/questions/17865762/how-to-get-absolute-path-of-currently-selected-item-in-qtreeview)
> [2] [Passing variables, creating instances, self, The mechanics and usage of classes: need explanation](https://stackoverflow.com/questions/11421659/passing-variables-creating-instances-self-the-mechanics-and-usage-of-classes)
> [3] [Pyside PyQt, how to connect Key_Delete to 4 different QListWidget](https://stackoverflow.com/questions/21958535/pyside-pyqt-how-to-connect-key-delete-to-4-different-qlistwidget/21960260)
> [4] [PyQt - trigger an event when nothing is selected in a QListWidget?](https://stackoverflow.com/questions/37867392/pyqt-trigger-an-event-when-nothing-is-selected-in-a-qlistwidget)
> [5] [Clear selection when clicking on blank area of Item View](https://stackoverflow.com/questions/8516163/clear-selection-when-clicking-on-blank-area-of-item-view)
> [6] [Catch which mousebutton is pressed on item](https://stackoverflow.com/questions/9312287/catch-which-mousebutton-is-pressed-on-item)
> [7] [Display hierarchical data with QTreeView widget | PyQt5 Tutorial](https://learndataanalysis.org/display-hierarchical-data-with-qtreeview-widget-pyqt5-tutorial/?fbclid=IwAR0wP0eVIVad2yZIEqngdnPOf69IIrmAGwA8eDHTYiaA8U9Uu5jmRIbF5hU)