I need to select the default file that appears in a combobox from a directory listing of files. With a normal combobox, it is easy enough to find the index of the value you want using .findText, but this does not appear to work for QFileSystemModel comboboxes, possibly due to the way the list of options does not populate until the directory listing can be resourced.
Here is what I have tried:
import sys
import collections
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QComboBox
from PyQt5.QtCore import QSize, QRect
class ComboWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(640, 140))
self.setWindowTitle("Combobox example")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
# Create combobox and add items.
self.fsm = QtWidgets.QFileSystemModel()
self.fsm.setNameFilters(["*.txt"])
self.configComboBox = QtWidgets.QComboBox(self)
self.configComboBox.setGeometry(QRect(40, 40, 491, 31))
self.configComboBox.setObjectName(("comboBox"))
self.configComboBox.setModel(self.fsm)
self.fsm.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
# "text_files" is a subdir of current dir with files
# example1.txt, example2.txt, example3.txt
self.configComboBox.setRootModelIndex(self.fsm.setRootPath("text_files"))
# V V This section does not work V V
index = self.configComboBox.findText(MainConfig.settings["default_txt_file"])
self.configComboBox.setCurrentIndex(index)
class MainConfig:
settings = collections.OrderedDict()
#staticmethod
def createDefaultConfig(name):
MainConfig.settings["default_txt_file"] = "example3.txt"
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ComboWindow()
mainWin.show()
sys.exit( app.exec_() )
As I indicated in this answer, the QFileSystemModel is loaded asynchronously in a new thread so you must use the directoryLoaded signal to know when the information is finished loading:
import collections
import os
import sys
from PyQt5.QtWidgets import (
QApplication,
QComboBox,
QFileSystemModel,
QMainWindow,
QWidget,
)
from PyQt5.QtCore import pyqtSlot, QDir, QRect, QSize
class ComboWindow(QMainWindow):
def __init__(self, parent=None):
super(ComboWindow, self).__init__(parent=None)
self.setMinimumSize(QSize(640, 140))
self.setWindowTitle("Combobox example")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
# Create combobox and add items.
self.fsm = QFileSystemModel()
self.fsm.setNameFilters(["*.txt"])
self.configComboBox = QComboBox(self)
self.configComboBox.setGeometry(QRect(40, 40, 491, 31))
self.configComboBox.setObjectName(("comboBox"))
self.configComboBox.setModel(self.fsm)
self.fsm.setFilter(QDir.NoDotAndDotDot | QDir.Files)
self.fsm.directoryLoaded.connect(self.on_directoryLoaded)
current_dir = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.join(current_dir, "text_files")
self.configComboBox.setRootModelIndex(self.fsm.setRootPath(dir_path))
#pyqtSlot(str)
def on_directoryLoaded(self, path):
index = self.configComboBox.findText(MainConfig.settings["default_txt_file"])
self.configComboBox.setCurrentIndex(index)
class MainConfig:
settings = collections.OrderedDict()
#staticmethod
def createDefaultConfig(name):
MainConfig.settings["default_txt_file"] = name
if __name__ == "__main__":
MainConfig.createDefaultConfig("example3.txt")
app = QApplication(sys.argv)
mainWin = ComboWindow()
mainWin.show()
sys.exit(app.exec_())
Related
I am using the code below. Mainwindow's state is preserved but qtreeviw's is not.
import sys
from PyQt5.QtCore import QSettings, QByteArray
from PyQt5.QtWidgets import QApplication, QTreeView, QFileSystemModel, QVBoxLayout, QWidget
from PyQt5 import QtCore
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.tree = QTreeView()
self.settings = QSettings('testorg', 'testapp')
try:
self.tree.header().restoreState(self.settings.value("estado_header"))
self.resize(self.settings.value('window size'))
self.move(self.settings.value('window position'))
except:
pass
self.model = QFileSystemModel()
self.model.setRootPath(r"C:\Users\dan-s>")
self.tree.setModel(self.model)
#self.tree.header().restoreState(self.settings.value("estado_header"))
#self.tree.collapseAll()
layout = QVBoxLayout()
layout.addWidget(self.tree)
self.setLayout(layout)
def closeEvent(self, event):
self.settings.setValue('window size', self.size())
self.settings.setValue('window position', self.pos())
state = self.tree.header().saveState()
self.settings.setValue('estado_header', state)
super().closeEvent(event)
app = QApplication(sys.argv)
demo = MyApp()
demo.show()
sys.exit(app.exec_())
I've tried other ways but I can't solve it.
I'm trying to integrate QFontComboBox within Qmenu.
I try to make two things happen when selecting a particular font from the menu:
The Qmenu will close.
print the selected font.
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QIcon, QFont, QCursor
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QFontComboBox, QWidgetAction, QMenu, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Pyside2 FontCombo Box")
self.setGeometry(300,200,300,250)
self.setFontBox()
self.setIcon()
self.show()
def setIcon(self):
appIcon = QIcon("icon.png")
self.setWindowIcon(appIcon)
def setFontBox(self):
self.font_button = QPushButton(self)
self.font_button.setFixedWidth(300)
self.font_button.clicked.connect(lambda: self.setFontmenu())
vbox = QVBoxLayout()
vbox.addWidget(self.font_button)
self.setLayout(vbox)
def setFontmenu(self):
font_menu = QMenu()
font_submenu = QFontComboBox()
font_submenu.setCurrentFont(QFont("Arial"))
objectTest = QObject()
widget = QWidgetAction(objectTest)
widget.setDefaultWidget(font_submenu)
font_menu.addAction(widget)
font_menu.exec_(QCursor.pos())
menu = font_menu
menu.addSeparator()
font_submenu.showPopup()
font_submenu.setFocus()
font_submenu.currentFontChanged.connect(self._changed)
def _changed(self):
font = self.currentFont().family()
print(font)
return
myapp = QApplication(sys.argv)
window = Window()
myapp.exec_()
sys.exit()
I tried codes below, but it seemed not work as normal. I cannot use embedded cmd whatever I did. It looks like decorations. I mean I just want to use the cmd like the ordinary one. Here, I paste the code, and any advice would be greatly appreciated.
I develop this app on Python 3.7.3 conda environment, Window10.
import sys
import subprocess
import time
import win32gui
from PyQt5.QtCore import QProcess, Qt
from PyQt5.QtGui import QWindow, QIcon, QFont
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMdiArea, QSplitter, QTextBrowser
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from win32com import client
from win32gui import GetWindowText, EnumWindows,SetForegroundWindow
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.p = QProcess()
self.layout = QVBoxLayout()
self.mdi = QMdiArea()
self.mainSplitter = QSplitter(Qt.Vertical)
self.setCentralWidget(self.mainSplitter)
self.mainSplitter.addWidget(QTextBrowser())
self.initUI()
def initUI(self):
self.runExe()
EnumWindows(self.set_cmd_to_foreground, None)
hwnd1 = win32gui.GetForegroundWindow()
#hwnd1 = win32gui.FindWindow(None, "C:\\Windows\\system32\\calc.exe")
print(hwnd1)
window = QWindow.fromWinId(hwnd1)
container_widge = self.createWindowContainer(window, self)
container_widge.setFocusPolicy(Qt.TabFocus)
container_widge.setFocus()
container_widge.setWindowTitle("ain")
container_widge.setFont(QFont("Times New Roman"))
container_widge.setGeometry(500, 500, 450, 400)
#container_widge.setFocusPolicy()
container_widge.activateWindow()
container_widge.acceptDrops()
container_widge.grabMouse()
container_widge.setMouseTracking(True)
self.mainSplitter.addWidget(container_widge)
self.showMaximized()
#self.setGeometry(200, 200, 700, 700)
#self.show()
def runExe(self):
shell.run("cmd.exe")
time.sleep(1)
def set_cmd_to_foreground(self, hwnd, extra):
"""sets first command prompt to forgeround"""
if "cmd.exe" in GetWindowText(hwnd):
print(hwnd)
SetForegroundWindow(hwnd)
return
def run_script(self, shell, scripts):
"""runs the py script"""
shell.SendKeys(scripts+"{ENTER}")
if __name__ == '__main__':
shell = client.Dispatch("WScript.Shell")
app = QApplication(sys.argv)
ex = Example()
#ex.run_script(shell, "python -m pip list")
#ex.show()
sys.exit(app.exec_())
you can try this I got it from this blog:
import sys
from PyQt5.QtCore import QProcess, QTextStream
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit,
QLineEdit, QVBoxLayout, QHBoxLayout, QWidget, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.process = QProcess(self)
self.output = QTextEdit(self)
self.input = QLineEdit(self)
self.run_command_button = QPushButton("Run Command", self)
layout = QVBoxLayout()
input_layout = QHBoxLayout()
input_layout.addWidget(self.input)
input_layout.addWidget(self.run_command_button)
layout.addLayout(input_layout)
layout.addWidget(self.output)
central_widget = QWidget(self)
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.process.readyReadStandardOutput.connect(self.read_output)
self.run_command_button.clicked.connect(self.run_command)
self.process.start("cmd.exe")
def read_output(self):
stream = QTextStream(self.process)
self.output.append(stream.readAll())
def run_command(self):
command = self.input.text() + "\n"
self.process.write(command.encode())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I have a QTextEdit which holds some formatted text. How do I return all text that matches a specific font size and/or font color?
I have tried the QTextDocument.objectForFormat() method, as an argument I provided a QTextFormat object with the property FontPointSize, but that returns None.
import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QApplication
from PyQt5.QtGui import QColor
from PyQt5 import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.textEdit.setTextColor(QColor('#00aaff'))
self.textEdit.setFontPointSize(14.0)
self.textEdit.insertPlainText('Title\n')
self.textEdit.setTextColor(QColor('#e0e0e0'))
self.textEdit.setFontPointSize(11.0)
self.textEdit.insertPlainText('content\n')
self.textEdit.setTextColor(QColor('#00aaff'))
self.textEdit.setFontPointSize(14.0)
self.textEdit.insertPlainText('Title2\n')
self.textEdit.setTextColor(QColor('#e0e0e0'))
self.textEdit.setFontPointSize(11.0)
self.textEdit.insertPlainText('content_title2')
self.printFontSizes()
self.show()
def printFontSizes(self):
doc = self.textEdit.document()
for i in range(doc.blockCount()):
print(doc.findBlockByNumber(i).text(),': ',
doc.findBlockByNumber(i).charFormat().fontPointSize())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
Running the above code prints this:
Title : 0.0
content : 14.0
Title2 : 11.0
content_title2 : 14.0
Is there something I am doing wrong here?
Since in my particular example the above code was displaying the wrong font size, I instead solved the problem by iterating over each line in the QTextEdit using QTextCursor and checking each one's font size. Here's the relevant code:
#!/bin/python3
import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QApplication
from PyQt5.QtGui import QColor, QTextCursor
from PyQt5 import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.textEdit.setTextColor(QColor('#00aaff'))
self.textEdit.setFontPointSize(14.0)
self.textEdit.insertPlainText('Title\n')
self.textEdit.setTextColor(QColor('#e0e0e0'))
self.textEdit.setFontPointSize(11.0)
self.textEdit.insertPlainText('content\n')
self.textEdit.setTextColor(QColor('#00aaff'))
self.textEdit.setFontPointSize(14.0)
self.textEdit.insertPlainText('Title2\n')
self.textEdit.setTextColor(QColor('#e0e0e0'))
self.textEdit.setFontPointSize(11.0)
self.textEdit.insertPlainText('content_title2\n')
self.textEdit.insertPlainText('content2_title2\n')
self.printFontSizes()
self.show()
def printFontSizes(self):
doc = self.textEdit.document()
self.textEdit.moveCursor(QTextCursor.Start)
for i in range(doc.blockCount()):
if self.textEdit.fontPointSize() == 14.0:
print(doc.findBlockByNumber(i).text())
self.textEdit.moveCursor(QTextCursor.NextBlock)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
Tabs added before app.exec_() is called look and act as any other tabs u met, though if adding another after the app.exec_() call makes the new tab 'detach' from the main app window. Pic below :)
Why? How can I make it move inside the window?
import threading
import time
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTabWidget
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QWidget
class ATry(threading.Thread):
def __init__(self):
super().__init__()
def run(self):
time.sleep(1)
anotherTextEdit = QTextEdit()
anotherLineEdit = QLineEdit()
anotherLayout = QFormLayout()
anotherLayout.addRow(anotherTextEdit)
anotherLayout.addRow(anotherLineEdit)
anotherTab = QWidget()
anotherTab.setLayout(anotherLayout)
md.addTab(anotherTab, "Outside")
app = QApplication(sys.argv)
md = QTabWidget()
aTextEdit = QTextEdit()
aLineEdit = QLineEdit()
layout = QFormLayout()
layout.addRow(aTextEdit)
layout.addRow(aLineEdit)
thisTab = QWidget()
thisTab.setLayout(layout)
md.addTab(thisTab, "Inside")
a = ATry()
a.start()
md.show()
app.exec_()
Screen describing the problem
It works with QTimer or signals:
import sys
import time
from PyQt5.QtCore import QObject
from PyQt5.QtCore import QThread
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTabWidget
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QWidget
class ATry(QThread):
def __init__(self, pointer):
super().__init__()
self.pointer = pointer
def run(self):
time.sleep(2)
self.pointer.emit()
def addTheTab():
anotherTextEdit = QTextEdit()
anotherLineEdit = QLineEdit()
anotherLayout = QFormLayout()
anotherLayout.addRow(anotherLineEdit)
anotherLayout.addRow(anotherTextEdit)
anotherTab = QWidget()
anotherTab.setLayout(anotherLayout)
md.addTab(anotherTab, "Whatever")
class MyQObject(QObject):
trigger = pyqtSignal()
def __init__(self):
super().__init__()
def connect_and_get_trigger(self):
self.trigger.connect(addTheTab)
return self.trigger
def getGFX(self):
app = QApplication(sys.argv)
md = QTabWidget()
md.show()
return app, md
obj = MyQObject()
app, md = obj.getGFX()
addTheTab()
a = ATry(obj.connect_and_get_trigger())
a.start()
# timer = QTimer()
# timer.timeout.connect(proba)
# timer.start(3000)
app.exec_()