I can't figure it. I want to be able to swap out QWidgets according to events like button clicks but I am missing something and I haven't been able to search out any example along the lines of the code below. What I want to do is click one of the top buttons and get the widget below to switch between either the QCalendar or QtextEdit. Where am I going wrong?
Thanks!
#!/usr/bin/python
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
widget = QtGui.QCalendarWidget()
button1 = QtGui.QPushButton('Calendar', self)
button1.setCheckable(True)
button1.clicked[bool].connect(self.setWidget)
button2 = QtGui.QPushButton('TextEdit', self)
button2.setCheckable(True)
button2.clicked[bool].connect(self.setWidget)
splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter1.addWidget(button1)
splitter1.addWidget(button2)
splitter1.addWidget(widget)
hbox.addWidget(splitter1)
self.setLayout(hbox)
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle('Switching QWidgets')
self.show()
def setWidget(self, pressed):
source = self.sender()
val1 = QtGui.QCalendarWidget()
val2 = QtGui.QTextEdit()
if source.text() == "Calendar":
widget = val1
QtGui.QWidget.update(Example.hbox)
elif source.text() == "TextEdit":
widget = val2
QtGui.QWidget.update(Example.hbox)
else:
widget = val1
QtGui.QWidget.update(Example.hbox)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You have to use QStackedWidget, where you update the indexes.
class Example(QtGui.QWidget):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
self.stacked = QtGui.QStackedWidget(self)
self.stacked.addWidget(QtGui.QCalendarWidget())
self.stacked.addWidget(QtGui.QTextEdit())
splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
for text in ["Calendar", "TextEdit"]:
btn = QtGui.QPushButton(text, self)
btn.clicked.connect(self.setWidget)
splitter1.addWidget(btn)
splitter1.addWidget(self.stacked)
hbox.addWidget(splitter1)
self.setLayout(hbox)
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle('Switching QWidgets')
self.show()
def setWidget(self):
source = self.sender()
if source.text() == "Calendar":
self.stacked.setCurrentIndex(0)
elif source.text() == "TextEdit":
self.stacked.setCurrentIndex(1)
If you change your code to keep a reference to the calendar and textedit widgets, then add them both to the splitter in init... You can just use the show and hide functions in your setWidget().
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.hbox = QtGui.QHBoxLayout(self)
self.widget = QtGui.QCalendarWidget()
self.widget2 = QtGui.QTextEdit()
button1 = QtGui.QPushButton('Calendar', self)
button1.setCheckable(True)
button1.clicked[bool].connect(self.setWidget)
button2 = QtGui.QPushButton('TextEdit', self)
button2.setCheckable(True)
button2.clicked[bool].connect(self.setWidget)
splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter1.addWidget(button1)
splitter1.addWidget(button2)
splitter1.addWidget(self.widget)
splitter1.addWidget(self.widget2)
self.widget2.hide()
self.hbox.addWidget(splitter1)
self.setLayout(self.hbox)
self.setGeometry(59, 59, 600, 600)
self.setWindowTitle('Switching QWidgets')
self.show()
def setWidget(self, pressed):
source = self.sender()
if source.text() == "Calendar":
self.widget.show()
self.widget2.hide()
elif source.text() == "TextEdit":
self.widget.hide()
self.widget2.show()
Related
I want users of my app to be able to press the button, and then keep typing. With QLineEdit.selectAll(), I am able to select the text entered after Run is pressed, but typing won't do anything. See:
The text is selected due to QLineEdit.selectAll(), but typing won't do anything.
Here's what I have so far:
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.title = 'window title'
self.left = 10
self.top = 10
self.width = 400
self.height = 75
self.initUI()
self.layout = QVBoxLayout()
self.line = QLineEdit()
label = QLabel('Enter a WORD:')
run_button = QPushButton('Run')
self.layout.addWidget(label)
self.layout.addWidget(self.line)
self.layout.addWidget(run_button)
widget = QWidget()
widget.setLayout(self.layout)
run_button.clicked.connect(self.on_click)
self.setCentralWidget(widget)
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
def on_click(self):
response = QLabel(self.line.text())
self.layout.addWidget(response)
self.line.selectAll()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
selectAll() is tricky. It looks like the widget has the focus, but it doesn't. Use setFocus(). Order doesn't matter in this case
self.line.setFocus()
self.line.selectAll()
or
self.line.selectAll()
self.line.setFocus()
Further, in this UI, you can hook up the returnPressed signal of self.line to on_click so that when the user presses enter/return in self.line, the on_click method runs.
self.line.returnPressed.connect(self.on_click)
Putting it all together:
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.left = 10
self.top = 10
self.width = 400
self.height = 75
self.initUI()
self.layout = QVBoxLayout()
self.line = QLineEdit()
label = QLabel('Enter a WORD:')
run_button = QPushButton('Run')
reset_button = QPushButton('Reset History')
self.label = QLabel()
self.layout.addWidget(label)
self.layout.addWidget(self.line)
self.layout.addWidget(run_button)
self.layout.addWidget(reset_button)
self.layout.addWidget(self.label)
widget = QWidget()
widget.setLayout(self.layout)
run_button.clicked.connect(self.on_click)
self.line.returnPressed.connect(self.on_click)
reset_button.clicked.connect(self.reset_click)
self.setCentralWidget(widget)
def initUI(self):
self.setGeometry(self.left, self.top, self.width, self.height)
def on_click(self):
self.label.setText(self.label.text() + '\n' + self.line.text())
self.line.setFocus()
self.line.selectAll()
def reset_click(self):
self.label.setText('')
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
By using this:
self.line.selectAll()
self.line.grabKeyboard()
It will select the text entered at every push of the button, and you will be able to continue typing without clicking in the textbox.
Here in my sample program i want to hide the hbox.but i couldn't find any method to hide hbox in pyqt4.Can any one please help me how to hide the horizontal box.Thank you in advance.
Given below is my code:
import sys
from PyQt4 import QtGui
global payments
payments = False
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.grid = QtGui.QGridLayout()
self.hbox = QtGui.QHBoxLayout()
self.cash = QtGui.QPushButton("cash")
self.card = QtGui.QPushButton("card")
self.wallet = QtGui.QPushButton("wallet")
self.hbox.addWidget(self.cash)
self.hbox.addWidget(self.card)
self.hbox.addWidget(self.wallet)
self.paybtn = QtGui.QPushButton("pay")
self.paybtn.clicked.connect(self.show_payments)
self.grid.addWidget(self.paybtn,1,0)
self.setLayout(self.grid)
self.setGeometry(300, 300, 500,500)
self.show()
def show_payments(self):
global payments
payments = not payments
print payments
if payments:
self.paybtn.setText('Edit Order')
self.grid.addLayout(self.hbox,0,0)
else:
self.paybtn.setText('Pay')
#here i want to hide the self.hbox
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The function of the layouts is to manage the positions and sizes of other widgets, your task is not to hide. Instead you must create a widget where the hbox is with the buttons and that widget set it in the grid layout, so it is only necessary to hide or show the new widget.
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 500,500)
grid = QtGui.QGridLayout(self)
self.foo_widget = QtGui.QWidget(visible=False)
self.foo_widget.setSizePolicy(QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.Maximum)
hbox = QtGui.QHBoxLayout(self.foo_widget)
hbox.setContentsMargins(0, 0, 0, 0)
self.cash = QtGui.QPushButton("cash")
self.card = QtGui.QPushButton("card")
self.wallet = QtGui.QPushButton("wallet")
hbox.addWidget(self.cash)
hbox.addWidget(self.card)
hbox.addWidget(self.wallet)
self.paybtn = QtGui.QPushButton("Pay", clicked=self.show_payments)
grid.addWidget(self.foo_widget, 0, 0)
grid.addWidget(self.paybtn, 1, 0)
def show_payments(self):
global payments
payments = not payments
self.paybtn.setText('Edit Order' if payments else 'Pay')
self.foo_widget.setVisible(payments)
There is a QWidget, which is called with the button "Start" in the main widget.
It shows some text, which should be updated every minute during the work of the QWidget.
How can I make this infinity updating within this code?
class ExampleWidget(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setWindowTitle('Example Widget ScrollArea')
self.initUi()
def initUi(self):
area = QScrollArea(self)
area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QLabel(some_text, self)
area.setWidget(self.scrollAreaWidgetContents)
button = QPushButton("Close")
button.clicked.connect(self.goMainWindow)
layoutV = QVBoxLayout()
layoutV.addWidget(area)
layoutV.addWidget(button)
self.setLayout(layoutV)
def goMainWindow(self):
self.hide()
def sizeHint(self):
return QSize(400, 200)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
start_main_button = QPushButton('Start', self)
start_main_button.move(40, 40)
start_main_button.clicked.connect(self.start)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Test')
def start(self):
global some_text
some_text = 'some text'
self.result_widget = ExampleWidget()
self.result_widget.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
One of my tries:
def start(self):
global some_text
some_text = 'some text'
self.result_widget = ExampleWidget()
self.result_widget.show()
i = 0
while True:
i+=1
some_text+=str(i)
self.result_widget = ExampleWidget()
self.result_widget.show()
Forget the global variables because they are considered bad practice, on the other hand in a GUI you should avoid having loops that consume a lot of time, and in your case a while True blocks the GUI. In Qt if you want to do periodic tasks you must use a QTimer:
from PyQt5 import QtCore, QtWidgets
class ExampleWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__()
self.setWindowTitle('Example Widget ScrollArea')
self.initUi()
def initUi(self):
area = QtWidgets.QScrollArea()
area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtWidgets.QLabel("some_text")
area.setWidget(self.scrollAreaWidgetContents)
button = QtWidgets.QPushButton("Close")
button.clicked.connect(self.hide)
layoutV = QtWidgets.QVBoxLayout(self)
layoutV.addWidget(area)
layoutV.addWidget(button)
def update_text(self, text):
# update the text
self.scrollAreaWidgetContents.setText(text)
def sizeHint(self):
return QtCore.QSize(400, 200)
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
start_main_button = QtWidgets.QPushButton('Start', self)
start_main_button.move(40, 40)
start_main_button.clicked.connect(self.start)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Test')
self.result_widget = ExampleWidget()
self.timer = QtCore.QTimer(self, interval=60*1000)
self.timer.timeout.connect(self.on_timeout)
self.counter = 0
self.initial_text = "some_text"
def on_timeout(self):
# this method will be called every 60 * 1000 ms
self.initial_text += str(self.counter)
self.result_widget.update_text(self.initial_text)
self.counter += 1
#QtCore.pyqtSlot()
def start(self):
self.result_widget.show()
# start timer
self.timer.start()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I have a QT widget that has two list boxes that are wrapped in QFrames along with a label and placed side-by-side on QWidget. I can move between them using the tab key but I would like to move between them using the left and right arrow keys. What is the best way to do this?
My QWidget and ListWidgets look like the following
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
hbox = QHBoxLayout()
hbox.addWidget(Left())
hbox.addWidget(Right())
self.setLayout(hbox)
self.show()
class Left(QFrame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
header = QLabel()
header.setText('Left')
l = QListWidget()
items = ['Item %s' % (i + 1) for i in range(10)]
l.addItems(items)
vbox = QVBoxLayout()
vbox.addWidget(header)
vbox.addWidget(l)
self.setLayout(vbox)
class Right(QFrame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
header = QLabel()
header.setText('Right')
l = QListWidget()
items = ['Item %s' % (i + 1) for i in range(10)]
l.addItems(items)
vbox = QVBoxLayout()
vbox.addWidget(header)
vbox.addWidget(l)
self.setLayout(vbox)
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You can install an event-filter on the list-widgets and then use the focusNextPrevChild method of their parent widget to move the focus:
class MainWindow(QWidget):
...
def init_ui(self):
hbox = QHBoxLayout()
self.left = Left()
self.left.installEventFilter(self)
hbox.addWidget(self.left)
self.right = Right()
self.right.installEventFilter(self)
hbox.addWidget(self.right)
self.setLayout(hbox)
self.show()
def eventFilter(self, source, event):
if (event.type() == QEvent.KeyPress and
(event.key() == Qt.Key_Left or event.key() == Qt.Key_Right) and
event.modifiers() == Qt.NoModifier and
(source is self.left or source is self.right)):
self.focusNextPrevChild(event.key() == Qt.Key_Right)
return True
return super(MainWindow, self).eventFilter(source, event)
I'm trying to extend the QComboClass with a right click menu, and offer it an option to set the current index to -1 (clearing the selection). I'm having trouble invoking the context menu or even the right click event.
class ComboBox(QComboBox):
def __init__(self, *args, **kwargs):
super(ComboBox, self).__init__()
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showMenu)
def showMenu(self, pos):
menu = QMenu()
clear_action = menu.addAction("Clear Selection", self.clearSelection)
action = menu.exec_(self.mapToGlobal(pos))
def clearSelection(self):
self.setCurrentIndex(-1)
Could someone tell me what I'm doing wrong?
can you try this,
def showMenu(self,event):
menu = QMenu()
clear_action = menu.addAction("Clear Selection", self)
action = menu.exec_(self.mapToGlobal(event.pos()))
if action == clear_action:
self.clearSelection()
You can try this
import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QMenu
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lbl = QtGui.QLabel("Ubuntu", self)
self.combo = QtGui.QComboBox(self)
self.combo.setContextMenuPolicy(Qt.CustomContextMenu)
self.combo.customContextMenuRequested.connect(self.showMenu)
self.combo.addItem("Ubuntu")
self.combo.addItem("Mandriva")
self.combo.addItem("Fedora")
self.combo.addItem("Red Hat")
self.combo.addItem("Gentoo")
self.combo.move(50, 50)
self.lbl.move(50, 150)
self.combo.activated[str].connect(self.onActivated)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QComboBox')
self.show()
def showMenu(self,pos):
menu = QMenu()
clear_action = menu.addAction("Clear Selection")
action = menu.exec_(self.mapToGlobal(pos))
if action == clear_action:
self.combo.setCurrentIndex(0)
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()