Using a search bar to find items in a list - pyqt5

I want to use a line edit as a search bar in order to find items in a Qlistwidget. I also want the qlistwidget to scroll up/down (in search) as text is being changed in the line edit.
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QGridLayout, QWidget, QListWidget, QLineEdit
class Window(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.ListBox = QListWidget()
self.ListBox.insertItem(0,'Temperature')
self.ListBox.insertItem(1,'Mass')
self.ListBox.insertItem(2,'Length')
self.ListBox.insertItem(3,'Height')
self.ListBox.insertItem(4,'Width')
self.ListBox.insertItem(5,'Volume')
self.ListBox.insertItem(6,'Surface_Area')
self.ListBox.insertItem(7,'Material')
self.ListBox.insertItem(8,'Location')
self.ListBox.insertItem(9,'Strength')
self.ListBox.insertItem(10,'Color')
self.Search_Bar = QLineEdit()
layout = QGridLayout(centralWidget)
layout.addWidget(self.ListBox)
layout.addWidget(self.Search_Bar)
self.Search_Bar.textChanged.connect(self.Search)
def Search(self):
if self.Search_Bar.text() == 'Strength':
pass
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

The internally implemented match function provided by all Qt item models is usually faster than cycling through the list via Python.
def Search(self, text):
model = self.ListBox.model()
match = model.match(
model.index(0, self.ListBox.modelColumn()),
QtCore.Qt.DisplayRole,
text,
hits=1,
flags=QtCore.Qt.MatchStartsWith)
if match:
self.ListBox.setCurrentIndex(match[0])
This will automatically select and scroll to the first item found (if any).

Related

PyQt5 input text can't be executed as code in QPlainTextEdit

I have a QMainWindow which contains a QPlainTextEdit and a button with clicked even connected. When user finishes text input and press the button, I just want to execute user input for example "1+1". I should get "2", but it is "1+1". Very appreciated for your reply, thanks!
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, \
QApplication, QVBoxLayout, QPlainTextEdit, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(600, 600)
l_layout = QVBoxLayout()
self.edit = QPlainTextEdit()
self.edit.setFixedSize(400, 300)
self.edit1 = QPlainTextEdit()
self.edit1.setFixedSize(100, 100)
self.btn = QPushButton('Test')
self.btn.clicked.connect(self.press)
l_layout.addWidget(self.edit)
l_layout.addWidget(self.edit1)
l_layout.addWidget(self.btn)
dummy_widget = QWidget()
dummy_widget.setLayout(l_layout)
self.setCentralWidget(dummy_widget)
def press(self):
text = self.edit.toPlainText()
try:
code = """print(text)"""
exec(code)
except Exception as e:
print('not ok!')
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
You should evaluate the input text and then print it, like so:
def press(self):
text = self.edit.toPlainText()
try:
print(eval(text))
except Exception as e:
print('not ok!')
Pay attention, because eval() use can lead to security issues (people executing python code on your app). Make sure your input is sanitized.

How to set QFrame color in an eventFilter?

I have a simple QWidget that contains a frame and two labels. I want to use eventFilter to change QFrame background color on label hover. Can someone please check the below code and tell me why I can't change the QFrame background and if it is the correct way for doing it?
import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox, QFrame, QLabel
from PyQt5 import QtCore
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.layout = QHBoxLayout(self)
self.frame = QFrame(self)
self.setObjectName("frame")
self.frame_lay = QHBoxLayout()
self.one_label = QLabel(self.frame)
self.one_label.setText("one")
self.one_label.setObjectName("one")
self.two_label = QLabel(self.frame)
self.two_label.setText("two")
self.two_label.setObjectName("two")
self.one_label.installEventFilter(self)
self.two_label.installEventFilter(self)
self.frame_lay.addWidget(self.one_label)
self.frame_lay.addWidget(self.two_label)
self.frame.setStyleSheet("""QFrame{background-color: red;}""")
self.frame.setLayout(self.frame_lay)
self.frame_lay.setContentsMargins(0, 0, 0, 0)
self.layout.addWidget(self.frame)
self.setLayout(self.layout)
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.Enter:
if type(obj) == QLabel:
if obj.objectName() in ["one", "two"]:
print(obj.objectName())
self.frame.setStyleSheet("""QFrame#frame{background-color: blue;}""")
return super(QWidget, self).eventFilter(obj, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Should the installEventFilter be applied to QWidget or QFrame? The labels are contained within the QFrame.
Thanks
You set the frame object name for the "MainWindow", but in the event filter you used the object name for a QFrame class.
Just set the object name for the frame instead:
self.frame.setObjectName("frame")
Note that QLabel inherits from QFrame, so, using QFrame{background-color: red;} technically applies the background for both the parent frame and any child label.
In case you want to be more specific, you either use the object name as you did in the event filter, or use the .ClassName selector, which applies the sheet only to the class and not its subclasses (note the full stop character before QFrame):
self.frame.setStyleSheet(""".QFrame{background-color: red;}""")

pyqt5 videowidget not showing in layout

I am writing a program with pyqt5 where pressing a button first cycles through some pictures then cycles through some videos.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
import argparse
import sys
class MainWindow(QMainWindow):
def __init__(self,args):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = CentralWidget(args)
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
QCoreApplication.instance().quit()
self.centralwidget.startvid()
class CentralWidget(QWidget):
def __init__(self,args):
super(CentralWidget, self).__init__()
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignCenter)
self.setLayout(self.layout)
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.vw = QVideoWidget()
self.player.setVideoOutput(self.vw)
def startvid(self):
self.layout.addWidget(self.vw)
url= QUrl.fromLocalFile(glob.glob("videos/*")[0])
content= QMediaContent(url)
self.player.setMedia(content)
self.player.setVideoOutput(self.vw)
self.player.play()
if __name__== "__main__":
parser = argparse.ArgumentParser()
#~ parser.add_argument("-nb","--nobox",action="store_true", help="do not wait for the box connection")
args = parser.parse_args()
app = QApplication(sys.argv)
mainwindow = MainWindow(args)
#~ mainwindow.showFullScreen()
mainwindow.show()
sys.exit(app.exec_())
I tried to paste the minimal code. The thing is, I press the button nothing shows, although I used examples like this one PyQt5 - Can't play video using QVideoWidget to test if playing the video is ok, and these work. It's as if it is not adding the widget to the layout or something. Any idea what might be wrong?
I had to use QGraphicsView to achieve what I wanted, here is a fix:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = VideoPlayer()
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
self.centralwidget.phaseQuit(2)
self.centralwidget.play()
class VideoPlayer(QWidget):
def __init__(self, parent=None):
super(VideoPlayer, self).__init__(parent)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.videoItem = QGraphicsVideoItem()
self.videoItem.setSize(QSizeF(640, 480))
scene = QGraphicsScene(self)
graphicsView = QGraphicsView(scene)
scene.addItem(self.videoItem)
layout = QVBoxLayout()
layout.addWidget(graphicsView)
self.setLayout(layout)
self.mediaPlayer.setVideoOutput(self.videoItem)
self.counter = 0
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
pass
else:
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(glob.glob("videos/*")[self.counter])))
self.mediaPlayer.play()
self.counter += 1
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = MainWindow()
player.show()
sys.exit(app.exec_())

