This is driving me a little crazy. Hope someone can clear this up for me. Running the following code results in the first print statement being a list with one element the QVBoxLayout object. I set two objects to layout why do I get only one?
The second print statement gives two objects the QHBoxLayout and QPushButton. Isn't QPushButton a child of layout?
I would expect layout.children() to give me two objects QPushButton and QVBoxLayout
and self.children() to give me one object QHBoxLayout. What am I missing?
from PySide.QtGui import *
import sys
class Main(QWidget):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
layout = QHBoxLayout(self)
layout.addWidget(QPushButton("foo"))
layout.addLayout(QVBoxLayout())
print layout.children()
print self.children()
app = QApplication([])
main = Main()
main.show()
sys.exit(app.exec_())
I guess the note from the documentation explains this clearly enough:
Note: Widgets in a layout are children of the widget on which the
layout is installed, not of the layout itself. Widgets can only have
other widgets as parent, not layouts.
Related
The code below, which is based on an example from zetcode.com, creates a single combo box. There are several issues with the resulting dialog, but the following are especially annoying:
PyQt displays a vertical scrollbar for the combo box, although there is plenty of space to display the entire list of options without a scrollbar.
I've tried to move the combo box to a position near the upper-left corner of the window, but this isn't working.
#!/usr/bin/python
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QComboBox, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.setFixedWidth(400)
self.setFixedHeight(500)
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
self.lbl = QLabel('Animals', self)
self.lbl.setStyleSheet('font-size:11pt')
combo = QComboBox(self)
combo.addItem('bear')
combo.addItem('cat')
combo.addItem('dog')
combo.addItem('dolphin')
combo.addItem('elephant')
combo.addItem('fish')
combo.addItem('frog')
combo.addItem('horse')
combo.addItem('rabbit')
combo.addItem('rat')
combo.addItem('shark')
combo.addItem('snake')
combo.addItem('tiger')
combo.addItem('whale')
combo.activated[str].connect(self.onActivated)
hbox.addWidget(combo)
hbox.setSpacing(20)
hbox.addWidget(self.lbl)
self.setContentsMargins(20, 20, 20, 20)
self.setLayout(hbox)
combo.move(20, 60)
self.setWindowTitle('QComboBox')
self.show()
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
There are two wrong assumptions in the question.
the list of a QComboBox is a popup widget, it doesn't care (nor it should) about the available space the combobox might have: consider it as much as a context menu, which just pops out over the window, possibly going outside its boundaries if it requires more space (and that's just because those boundaries are meaningless to the menu);
the combo has been added to a layout manager, which takes care about resizing and positioning its (managed) child widgets, and that's why you cannot manually "move" them: the layout already sets the geometries automatically on its own everytime the widget is resized (which also happen when it's shown the first time), so any attempt to use move(), resize() or setGeometry() is completely useless;
When adding a widget to a layout, the default behavior is to make it occupy as much space as possible; since a QComboBox is one of those widgets that have a fixed size, the result is that it's actually centered (vertically and horizontally) in the space the layout is "assigning" to it, and this is clearly visible in your case because you set a fixed size for the container widget that is much bigger than what its contents would need.
There are two ways to align those widgets on top:
add the alignment arguments to addWidget:
hbox.addWidget(combo, alignment=QtCore.Qt.AlignTop)
hbox.addWidget(self.lbl, alignment=QtCore.Qt.AlignTop)
note that this won't give you good results in your case, because the label and the combo box have different heights, so the label might look "higher" than the combo;
use a QVBoxLayout layout as main layout for the widget, add the horizontal layout to it and then add a stretch after that (a stretch on a box layout is a "spacer" that tries to occupy as much space as possible)
# ...
mainLayout = QVBoxLayout()
mainLayout.addLayout(hbox)
mainLayout.addStretch()
self.setLayout(mainLayout)
PS: if you need to add lots of (string only) elements to a QComboBox, use addItems() instead of individually adding each of them.
I am trying to identify the object name of a pyqtgraph plotwidget I am mouse wheeling on. However, I can only seem to get the object id "PyQt5.QtWidgets.QWidget object at 0x0000018ED2ED74C8". If I use the QApplication.widgetAt(event.globalPos()).objectName I get nothing, even though I have set the object name. Can you help me?
Sample code:
# Import packages
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout
import pyqtgraph as pg
import sys
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.graphLayout = QHBoxLayout()
self.graph = pg.PlotWidget(name="graph1")
self.graph.setObjectName("graph1")
self.graphLayout.addWidget(self.graph)
self.setLayout(self.graphLayout)
def wheelEvent(self, event):
hoveredWidget = QApplication.widgetAt(event.globalPos())
print(hoveredWidget.objectName())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
A PlotWidget is actually a subclass of QAbstractScrollArea, which is a complex widget that has at least three children widgets: the scroll bars (even when they're hidden) and, most importantly, the viewport, which actually is the "content" of the scroll area.
This means that using widgetAt() you are not getting the plot widget (the scroll area), but its viewport. In fact, in your case you can get the plot widget by checking the parent:
def wheelEvent(self, event):
hoveredWidget = QApplication.widgetAt(event.globalPos())
if hoveredWidget and hoveredWidget.parent():
print(hoveredWidget.parent().objectName())
Be careful when intercepting events from a parent widget, especially for widget as complex as scroll areas: it's not guaranteed that you will receive them, as the children could accept them, preventing further propagation to their parent(s).
If you need more control over them, it's usually better to implement the respective methods in their subclasses or installing an event filter on the instances.
Note that, for the reason above, if you want to filter events on a scroll area you might prefer to install the filter on the viewport:
self.graph.viewport().installEventFilter(self)
I want to show tooltip while not focusing.
I made the code by referring to this PyQt Window Focus
But, it works after click window just one. Works fine, but window always blink at taskbar.
And I think this method is inefficient.
I think it's as if os are not resting while waiting for task to come, but checking every moment for task to come.
This is a simple window window, so it won't use up the cpu much, but I want to code it more efficiently.
Is there any way to improve this?
Or this method right because focusoutEvent excuted only one? ( Cpu resource 0% )
If right, how can I remove blink at taskbar?
I check reference focusPolicy-prop
import sys, os
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.setFocusPolicy(QtCore.Qt.ClickFocus)
self.initUI()
def initUI(self):
vbox = QVBoxLayout()
vbox.addStretch(2)
btn = QPushButton("Test")
btn.setToolTip("This tooltip")
vbox.addWidget(btn)
vbox.addStretch(1)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 200)
self.show()
def focusOutEvent(self, event):
self.setFocus(True)
self.activateWindow()
self.raise_()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
You are having an XY problem: trying to find a solution (usually unorthodox and overly complicated) for a problem that is originated elsewhere.
What you want to do is to show tooltips even if the window is not focused, not to restore the focus of the window; to achieve this you must not reactivate the window when it loses focus (which not only is WRONG, but is both a wrong way and reason for doing so).
You just have to set the WA_AlwaysShowToolTips widget attribute on the top level window (and remove the unnecessary focusOutEvent override, obviously).
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.setFocusPolicy(QtCore.Qt.ClickFocus)
self.initUI()
self.setAttribute(QtCore.Qt.WA_AlwaysShowToolTips, True)
Note that the attribute must be set on a widget that is a top level window, so, unless you're using a QMainWindow or you are absolutely sure that the QWidget will always be a window, it's usually better to do this instead:
self.window().setAttribute(QtCore.Qt.WA_AlwaysShowToolTips, True)
Besides that, the blinking is normal on windows, and has nothing to do with CPU usage:
activateWindow():
[...] On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.
During the study, Qt encountered such a problem. Suppose I have a QWidget on the QMainWindow. How can I make sure that when I resize QMainWindow, QWidget on this QMainWindow do not repaint content until the resizing does not stop.
Yeah, I saw this example How to disable multiple auto-redrawing at resizing widgets in PyQt?
But when I tried this method it's just locked widget content. I just wondered if it was possible to make sure that the contents of the QWidget did not change while we resizing MainWindow. Please tell me, is this possible?
Thanks a lot.
I'm still guessing slightly as to what you want exactly but it sounds as if you essentially want two modes for your paintEvent method -- the normal one that takes care of rendering the widget most of the time and a second, lightweight, mode that can be used whilst the widget is being resized.
If that's the case then you could try something like the following...
#!/usr/bin/python3
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class widget(QWidget):
def __init__(self):
super().__init__()
self.resize_timer = QTimer(self)
self.resize_timer.setSingleShot(True)
self.resize_timer.setInterval(100)
self.resize_timer.timeout.connect(self.delayed_update)
def delayed_update(self):
self.update()
def paintEvent(self, event):
if self.resize_timer.isActive():
print("painting deferred")
# Your `lightweight' rendering goes here and will be used
# while the widget is being resized.
else:
print("painting now")
# Full rendering code goes here.
def resizeEvent(self, event):
super(widget, self).resizeEvent(event)
self.resize_timer.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
f = widget()
f.show()
sys.exit(app.exec_())
Note that it is essentially just a simple modification of the code in the answer you linked to.
I try merge QgsMessageBar with ma ui file
from ui_file import Ui_File
class MyClass(QDialog):
def __init__(self):
QDialog.__init__(self)
self.bar = QgsMessageBar()
self.ui = Ui_File()
self.ui.setupUi(self)
self.ui.pushButton_2.clicked.connect(self.run)
def run(self):
self.bar.pushMessage("Hello", "World", level=QgsMessageBar.INFO)
When I change 'bar' to 'ui' I get error:
'Ui_File' object has no attribute 'pushMessage'
Something is missing in ui file?
How to fix it?
The code in your example looks fine as it is, but you need to add the message-bar to the layout of the dialog.
How you do this will depend on what kind of layout the dialog has, and where you want the message-bar to appear. If the layout is QVBoxLayout, and you want the message-bar at the bottom of the dialog, just do:
self.layout().addWidget(self.bar)
and to put it at the top of the dialog, you would do:
self.layout().insertWidget(0, self.bar)
However, if the layout is a QHBoxLayout or QGridLayout, you will probably have to alter things in Qt Designer to get things to work properly. In particular, QGridLayout has no method for inserting widgets, so you would have to leave a space for the message-bar if you want it at the top of the dialog. You may also need to ensure that the message-bar spans the full width of the dialog. See the docs for QGridLayout.addWidget for more details.
For a QHBoxLayout it's much easier - just put the existing layout inside a QVBoxLayout layout, and proceeed as above.