本文整理汇总了Python中PyQt5.QtWidgets.QListView.selectedIndexes方法的典型用法代码示例。如果您正苦于以下问题:Python QListView.selectedIndexes方法的具体用法?Python QListView.selectedIndexes怎么用?Python QListView.selectedIndexes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QListView
的用法示例。
在下文中一共展示了QListView.selectedIndexes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: QueryDialog
# 需要导入模块: from PyQt5.QtWidgets import QListView [as 别名]
# 或者: from PyQt5.QtWidgets.QListView import selectedIndexes [as 别名]
class QueryDialog(QDialog):
"""a dialog to choose an item from a query
"""
choice = pyqtSignal(str)
def __init__(self, query):
super().__init__()
self.query = query
self.create_model()
self.init_UI()
def create_model(self):
"""creates the model as QSqlQueryModel,
using the given query
"""
self.model = QSqlQueryModel()
q = QSqlQuery()
q.exec_(self.query)
self.model.setQuery(q)
def init_UI(self):
"""setup the UI
"""
layout = QVBoxLayout()
self.setLayout(layout)
self.resize(200,200)
self.title = "Choose an existing project"
self.list = QListView(self)
layout.addWidget(self.list)
self.list.setModel(self.model)
self.list.setWhatsThis("Choose a project by clicking on it")
self.btn = QPushButton("Accept", self)
layout.addWidget(self.btn)
self.btn.clicked.connect(self.on_btn_clicked)
self.btn.setWhatsThis("Click here to accept your selection (works only if a project has been selected)")
def on_btn_clicked(self):
"""when self.btn is clicked, accept the choice and emit it as self.choice
"""
selected = self.list.selectedIndexes()
if selected:
index = selected[0]
chosen = self.model.data(index, Qt.DisplayRole)
self.choice.emit(chosen)
self.close()
self.choice.emit("")
self.close()
示例2: MDIHistory
# 需要导入模块: from PyQt5.QtWidgets import QListView [as 别名]
# 或者: from PyQt5.QtWidgets.QListView import selectedIndexes [as 别名]
class MDIHistory(QWidget, _HalWidgetBase):
def __init__(self, parent=None):
super(MDIHistory, self).__init__(parent)
self.setMinimumSize(QSize(300, 200))
self.setWindowTitle("PyQt5 editor test example")
lay = QVBoxLayout()
lay.setContentsMargins(0,0,0,0)
self.setLayout(lay)
self.list = QListView()
self.list.setEditTriggers(QListView.NoEditTriggers)
self.list.activated.connect(self.activated)
self.list.setAlternatingRowColors(True)
self.list.selectionChanged = self.selectionChanged
self.model = QStandardItemModel(self.list)
self.MDILine = MDILine()
self.MDILine.soft_keyboard = False
self.MDILine.line_up = self.line_up
self.MDILine.line_down = self.line_down
# add widgets
lay.addWidget(self.list)
lay.addWidget(self.MDILine)
self.reload()
def _hal_init(self):
STATUS.connect('state-off', lambda w: self.setEnabled(False))
STATUS.connect('state-estop', lambda w: self.setEnabled(False))
STATUS.connect('interp-idle', lambda w: self.setEnabled(STATUS.machine_is_on()
and (STATUS.is_all_homed()
or INFO.NO_HOME_REQUIRED)))
STATUS.connect('interp-run', lambda w: self.setEnabled(not STATUS.is_auto_mode()))
STATUS.connect('all-homed', lambda w: self.setEnabled(STATUS.machine_is_on()))
def reload(self, w=None ):
print 'RELOAD'
try:
fp = os.path.expanduser(INFO.MDI_HISTORY_PATH)
with open(fp,'r') as inputfile:
for line in inputfile:
line = line.rstrip('\n')
item = QStandardItem(line)
self.model.appendRow(item)
self.list.setModel(self.model)
self.list.scrollToBottom()
except Exception as e:
print e
LOG.error('File path is not valid: {}]n,()'.format(fp),e)
def line_up(self):
print 'up'
def line_down(self):
print 'down'
def selectionChanged(self,old, new):
cmd = self.getSelected()
self.MDILine.setText(cmd)
def getSelected(self):
selected_indexes = self.list.selectedIndexes()
selected_rows = [item.row() for item in selected_indexes]
# iterates each selected row in descending order
for selected_row in sorted(selected_rows, reverse=True):
text = self.model.item(selected_row).text()
return text
def activated(self):
cmd = self.getSelected()
self.MDILine.setText(cmd)
self.MDILine.submit()
item = QStandardItem(cmd)
self.model.appendRow(item)
self.list.update()
#########################################################################
# This is how designer can interact with our widget properties.
# designer will show the pyqtProperty properties in the editor
# it will use the get set and reset calls to do those actions
#########################################################################
def set_soft_keyboard(self, data):
self.MDILine.soft_keyboard = data
def get_soft_keyboard(self):
return self.MDILine.soft_keyboard
def reset_soft_keyboard(self):
self.MDILine.soft_keyboard = False
# designer will show these properties in this order:
soft_keyboard_option = pyqtProperty(bool, get_soft_keyboard, set_soft_keyboard, reset_soft_keyboard)
示例3: MDIHistory
# 需要导入模块: from PyQt5.QtWidgets import QListView [as 别名]
# 或者: from PyQt5.QtWidgets.QListView import selectedIndexes [as 别名]
class MDIHistory(QWidget, _HalWidgetBase):
def __init__(self, parent=None):
super(MDIHistory, self).__init__(parent)
self.setMinimumSize(QSize(200, 150))
self.setWindowTitle("PyQt5 editor test example")
lay = QVBoxLayout()
lay.setContentsMargins(0,0,0,0)
self.setLayout(lay)
self.list = QListView()
self.list.setEditTriggers(QListView.NoEditTriggers)
self.list.activated.connect(self.activated)
self.list.setAlternatingRowColors(True)
self.list.selectionChanged = self.selectionChanged
self.model = QStandardItemModel(self.list)
self.MDILine = MDILine()
self.MDILine.soft_keyboard = False
self.MDILine.line_up = self.line_up
self.MDILine.line_down = self.line_down
STATUS.connect('reload-mdi-history', self.reload)
# add widgets
lay.addWidget(self.list)
lay.addWidget(self.MDILine)
self.fp = os.path.expanduser(INFO.MDI_HISTORY_PATH)
try:
open(self.fp, 'r')
except:
open(self.fp, 'a+')
LOG.debug('MDI History file created: {}'.format(self.fp))
self.reload()
self.select_row('last')
def _hal_init(self):
STATUS.connect('state-off', lambda w: self.setEnabled(False))
STATUS.connect('state-estop', lambda w: self.setEnabled(False))
STATUS.connect('interp-idle', lambda w: self.setEnabled(STATUS.machine_is_on()
and (STATUS.is_all_homed()
or INFO.NO_HOME_REQUIRED)))
STATUS.connect('interp-run', lambda w: self.setEnabled(not STATUS.is_auto_mode()))
STATUS.connect('all-homed', lambda w: self.setEnabled(STATUS.machine_is_on()))
def reload(self, w=None ):
self.model.clear()
try:
with open(self.fp,'r') as inputfile:
for line in inputfile:
line = line.rstrip('\n')
item = QStandardItem(line)
self.model.appendRow(item)
self.list.setModel(self.model)
self.list.scrollToBottom()
if self.MDILine.hasFocus():
self.select_row('last')
except:
LOG.debug('File path is not valid: {}'.format(fp))
def selectionChanged(self,old, new):
cmd = self.getSelected()
self.MDILine.setText(cmd)
selectionModel = self.list.selectionModel()
if selectionModel.hasSelection():
self.row = selectionModel.currentIndex().row()
def getSelected(self):
selected_indexes = self.list.selectedIndexes()
selected_rows = [item.row() for item in selected_indexes]
# iterates each selected row in descending order
for selected_row in sorted(selected_rows, reverse=True):
text = self.model.item(selected_row).text()
return text
def activated(self):
cmd = self.getSelected()
self.MDILine.setText(cmd)
self.MDILine.submit()
self.select_row('down')
def select_row(self, style):
selectionModel = self.list.selectionModel()
parent = QModelIndex()
self.rows = self.model.rowCount(parent) - 1
if style == 'last':
self.row = self.rows
elif style == 'up':
if self.row > 0:
self.row -= 1
else:
self.row = self.rows
elif style == 'down':
if self.row < self.rows:
self.row += 1
else:
self.row = 0
else:
return
#.........这里部分代码省略.........
示例4: ConfigurationView
# 需要导入模块: from PyQt5.QtWidgets import QListView [as 别名]
# 或者: from PyQt5.QtWidgets.QListView import selectedIndexes [as 别名]
#.........这里部分代码省略.........
config_wide_print_amount,
self._recalculateEffectivePrintAmounts
)
self._recalculateEffectivePrintAmounts = False
if self._selected_counter == 0:
MaterialView.reset_check_state_and_print_amount()
for material in current_config.get_materials():
item = get_item(MaterialView.get_model().invisibleRootItem(), material.get_name())
if item is not None:
print_amount = material_print_amounts[material.get_name()]
if is_checked(selected_item):
check_item(item)
if current_config_name not in self._selected_configs:
print_amount += int(item.text(1))
else:
print_amount = int(item.text(1))
item.setText(1, str(print_amount))
if is_checked(selected_item) and current_config_name not in self._selected_configs:
self._selected_configs[current_config_name] = True
self._selected_counter += 1
if not is_checked(selected_item) and current_config_name in self._selected_configs:
self._selected_configs.pop(current_config_name)
self._selected_counter -= 1
self._currentConfiguration = current_config
def _create_detail_view(self):
"""
Adds the permanent elements to the detail view.
"""
edit_button = QPushButton("Edit")
edit_button.clicked.connect(self._show_edit_view)
self._detailLayout.addWidget(QLabel("Detail view for selected configuration"))
self._detailLayout.addWidget(edit_button)
def _change_config_wide_print_amount(self, new_value):
self._config_wide_print_amounts[self._currentConfiguration.get_name()] = new_value
self._recalculateEffectivePrintAmounts = True
def _create_update_function_for_sub_configs(self, sub_config, configuration):
def update_func(new_value):
if self._currentConfiguration.get_name() == configuration.get_name():
self._currentConfiguration.set_config_print_amount(sub_config, new_value)
self._recalculateEffectivePrintAmounts = True
return update_func
def _show_edit_view(self):
"""
Shows the edit view for the selected configuration.
"""
self._editView.show_for_configuration(self._currentConfiguration)
selected_indexes = self._listView.selectedIndexes()
self._on_selection_change(selected_indexes[0])
def _show_detail_view(self, configuration: Configuration):
"""
Shows the detail view for the selected configuration.
:param configuration:
:type configuration: Configuration
"""
if self._currentDetailPanel is not None:
self._currentDetailPanel.hide()
if configuration.get_name() in self._detailPanels:
self._currentDetailPanel = self._detailPanels[configuration.get_name()]
self._currentDetailPanel.show()
else:
panel = QWidget()
layout = QFormLayout()
panel.setLayout(layout)
config_wide_counter = QSpinBox()
config_wide_counter.setMinimum(1)
config_wide_counter.setValue(1)
config_wide_counter.valueChanged.connect(self._change_config_wide_print_amount)
# add panel to parent layout
self._detailLayout.addWidget(panel)
# save panel for future use
self._detailPanels[configuration.get_name()] = panel
self._currentDetailPanel = panel
self._config_wide_print_amounts[configuration.get_name()] = 1
# panel
layout.addRow("Print this config x times", config_wide_counter)
layout.addRow("Child configurations", QLabel())
configurations = configuration.get_configurations()
config_print_amounts = configuration.get_config_print_amounts()
for config in configurations: # type: Configuration
config_counter = QSpinBox()
config_counter.setMinimum(1)
config_counter.setValue(config_print_amounts[config.get_name()])
update_func = self._create_update_function_for_sub_configs(config, configuration)
config_counter.valueChanged.connect(update_func)
layout.addRow("Print config " + config.get_name() + " x times", config_counter)
self._detailView.show()
示例5: Player
# 需要导入模块: from PyQt5.QtWidgets import QListView [as 别名]
# 或者: from PyQt5.QtWidgets.QListView import selectedIndexes [as 别名]
#.........这里部分代码省略.........
# button to add decoder
if add_button:
add_decoder_btn = QPushButton("Decode Keystrokes of Selected Media")
add_decoder_btn.clicked.connect(add_button)
controlLayout.addWidget(add_decoder_btn)
# controlLayout.addStretch(1)
controlLayout.addWidget(controls)
layout = QVBoxLayout()
layout.addLayout(displayLayout)
hLayout = QHBoxLayout()
hLayout.addWidget(self.slider)
hLayout.addWidget(self.labelDuration)
layout.addLayout(hLayout)
layout.addLayout(controlLayout)
self.setLayout(layout)
if not self.player.isAvailable():
QMessageBox.warning(self, "Service not available",
"The QMediaPlayer object does not have a valid service.\n"
"Please check the media service plugins are installed.")
controls.setEnabled(False)
self.playlistView.setEnabled(False)
openButton.setEnabled(False)
self.metaDataChanged()
self.addToPlaylist(playlist)
def get_current_file(self):
inds = self.playlistView.selectedIndexes()
if len(inds) == 1:
index = inds[0]
location = self.playlistModel.m_playlist.media(index.row()).canonicalUrl()
return location.path()
def open(self):
fileNames, _ = QFileDialog.getOpenFileNames(self, "Open Files")
self.addToPlaylist(fileNames)
def addToPlaylist(self, fileNames):
for name in fileNames:
fileInfo = QFileInfo(name)
if fileInfo.exists():
url = QUrl.fromLocalFile(fileInfo.absoluteFilePath())
if fileInfo.suffix().lower() == 'm3u':
self.playlist.load(url)
else:
self.playlist.addMedia(QMediaContent(url))
else:
url = QUrl(name)
if url.isValid():
self.playlist.addMedia(QMediaContent(url))
def durationChanged(self, duration):
duration /= 1000
self.duration = duration
self.slider.setMaximum(duration)
def positionChanged(self, progress):