How to change ipython qtconsole input

I'm making a guide with pyqt and I'm including an ipython qtconsole widget.
try:
from qtconsole.rich_jupyter_widget import RichJupyterWidget as ipythonWidget
from qtconsole.inprocess import QtInProcessKernelManager
except:
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget as ipythonWidget
from IPython.qt.inprocess import QtInProcessKernelManager
I want to modify the qtconsole input from my code but is not working. I've tried the set_next_input function but it doesn't work and I can't find another function I can use to acomplish what I want. Is even possible to achieve what I want? and if so, how can I do it?
Here is my code:
try:
from qtconsole.rich_jupyter_widget import RichJupyterWidget as ipythonWidget
from qtconsole.inprocess import QtInProcessKernelManager
except:
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget as ipythonWidget
from IPython.qt.inprocess import QtInProcessKernelManager
import sys
from PyQt4 import QtGui
class sympyIpython(QtGui.QWidget):
def __init__(self):
super().__init__()
self.ipython = IpythonWidget()
v = QtGui.QVBoxLayout(self)
button = QtGui.QPushButton('append to input')
v.addWidget(self.ipython)
v.addWidget(button)
button.clicked.connect(self.symClicked)
def symClicked(self):
self.ipython.kernel.shell.set_next_input(' appended text')
class IpythonWidget(ipythonWidget):
def __init__(self):
super().__init__()
self.kernel_manager = QtInProcessKernelManager()
self.kernel_manager.start_kernel()
self.kernel = self.kernel_manager.kernel
self.kernel.gui = 'qt4'
self.kernel_client = self.kernel_manager.client()
self.kernel_client.start_channels()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
m = sympyIpython()
m.show()
sys.exit(app.exec_())
Reposting as an answer:
To change the text at the prompt in the Qt console, set input_buffer on the widget object:
jupyter_widget.input_buffer = 'text'

Access untranslated string in PyQt?

I am writing a little pyqt application. Now I have start using Qtranslator with *.ts and *.qm files to get a Swedish translation.
I use e.g. self.tr(“Test this”) “Swedish Testar detta”. I am wondering while using the Swedish translation, if it is possible to get back the original untranslated string, in this case “Test this” in the program?
This i my little program and I want the "translate_string" method to print the original string.
from PyQt5.QtWidgets import (QApplication, QDialog, QPushButton, QVBoxLayout)
import sys
from PyQt5.QtCore import (QTranslator)
class Form(QDialog):
def __init__(self):
super(Form, self).__init__()
button = QPushButton(self.tr("&Close"))
self.test_lang = QPushButton(self.tr("Translate"))
self.string = self.tr("Test this")
layout = QVBoxLayout()
layout.addWidget(self.test_lang)
layout.addWidget(button)
self.setLayout(layout)
self.test_lang.clicked.connect(self.translate_string)
button.clicked.connect(self.close)
def translate_string(self):
print(self.string)
if __name__ == '__main__':
app = QApplication(sys.argv)
translator = QTranslator()
translator.load("test_trans_sv_SE.qm")
app.installTranslator(translator)
form = Form()
form.show()
sys.exit(app.exec_())