tags: jptw thesis technology python pyqt5

Notes for PyQt5

Connect PyQt5 with MySQL

[1] Python+PyQt5+MySQL 實現天氣管理系統
[2] How To Connect PyQt5 Application With Mysql Database

Object

Official Doc:
[1] Qt Widgets
[2] Qt Style Sheets Examples

QComboBox

### 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 , 下拉式選單
[2] Get previous and newly selected item on activation in QComboBox
[3] PyQt5 基本教學 (6) 下拉選單、BoxLayout

QFileSystemModel + QTreeView

### 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?
[2] PyQt5 Tutorial | Create an application to view folders and files (code included)
[3] Python: PyQt QTreeview example - selection
[4] Get the text and index of the current selected QTreeView item
[5] Getting the currently selected item in QTreeView
[6] How to show only files using QFileSystemModel and QTreeView?
[7] QItemSelectionModel
[8] how to get the pyqt qtreeview item child using double click event?
[9] 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

QListWdiget

ScrollBar within a QListWidget

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?
[2] PYQT5中给listwidget的滚动条添加滚动信号

Event

Drag and Drop

### 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
[2] drag-and-drop - 在PyQt中,帶有拖放支持的QTreeView
[3] QAbstractItemView Class

Sorting Element

### 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
[2] QTreeWidget - excluding top level items from sort
[3] PyQt: How to get most of QListWidget

UN-Organized

[1] 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
[3] Pyside PyQt, how to connect Key_Delete to 4 different QListWidget
[4] PyQt - trigger an event when nothing is selected in a QListWidget?
[5] Clear selection when clicking on blank area of Item View
[6] Catch which mousebutton is pressed on item
[7] Display hierarchical data with QTreeView widget | PyQt5 Tutorial