Here is my code, How to show My Labels and activate respective QShortcut?. Want to show my labels(both instances) and assign respective shortcut keys to labels and activate it.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Create_workingDict(QWidget):
def __init__(self,lblname,lblscut):
super(). __init__()
self.lblname = lblname
self.lblscut = lblscut
lbl_1 = QLabel()
lbl_1.setText(self.lblname)
lbl_2 = QLabel()
lbl_2.setText(self.lblscut)
vbox = QVBoxLayout()
vbox.addWidget(lbl_1)
vbox.addWidget(lbl_2)
self.setLayout(vbox)
self.msgSc = QShortcut(QKeySequence(f'{self.lblscut}'), self)
self.msgSc.activated.connect(lambda: QMessageBox.information(self,'Message', 'Ctrl + M initiated'))
class Mainclass(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Sample Window")
x = Create_workingDict("Accounts","Alt+A")
y = Create_workingDict("Inventory", "Ctrl+B")
def main():
app = QApplication(sys.argv)
ex = Mainclass()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Shortcuts work based on the context (and visibility) of the parent widget: as long as the parent is visible and the context is compatible, they will be triggered, otherwise they will not even be considered.
Be aware that the visibility is of the parent widgets (note the plural) is mandatory, no matter of the context: Qt has no API for a "global" shortcut, not only external to the application, but also within it: there is no shortcut that can automatically work anywhere in the app if (any of) its parent(s) is hidden. The context only ensures that the shortcut can only be activated if the active widget is part of the application (ApplicationShortcut), if the current active window is an ancestor of the shortcut parent (WindowShortcut), if any of the grand[...]parent widgets has focus (WidgetWithChildrenShortcut) or the current parent has it (WidgetShortcut).
Long story short: if the shortcut's parent is not visible (at any level), it will not be triggered.
Not only. In your code, both x and y are potentially garbage collected (they are not due to the fact that the lambda scope avoids destruction, but that's just "sheer dumb luck"), so that code would be actually prone to fail anyway if the activated signal would have been connected to an instance method.
If you want them to be available to the visible window, you must add their parent widgets to that window, even if they're not shown. Otherwise, just add the shortcuts to that window.
For instance:
class Mainclass(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Sample Window")
x = Create_workingDict("Accounts","Alt+A")
y = Create_workingDict("Inventory", "Ctrl+B")
layout = QVBoxLayout(self)
layout.addWidget(x)
layout.addWidget(y)
The only and automatic way to access a global application shortcut from any window of the application is to create a QKeySequence that is checked within an event filter installed on the application.
This is a possible, but crude implementation, so, take it as it is and consider its consequences:
class ShortCutFilter(QObject):
triggered = pyqtSignal(QKeySequence)
def __init__(self, shortcuts=None):
super().__init__()
self.shortcuts = {}
def addShortcut(self, shortcut, slot=None):
if isinstance(shortcut, str):
shortcut = QKeySequence(shortcut)
slots = self.shortcuts.get(shortcut)
if not slots:
self.shortcuts[shortcut] = slots = []
if slot is not None:
slots.append(slot)
return shortcut
def eventFilter(self, obj, event):
if event.type() == event.KeyPress:
keyCode = event.key()
mods = event.modifiers()
if mods & Qt.ShiftModifier:
keyCode += Qt.SHIFT
if mods & Qt.MetaModifier:
keyCode += Qt.META
if mods & Qt.ControlModifier:
keyCode += Qt.CTRL
if mods & Qt.ALT:
keyCode += Qt.ALT
for sc, slots in self.shortcuts.items():
if sc == QKeySequence(keyCode):
self.triggered.emit(sc)
for slot in slots:
try:
slot()
except Exception as e:
print(type(e), e)
return True
return super().eventFilter(obj, event)
def main():
app = QApplication(sys.argv)
shortcutFilter = ShortCutFilter()
app.installEventFilter(shortcutFilter)
shortcutFilter.addShortcut('alt+b', lambda:
QMessageBox.information(None, 'Hello', 'How are you'))
shortcutFilter.triggered.connect(lambda sc:
print('triggered', sc.toString())
ex = Mainclass()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This, obviously, means that any key press event will pass through the known python bottleneck. A better solution would be to create global QActions and addAction() to any possible top level window that could accept it.
While this approach might seem more complex, it has its benefits; for instance, you have more control on the context of the shortcut: in the case above, you could trigger Alt+B from any window, including the one shown after previously triggering it, which is clearly not a good thing.
Add the layout in main widget.
Then pass the layout to the function where you are creating labels and add them to layout.
Below is the modified code.
Find the details as comments in below code.
Your main window class
class Mainclass(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Sample Window")
#CREATE THE LAYOUT HERE IN MAIN WINDOW
vbox = QVBoxLayout()
x = Create_workingDict("Accounts","Alt+A",vbox) #PASS THE LAYOUT OBJECT (vbox) AS AN ARGUMENT
y = Create_workingDict("Inventory", "Ctrl+B",vbox) #PASS THE LAYOUT OBJECT (vbox) AS AN ARGUMENT
#SET THE LAYOUT TO MAIN WINDOW
self.setLayout(vbox)
Your function where labels created.
Observe that the functions vbox = QVBoxLayout() and self.setLayout(vbox) are moved out of this function to main window.
class Create_workingDict(QWidget):
def __init__(self,lblname,lblscut,vbox): #RECEIVE LAYOUT (vbox) ARGUMENT
super(). __init__()
self.lblname = lblname
self.lblscut = lblscut
lbl_1 = QLabel()
lbl_1.setText(self.lblname)
lbl_2 = QLabel()
lbl_2.setText(self.lblscut)
vbox.addWidget(lbl_1)
vbox.addWidget(lbl_2)
self.msgSc = QShortcut(QKeySequence(f'{self.lblscut}'), self)
self.msgSc.activated.connect(lambda: QMessageBox.information(self,'Message', 'Ctrl + M initiated'))
Related
In QMdiArea, we can select (point) to the activate subwindow. In my application, I want to select multiple subwindows (maybe using the "Ctrl" button) and set them as active windows (>=2 subwindows) and create a list pointer for them. I am trying to get pointers for more than one subwindow at the same time. Yes, activeSubWindow() gives only one window. But I wonder if I can use somthing like the "Ctrl" button in keyboard to select two subwindows and print the pointers to these subwindows. The idea is to get the widgets inside each subwindow (e.g TextEditor) at the same time to do afterward tasks, e.g., comparison
from PyQt5.QtWidgets import QApplication, QMainWindow, QMdiArea, QAction, QMdiSubWindow, QTextEdit
import sys
class MDIWindow(QMainWindow):
count = 0
def __init__(self):
super().__init__()
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.addAction("cascade")
file.addAction("Tiled")
file.addAction("selected_subwindows")
file.triggered[QAction].connect(self.WindowTrig)
self.setWindowTitle("MDI Application")
def WindowTrig(self, p):
if p.text() == "New":
MDIWindow.count = MDIWindow.count + 1
sub = QMdiSubWindow()
sub.setWidget(QTextEdit())
sub.setWindowTitle("Sub Window" + str(MDIWindow.count))
self.mdi.addSubWindow(sub)
sub.show()
if p.text() == "cascade":
self.mdi.cascadeSubWindows()
if p.text() == "Tiled":
self.mdi.tileSubWindows()
if p.text()=="selected_subwindows":
"""I want to select multiple subwindows and and set as activate
windows with the "Ctrl" button and return a points fot all active windows"""
print("active windows: ", self.mdi.activeSubWindow())
app = QApplication(sys.argv)
mdi =MDIWindow()
mdi.show()
app.exec_()
Just like with normal window handling, it's not possible to have multiple active sub windows even in an MDI area.
In order to achieve a "multiple selection" system, you need to track the activation state of the sub windows, which can be tricky.
Subwindows can be activated in different ways:
by clicking on their title bar (including any of its buttons);
by clicking on its contained widget;
by programmatically activating it with setActiveSubWindow() (which is similar to selecting a normal window from the task bar);
While Qt provides the aboutToActivate signal, it's not always reliable: it is always emitted even when the top level window gets focus, so there's no direct way to know the reason of the activation.
The same also goes for the windowStateChanged signal (which is emitted after the state has changed).
For your situation, the best approach is mainly based on the mousePressEvent implementation of the subwindow, but also considering the window state changes, because you need to keep track of the current active windows whenever the activation is changed in any other way (by clicking on the widget or by using setActiveSubWindow().
Since mouse events are handled after the window activation is changed, the proper solution is to create a signal for which the emission will be delayed (scheduled), in order to know if the activation was actually achieved by a mouse button press on the subwindow (not on the child widget) and finally check if the Ctrl key was pressed in the meantime.
Please note that the following code is very basic, and you might need to do some adjustments. For instance, it doesn't consider activations for minimized windows (unlike normal windows, a subwindow could be active even if it's minimized), nor considers activations when clicking on any of the window buttons.
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class SubWindow(QMdiSubWindow):
activated = pyqtSignal(object, bool)
ctrlPressed = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setAttribute(Qt.WA_DeleteOnClose)
self.windowStateChanged.connect(self.delayActivated)
self.activatedTimer = QTimer(
singleShot=True, interval=1, timeout=self.emitActivated)
def delayActivated(self, oldState, newState):
# Activation could also be triggered for a previously inactive top
# level window, but the Ctrl key might still be handled by the child
# widget, so we should always assume that the key was not pressed; if
# the activation is done through a mouse press event on the subwindow
# then the variable will be properly set there.
# Also, if the window becomes inactive due to programmatic calls but
# *after* a mouse press event, the variable has to be reset anyway.
self.ctrlPressed = False
if newState & Qt.WindowActive:
self.activatedTimer.start()
elif not newState and self.activatedTimer.isActive():
self.activatedTimer.stop()
def emitActivated(self):
self.activated.emit(self, self.ctrlPressed)
self.ctrlPressed = False
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.ctrlPressed = event.modifiers() & Qt.ControlModifier
self.activatedTimer.start()
super().mousePressEvent(event)
class MDIWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("MDI Application")
self.activeWindows = []
activeContainer = QWidget()
activeLayout = QVBoxLayout(activeContainer)
activeLayout.setContentsMargins(0, 0, 0, 0)
self.activeList = QListWidget()
# Note: the following "monkey patch" is only for educational purposes
# and done in order to keep the code short, you should *not* normally
# do this unless you really know what you're doing.
self.activeList.sizeHint = lambda: QSize(150, 256)
activeLayout.addWidget(self.activeList)
self.compareBtn = QPushButton('Compare', enabled=False)
activeLayout.addWidget(self.compareBtn)
self.activeDock = QDockWidget('Selected windows')
self.activeDock.setWidget(activeContainer)
self.addDockWidget(Qt.LeftDockWidgetArea, self.activeDock)
self.activeDock.setFeatures(self.activeDock.NoDockWidgetFeatures)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
fileMenu = bar.addMenu("File")
self.newAction = fileMenu.addAction("New")
self.cascadeAction = fileMenu.addAction("Cascade")
self.tileAction = fileMenu.addAction("Tiled")
self.compareAction = fileMenu.addAction("Compare subwindows")
fileMenu.triggered.connect(self.menuTrigger)
self.compareBtn.clicked.connect(self.compare)
def menuTrigger(self, action):
if action == self.newAction:
windowList = self.mdi.subWindowList()
if windowList:
count = windowList[-1].index + 1
else:
count = 1
sub = SubWindow()
sub.index = count
sub.setWidget(QTextEdit())
sub.setWindowTitle("Sub Window " + str(count))
self.mdi.addSubWindow(sub)
sub.show()
sub.activated.connect(self.windowActivated)
elif action == self.cascadeAction:
self.mdi.cascadeSubWindows()
elif action == self.tileAction:
self.mdi.tileSubWindows()
elif action == self.compareAction:
self.compare()
def windowActivated(self, win, ctrlPressed):
if not ctrlPressed:
self.activeWindows.clear()
if win in self.activeWindows:
self.activeWindows.remove(win)
self.activeWindows.append(win)
self.activeList.clear()
self.activeList.addItems([w.windowTitle() for w in self.activeWindows])
valid = len(self.activeWindows) >= 2
self.compareBtn.setEnabled(valid)
self.compareAction.setEnabled(valid)
def compare(self):
editors = [w.widget() for w in self.activeWindows]
if len(editors) < 2:
return
it = iter(editors)
oldEditor = next(it)
while True:
try:
editor = next(it)
except:
msg = 'Documents are equal!'
break
if oldEditor.toPlainText() != editor.toPlainText():
msg = 'Documents do not match!'
break
oldEditor = editor
QMessageBox.information(self, 'Comparison result', msg, QMessageBox.Ok)
app = QApplication(sys.argv)
mdi = MDIWindow()
mdi.show()
app.exec_()
Note that I had to make some further changes to your code:
action checking should never be done by string comparison: the style or localization could potentially add mnemonics or text variations to action texts, and you'll never get your action triggered: create proper instance attributes and verify the action by object comparison instead.
the count must be an instance attribute, not a class one: if, for any reason, you have to create multiple instances of the main window, you'll get an inconsistent count; you should also consider the currently existing windows;
you should not specify signal overloads if there are no overloads at all (which is the case of QMenu.triggered) nor create local variables if they are being used only once (and their names are not that long, like self.menuBar());
In QMidArea how to open a SubWindow? My Programs as follows. My intention to open/attach my second program in SubWindow. But Nothing Will happen. visible Only blank Window. How to resolve it?
How to attach my file in QMdi Sub-window ? and after my work, how to close the sub-window properly?
Main Programme
import sys,os
from PyQt5.QtWidgets import *
from sample_countrypage import Countrypage
class MainPage(QMainWindow):
def __init__(self):
super().__init__()
self.mdi = QMdiArea()
self.setWindowTitle(" Sample Programme")
self.setGeometry(100,100,1600,600)
self.Ui()
self.show()
def Ui(self):
self.btn1=QPushButton("Country")
self.btn1.setFixedSize(100, 30)
self.btn1.clicked.connect(self.countrypage)
self.left_layout = QVBoxLayout()
self.main_layout = QHBoxLayout()
self.left_layout.setContentsMargins(3,5,5,3)
self.left_layout.addWidget(self.btn1)
self.left_layout.addStretch()
self.main_layout.setSpacing(5)
self.main_layout.setContentsMargins(0,0,0,0)
self.main_layout.addLayout(self.left_layout)
self.main_layout.addStretch()
self.setLayout(self.main_layout)
widget = QWidget()
widget.setLayout(self.main_layout)
self.setCentralWidget(widget)
def countrypage(self):
print("country page")
self.countrywindow = Countrypage()
subwindow = QMdiSubWindow()
subwindow.setWidget(self.countrywindow)
self.mdi.addSubWindow(subwindow)
# subwindow.setFixedSize(500,500)
subwindow.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainPage()
app.setStyle("fusion")
mainwindow.show()
sys.exit(app.exec_())
Second Program
import sys,os
from PyQt5.QtWidgets import *
class Countrypage(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Country Page")
self.btn1 = QPushButton("Accept")
self.btn1.clicked.connect(self.result)
self.btn2 = QPushButton("Re Enter")
self.form_layout = QFormLayout()
self.form_layout.addRow("Country",QLineEdit())
self.form_layout.addRow("continent",QLineEdit())
self.layout_btn = QHBoxLayout()
self.layout_btn.addStretch()
self.layout_btn.addWidget(self.btn1)
self.layout_btn.addWidget(self.btn2)
self.layout_country = QVBoxLayout()
self.layout_country.addLayout(self.form_layout)
self.layout_country.addLayout(self.layout_btn)
self.layout_country.addStretch()
self.setLayout(self.layout_country)
def result(self):
print("bye")
exec .close()
if __name__=="__main__":
app = QApplication(sys.argv)
countrywin = Countrypage()
countrywin.show()
sys.exit(app.exec_())
First of all, there are two main issues with your code:
You never added the mdi area to the main layout (and you also tried to set the layout for the QMainWindow, which is forbidden);
exec is a python builtin, and has no close attribute; if you want to close the widget, you have to call self.close();
Then, the setWidget() method of QMdiSubWindow reparents the widget:
QMdiSubWindow takes temporary ownership of widget;
This means that if you want to close the sub window that contains the widget from that widget, you have to check the parent and eventually close it, as soon as you verify that it's an instance of QMdiSubWindow.
class Countrypage(QWidget):
# ...
def result(self):
print("bye")
# ensure that the parent is actually a subwindow
if isinstance(self.parent(), QMdiSubWindow):
self.parent().close()
else:
self.close()
Alternatively, you can use a custom signal and connect that when creating the subwindow.
class Countrypage(QWidget):
closeRequested = pyqtSignal()
# ...
def result(self):
print("bye")
self.closeRequested.emit()
class MainPage(QMainWindow):
# ...
def countrypage(self):
print("country page")
self.countrywindow = Countrypage()
subwindow = QMdiSubWindow()
subwindow.setWidget(self.countrywindow)
self.mdi.addSubWindow(subwindow)
subwindow.show()
self.countrywindow.closerequested.connect(subwindow.close)
If you want to close the active subwindow from the mdi area (or outside of it) and no matter what that sub window is, just call self.mdi.closeActiveSubWindow().
Note that if you're going to create multiple Countrypage instances, there's no point in creating an instance attribute (self.countrywindow) as it will always be overwritten as soon as another instance will be created. Adding the widget to the subwindow and that subwindow to the mdi area will automatically create a persistent reference (due to the parenting); if you need a python reference to existing pages, then create a list as an instance member in the __init__ (eg. self.pages = []) and add the new instances to that list.
I wish to have the QPushButton do different things on different clicks. One the first click it should execute one command and on the next click, it should execute the other command. I've tried to make a program to do it but it only executes one command, not the other
my code I:
import PyQt5.QtWidgets as pyqt
import sys
ongoing = False
class Stuff(pyqt.QWidget):
def __init__(self):
super().__init__()
self.windows()
def windows(self):
w = pyqt.QWidget()
layout = pyqt.QGridLayout()
self.setLayout(layout)
button = pyqt.QPushButton('click me', w)
layout.addWidget(button)
if not ongoing:
button.clicked.connect(click_one)
else:
button.clicked.connect(click_two)
self.show()
w.show()
def click_one():
global ongoing
print('one')
ongoing = not ongoing
def click_two():
global ongoing
print('two')
ongoing = not ongoing
if __name__ == '__main__':
app = pyqt.QApplication(sys.argv)
x = Stuff()
app.exec_()
What should I do to fix this?
Since the value of ongoing is False when the class is initialized, the button's clicked signal gets connected to click_one(). Connect the button to an initial slot and then call the desired function based on the value of ongoing.
class Stuff(pyqt.QWidget):
def __init__(self):
super().__init__()
self.windows()
def windows(self):
w = pyqt.QWidget()
layout = pyqt.QGridLayout()
self.setLayout(layout)
button = pyqt.QPushButton('click me', w)
layout.addWidget(button)
button.clicked.connect(on_click)
self.show()
w.show()
def on_click():
global ongoing
if not ongoing:
click_one()
else:
click_two()
I suggest rewriting the code with the functions and ongoing variable belonging to the class. The QWidget assigned to variable w seems redundant because the QPushButton is then added to the layout of the class, so its parent gets changed anyways.
class Stuff(pyqt.QWidget):
def __init__(self):
super().__init__()
self.ongoing = False
self.windows()
def windows(self):
layout = pyqt.QGridLayout(self)
button = pyqt.QPushButton('click me')
layout.addWidget(button)
button.clicked.connect(self.on_click)
self.show()
def on_click(self):
self.click_one() if not self.ongoing else self.click_two()
self.ongoing = not self.ongoing
def click_one(self):
print('one')
def click_two(self):
print('two')
Also you might be interested in using a checkable button.
I'm doing an app with in GUI written with PySide. I set a QMenu on a QPushButton, added several QActions via QMenu.addAction. To further explain these actions to the user I added QToolTip's to these with QAction.setToolTip.
When I run the GUI now my QToolTip won't show. The example posted below reproduces the same issue, any ideas?
Thanks in advance
import sys
from PySide import QtGui
class Example(QtGui.QPushButton):
def __init__(self, parent = None):
super(Example, self).__init__(parent)
self.setText('TestMenu')
self.setToolTip('This is a Test Button')
menu = QtGui.QMenu(self)
action_1 = menu.addAction('Action1')
action_1.setToolTip('This is action 1')
action_2 = menu.addAction('Action2')
action_2.setToolTip('This is action 2')
action_3 = menu.addAction('Action3')
action_3.setToolTip('This is action 3')
action_4 = menu.addAction('Action4')
action_4.setToolTip('This is action 4')
self.setMenu(menu)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
app.exec_()
if __name__ == '__main__':
main()
In Qt-5.1 or later, you can simply use QMenu.setToolTipsVisible, and the menu items will show their tooltips as expected (see QTBUG-13663):
menu.setToolTipsVisible(True)
However, for Qt-4.* and Qt-5.0, the situation is different. If an action is added to a toolbar, its tooltip will be shown; but if the same action is added to a QMenu, it won't be, and there is no built-in API to change that. There are a couple of ways to work around this. One is to use status tips instead, which will show the menu-item information in the status-bar. The other is to implement the menu-item tooltip functionality yourself using the QMenu.hovered signal and QToolTip.showText:
self.menu = QtGui.QMenu(self)
...
self.menu.hovered.connect(self.handleMenuHovered)
def handleMenuHovered(self, action):
QtGui.QToolTip.showText(
QtGui.QCursor.pos(), action.toolTip(),
self.menu, self.menu.actionGeometry(action))
Actually you don't have to do any workaround to display your tooltip, since Qt 5.1, you can use QMenu's property toolTipsVisible, which is by default set to false.
See the related Qt suggestion.
With ekhumoro helping me on the way got to this solution. It's probably not the most beautiful thing, and the code below positions the menu and the tool tips somewhat arkward, but in my actual programm it looks quite neat.
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QPushButton):
def __init__(self, parent = None):
super(Example, self).__init__(parent)
self.setText('TestMenu')
self.setToolTip('This is a Test Button')
menu = QtGui.QMenu(self)
action_1 = menu.addAction('Action1')
action_1.setToolTip('This is action 1')
action_2 = menu.addAction('Action2')
action_2.setToolTip('This is action 2')
action_3 = menu.addAction('Action3')
action_3.setToolTip('This is action 3')
action_4 = menu.addAction('Action4')
action_4.setToolTip('This is action 4')
action_1.hovered.connect(lambda pos = [self], parent = action_1, index = 0: show_toolTip(pos, parent, index))
action_2.hovered.connect(lambda pos = [self], parent = action_2, index = 1: show_toolTip(pos, parent, index))
action_3.hovered.connect(lambda pos = [self], parent = action_3, index = 2: show_toolTip(pos, parent, index))
action_4.hovered.connect(lambda pos = [self], parent = action_4, index = 3: show_toolTip(pos, parent, index))
self.setMenu(menu)
self.show()
def show_toolTip(pos, parent, index):
'''
**Parameters**
pos: list
list of all parent widget up to the upmost
parent: PySide.QtGui.QAction
the parent QAction
index: int
place within the QMenu, beginning with zero
'''
position_x = 0
position_y = 0
for widget in pos:
position_x += widget.pos().x()
position_y += widget.pos().y()
point = QtCore.QPoint()
point.setX(position_x)
point.setY(position_y + index * 22) # set y Position of QToolTip
QtGui.QToolTip.showText(point, parent.toolTip())
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
app.exec_()
if __name__ == '__main__':
main()
I have to say I'm not perfectly happy with this, mainly because the show_toolTip function has to be global, for the lambda operator didn't recognize it when I had it in the class (self.show_toolTip). I'm still open for suggesetions if someone has a suggestion.
Instead of showing tooltip immediately one can just update the tooltip of the parent (menu) when hovering and wait for the tooltip to be shown! Therefore:
menu = QtGui.QMenu(self)
action_1 = menu.addAction('Action1')
action_1.setToolTip('This is action 1')
...
menu.hovered.connect(self.handleMenuHovered)
def handleMenuHovered(self, action):
action.parent().setToolTip(action.toolTip())
Is it possible to scale (or "zoom") a QTextEdit area? I believe I read that placing QTextEdit inside QLayout can allow for scaling of the QTextEdit area, though did not find how to implement it. Couple of options...
CTRL + Roll of Mouse Wheel
Running the code below, holding down the CTRL (control) key and rolling the mouse wheel, the event is captured and the text does scale (at least on Windows), however, as the text grows larger the wheel has to move further and further for very much effect, so one goal is to be able to modify that somehow, maybe some math to increase the increments to a greater degree on increases in the plus direction.
(The setReadOnly()'s below are because it would seem textEdit has to be ReadOnly(False) for the mouse event to be captured, then True to be able to scale during roll of the mouse wheel, so it is then set back to original state of False again on release of the CTRL key).
Toolbar Button Click
The other option is toolbar buttons for zoom in and out.
onZoomInClicked() is called.
Some current problems with the code below
1. It prints: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout and I don't have my head wrapped around that yet.
2. QtGui.QTextEdit(self.formLayout) instead of (self) to place the textEdit area inside the layout produces TypeError: 'PySide.QtGui.QTextEdit' called with wrong argument types
3. wheelEvent() could use some way to modify event.delta() maybe?
4. The toolbar button (text only) will currently run its def when clicked, however it only contains a print statement.
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.formLayout = QtGui.QFormLayout(self)
self.textEdit = QtGui.QTextEdit(self)
self.toolBar = QtGui.QToolBar(self)
self.actionZoomIn = QtGui.QAction(self)
self.textEdit.setHtml('<font color=blue>Hello <b>world</b></font>')
self.setCentralWidget(self.textEdit)
self.addToolBar(self.toolBar)
self.toolBar.addAction(self.actionZoomIn)
self.actionZoomIn.setText('Zoom In')
self.actionZoomIn.connect(self.actionZoomIn,
QtCore.SIGNAL('triggered()'), self.onZoomInClicked)
def onZoomInClicked(self):
print "onZoomInClicked(self) needs code"
def wheelEvent(self, event):
print "wheelEvent() captured"
if (event.modifiers() & QtCore.Qt.ControlModifier):
self.textEdit.setReadOnly(True)
event.accept()
def keyReleaseEvent(self, evt):
if evt.key() == QtCore.Qt.Key_Control:
self.textEdit.setReadOnly(False)
if __name__ == '__main__':
app = QtGui.QApplication([])
frame = MainWindow()
frame.show()
app.exec_()
I've been grappling with this for days so would be great to have the more customizable QTextEdit scale/zoom working if it is even possible.
The two error messages can be expained as follows:
The QMainWidget automatically gets a layout, so the QFormLayout is redundant. If you want to add a layout, create a QWidget to be the central widget and make it the parent of the new layout. Other widgets can then be added to that new layout.
The parent of a QWidget subclass must itself be QWidget subclass, which QFormLayout isn't.
I've modified your example so that it does most of what you asked for. Note that QTextEdit.zoomIn and QTextEdit.zoomOut both take a range argument for controlling the degree of zoom.
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.textEdit = Editor(self)
self.toolBar = QtGui.QToolBar(self)
self.actionZoomIn = QtGui.QAction('Zoom In', self)
self.actionZoomOut = QtGui.QAction('Zoom Out', self)
self.textEdit.setHtml('<font color=blue>Hello <b>world</b></font>')
self.setCentralWidget(self.textEdit)
self.addToolBar(self.toolBar)
self.toolBar.addAction(self.actionZoomIn)
self.toolBar.addAction(self.actionZoomOut)
self.actionZoomIn.triggered.connect(self.onZoomInClicked)
self.actionZoomOut.triggered.connect(self.onZoomOutClicked)
def onZoomInClicked(self):
self.textEdit.zoom(+1)
def onZoomOutClicked(self):
self.textEdit.zoom(-1)
class Editor(QtGui.QTextEdit):
def __init__(self, parent=None):
super(Editor, self).__init__(parent)
def zoom(self, delta):
if delta < 0:
self.zoomOut(1)
elif delta > 0:
self.zoomIn(5)
def wheelEvent(self, event):
if (event.modifiers() & QtCore.Qt.ControlModifier):
self.zoom(event.delta())
else:
QtGui.QTextEdit.wheelEvent(self, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()