Created
October 15, 2019 22:24
-
-
Save wmalarski/386cb5f6ea4021297eb944c917da18ae to your computer and use it in GitHub Desktop.
QFileDialog embedded in QWidget, QIdentityProxyModel example
This file contains hidden or 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
import sys | |
from typing import Any | |
from PySide2.QtCore import QAbstractProxyModel, QModelIndex, QIdentityProxyModel, QSortFilterProxyModel, QFileInfo, Qt | |
from PySide2.QtWidgets import QApplication, QFileDialog, QWidget, QGridLayout, QLabel, QDialogButtonBox, QPushButton, \ | |
QSizeGrip, QSplitter, QFrame, QListView, QToolButton, QFileSystemModel | |
# class AdditionalColumnsProxyModel(QIdentityProxyModel): | |
# | |
# def is_extra(self, index: QModelIndex) -> bool: | |
# return index.column() >= self.sourceModel().columnCount() | |
# | |
# def columnCount(self, parent: QModelIndex = ...) -> int: | |
# return self.sourceModel().columnCount() + 1 | |
# | |
# def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> Any: | |
# if role == Qt.DisplayRole and section >= self.sourceModel().columnCount(): | |
# return "0" | |
# return super().headerData(section, orientation, role) | |
# | |
# def data(self, proxy_index: QModelIndex, role: int = ...) -> Any: | |
# source_index = self.mapToSource(proxy_index) | |
# | |
# if role == Qt.DisplayRole: | |
# if self.is_extra(proxy_index): | |
# sibling = proxy_index.siblingAtColumn(0) | |
# print(proxy_index.internalId()) | |
# print(sibling, sibling.row(), sibling.column(), sibling.isValid()) | |
# # source_index = self.mapToSource(sibling) | |
# # print(source_index, source_index.row(), source_index.column(), source_index.isValid()) | |
# # print(sibling.data(role)) | |
# return '0' | |
# # return "Log" if file_info.suffix() == "log" else "" | |
# return source_index.data(role) | |
# | |
# def index(self, row: int, column: int, parent: QModelIndex=...) -> QModelIndex: | |
# if column == self.columnCount() - 1: | |
# return self.createIndex(row, column) | |
# return super().index(row, column, parent) | |
class HighLightProxyModel(QIdentityProxyModel): | |
def data(self, proxy_index: QModelIndex, role: int = ...) -> Any: | |
source_index = self.mapToSource(proxy_index) | |
data = source_index.data(role) | |
if role == Qt.DisplayRole: | |
file_info: QFileInfo = self.sourceModel().fileInfo(source_index) | |
if file_info.suffix() == "log": | |
return data + 'color_LOG' | |
return data | |
class SilentFileWidget(QFileDialog): | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
self.setViewMode(QFileDialog.Detail) | |
self.setOption(QFileDialog.DontUseNativeDialog, True) | |
self.setStyleSheet("background: blue") | |
proxy = HighLightProxyModel(self) | |
self.setProxyModel(proxy) | |
for child in self.children(): | |
if isinstance(child, (QDialogButtonBox, QSizeGrip)): | |
child.setVisible(False) | |
# favorite button | |
favorite_button = QToolButton(self) | |
favorite_button.setText(self.tr("Favourite")) | |
favorite_button.clicked.connect(self._on_favorite_button_clicked) | |
buttons_layout = self.layout().itemAt(1).layout() | |
buttons_layout.addWidget(favorite_button) | |
def _on_favorite_button_clicked(self): | |
self.setSidebarUrls(self.sidebarUrls() + self.selectedUrls()) | |
def accept(self): | |
"""Ignoring accept action""" | |
class MyWidget(QWidget): | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
layout = QGridLayout(self) | |
self.setLayout(layout) | |
# text over dialog | |
header_label = QLabel(self.tr("I co?")) | |
layout.addWidget(header_label, 0, 0) | |
# file dialog | |
self._file_form = SilentFileWidget() | |
layout.addWidget(self._file_form, 1, 0) | |
# project loading | |
load_button = QPushButton(self.tr("Dawaj kurwa ten projekt"), self) | |
load_button.clicked.connect(self._on_load_button_clicked) | |
layout.addWidget(load_button, 2, 0) | |
def _on_load_button_clicked(self): | |
print(self._file_form.selectedFiles()) | |
if __name__ == '__main__': | |
# Create the Qt Application | |
app = QApplication(sys.argv) | |
# Create and show the form | |
widget = MyWidget() | |
widget.show() | |
# Run the main Qt loop | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment