I have 2 different QListwidgets which scrolls are synced properly with the code below. The problem is the secondary Qlistwidget (the one that is following the other´s scroll) doesn´t update its items unless i hover over it with the mouse.
I have very little experience with pyqt, so the only thing i tried is using the repaint and update methods (i added both in the code, at the end of the function). Unfortunately nothing really happens.
def move_scrollbar(vs, value):
vs.blockSignals(True)
vs.setValue(value)
vs.blockSignals(False)
self.list1.repaint()
self.list2.repaint()
self.list1.update()
self.list2.update()
vs1 = self.list1.verticalScrollBar()
vs2 = self.list2.verticalScrollBar()
vs1.valueChanged.connect(partial(move_scrollbar, vs2))
vs2.valueChanged.connect(partial(move_scrollbar, vs1))
I expect the view of the secondary or linked QListwidget update the same way to the primary (the one which i actually scroll) does.
This works for me:
self.vs1 = self.list1.verticalScrollBar()
self.vs2 = self.list2.verticalScrollBar()
self.vs1.valueChanged.connect(self.move_scrollbar)
self.vs2.valueChanged.connect(self.move_scrollbar)
def move_scrollbar(self, value):
self.vs1.setValue(value)
self.vs2.setValue(value)
Related
I want to dynamically change the number of sliders on my application window, in dependence of the number of checked items in a QStandardItemModel structure.
My application window has an instance of QVBoxLayout called sliders, which I update when a button is pressed:
first removing all sliders eventually in there:
self.sliders.removeWidget(slider)
And then creating a new set.
The relevant code:
def create_sliders(self):
if len(self.sliders_list):
for sl in self.sliders_list:
self.sliders.removeWidget(sl)
self.sliders_list = []
for index in range(self.model.rowCount()):
if self.model.item(index).checkState():
slid = QSlider(Qt.Horizontal)
self.sliders.addWidget(slid)
self.sliders_list.append(slid)
The principle seems to work, however what happens is weird as the deleted sliders do not really disappear but it is as they were 'disconnected' from the underlying layout.
When created, the sliders keep their position among other elements while I resize the main window.
However, once they've been removed, they occupy a fixed position and can for instance disappear if I reduce the size of the window.
Unfortunately I'm having difficulties in linking images (it says the format is not supported when I try to link from pasteboard), so I hope this description is enough to highlight the issue.
Do I have to remove the sliders using a different procedure?
EDIT
Thanks to #eyllansec for his reply, it condenses a bunch of other replies around the topic, which I wasn't able to find as I did not know the method deleteLater() which is the key to get rid of widgets inside a QLayout.
I am marking it as my chosen (hey, it's the only one and it works, after all!), however I want to propose my own code which also works with minimal changes w.r.t. to what I proposed in the beginning.
The key point here is that I was using the metod QLayout.removeWidget(QWidget) which I was wrongly thinking, it would..er..Remove the widget! But actually what it does is (if I understood it right) remove it from the layout instance.
That is why it was still hanging in my window, although it seemed disconnected
Manual reference
Also, the proposed code is far more general than what I need, as it is a recursion over layout contents, which could in principle be both other QLayout objects or QWidgets (or even Qspacer), and be organized in a hierarchy (i.e., a QWidget QLayout within a QLayout and so on).
check this other answer
From there, the use of recursion and the series of if-then constructs.
My case is much simpler though, as I use this QVLayout instance to just place my QSliders and this will be all. So, for me, I stick to my list for now as I do not like the formalism of QLayout.TakeAt(n) and I don't need it. I was glad that the references I build in a list are absolutely fine to work with.
In the end, this is the slightly changed code that works for me in this case:
def create_sliders(self):
if len(self.sliders_list):
for sl in self.sliders_list:
sl.deleteLater()
self.sliders_list = []
for index in range(self.model.rowCount()):
if self.model.item(index).checkState():
slid = QSlider(Qt.Horizontal)
self.sliders.addWidget(slid)
self.sliders_list.append(slid)
It is not necessary to save the sliders in a list, they can be accessed through the layout where it is contained. I have implemented a function that deletes the elements within a layout. The solution is as follows:
def create_sliders(self):
self.clearLayout(self.sliders)
for index in range(self.model.rowCount()):
if self.model.item(index).checkState():
slid = QSlider(Qt.Horizontal)
self.sliders.addWidget(slid)
def clearLayout(self, layout):
if layout:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget:
widget.deleteLater()
else :
self.clearLayout(item.layout())
layout.removeItem(item)
I'm brand new to Python and I'm trying to make my first program with PyQt4. My problem is basically the following: I have two checkboxes (Plot1 and Plot2), and a "End" push button, inside my class. When I press End, I would like to see only the plots that the user checks, using matplotlib. I'm not being able to do that. My first idea was:
self.endButton.clicked.connect(self.PlotandEnd)
self.plot1Checkbox.clicked.connect(self.Plot1)
self.plot2Checkbox.clicked.conncet(self.Plot2)
def PlotandEnd(self)
plot1=self.Plot1()
pyplot.show(plot1)
plot2=self.Plot2()
pyplot.show(plot2)
def Plot1(self)
plot1=pyplot.pie([1,2,5,3,2])
return plot1
def Plot2(self)
plot2=pyplot.plot([5,3,5,8,2])
return plot2
This doesn't work, of course, because "PlotandEnd" will plot both figures, regardless of the respective checkbox. How can I do what I'm trying to?
Wrap the plot creation in an if statement that looks at the state of the check boxes. For example:
def PlotandEnd(self)
if self.plot1Checkbox.isChecked():
plot1=self.Plot1()
pyplot.show(plot1)
if self.plot2Checkbox.isChecked():
plot2=self.Plot2()
pyplot.show(plot2)
You also don't need the following lines:
self.plot1Checkbox.clicked.connect(self.Plot1)
self.plot2Checkbox.clicked.conncet(self.Plot2)
This does nothing useful at the moment! Qt never uses the return value of your PlotX() methods, and you only want things to happen when you click the End button, not when you click a checkbox. The PlotX() methods are only currently useful for your PlotandEnd() method.
I've a Dialog window with some QLineEdits to insert the data in my software, I Switch from first QLineEdit to the next with TAB key on keyboard
I want the background to change its color to (for example) Yellow and when is focused out (the focus is switched to another) it must go back to White a QLineEdit is Focused,the . for doing this, I inserted a Different StyleSheet in FocusInEvent and FocusOutEvent.
But i have a problem...
The Problem is When i focus on QlineEdit it works (The background change color to yellow) but if i write something and i switch to the next QLineEdit. the TextCursor in the last QlineEdit doesn't disappear and I view two or also more Text Cursors in my window.
*I omit part of source code (Like=>Layout, Database Functions, etc..) because I think they are irrelevant for helping me to fix my problem.
from PyQt4 import QtGui,QtCore;
class AddWindow(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self);
#Surname
self.SurnameLabel=QtGui.QLabel("Surname:",self);
self.SurnameLabel.move(5,20);
self.SurnameBox=QtGui.QLineEdit(self);
self.SurnameBox.move(5,35);
self.SurnameBox.focusInEvent=self.OnSurnameBoxFocusIn;
self.SurnameBox.focusOutEvent=self.OnSurnameBoxFocusOut;
#Name
self.NameLabel=QtGui.QLabel("Name:",self);
self.NameLabel.move(150,20);
self.NameBox=QtGui.QLineEdit(self);
self.NameBox.move(150,35);
self.NameBox.focusInEvent=self.OnNameBoxFocusIn;
self.NameBox.focusOutEvent=self.OnNameBoxFocusOut;
def OnSurnameBoxFocusIn(self,event):
self.SurnameBox.setStyleSheet("QLineEdit {background-color:yellow}");
def OnSurnameBoxFocusOut(self,event):
self.SurnameBox.setStyleSheet("QLineEdit {background-color:white}");
def OnNameBoxFocusIn(self,event):
self.NameBox.setStyleSheet("QLineEdit {background-color:yellow}");
def OnNameBoxFocusOut(self,event):
self.NameBox.setStyleSheet("QLineEdit {background-color:white}");
The problem is your implement some event is important of cursor behavior and your code has been interrupt it. To fix it please get old behavior back them after your code has work successful;
def OnSurnameBoxFocusIn(self,event):
self.SurnameBox.setStyleSheet("QLineEdit {background-color:yellow}");
QtGui.QLineEdit.focusInEvent(self.SurnameBox,event) # <- put back old behavior
def OnSurnameBoxFocusOut(self,event):
self.SurnameBox.setStyleSheet("QLineEdit {background-color:white}");
QtGui.QLineEdit.focusOutEvent(self.SurnameBox,event) # <- put back old behavior
def OnNameBoxFocusIn(self,event):
self.NameBox.setStyleSheet("QLineEdit {background-color:yellow}");
QtGui.QLineEdit.focusInEvent(self.NameBox,event) # <- put back old behavior
def OnNameBoxFocusOut(self,event):
self.NameBox.setStyleSheet("QLineEdit {background-color:white}");
QtGui.QLineEdit.focusOutEvent(self.NameBox,event) # <- put back old behavior
Regards,
I have a window containing multiple QRowWidgets, which are custom widgets defined by me. These QRowWidgets contain QLineEdits and other standard widgets. To show or hide certain parts of a QRowWidget, I overdefined the focusInEvent() methodes of all the widgets within it. It works perfectly, when I click on the QRowWidget, the hidden elements appear.
The weird thing is that the blinking cursor line hovewer doesn't appear in the QLineEdits within the custom widgets. I can select them both by a mouse click or with Tab, and a glow effect indicates that the QLineEdit is selected in it, I can select a text in it, or start typing at any location wherever I clicked, but the cursor never appears and it's quite annoying.
My 1st thought was that it is a bug on Mac, but I have the same experience on SuSe Linux.
I'm using python 2.7 and PyQt4.
This is in the __init__() of the QRowWidget:
for i in self.findChildren(QWidget):
i.focusInEvent = self.focusInEvent
And then this is the own focusInEvent():
def focusInEvent(self, event):
if self.pself.focusedLine:
self.pself.focusedLine.setStyleSheet("color: #666;")
self.pself.focusedLine.desc.hide()
self.pself.focusedLine.closebutton.hide()
self.setStyleSheet("color: #000;")
self.desc.show()
self.closebutton.show()
self.pself.focusedLine = self
I suspect you do not make a call to the original focusInEvent() when you override it. Your function should look something like:
def focusInEvent(self,...):
QParent.focusInEvent(self,...)
# the rest of your code
where QParent is the nearest base class for your widgets is.
Either that, or make sure you call focusInEvent() on your QLineEdit widgets as part of your function.
Given the comments, it sounds like you are dynamically reassigning the focusInEvent function on the insantiatations in your custom widget. I would either make a derived class for each of the widgets you use that just overrides focusInEvent as above, or include a line like
type(self).focusInEvent(self,..)
in you function.
I am trying to adjust the context menu in a QTextEdit. I have succeeded in getting access to and then displaying the default menu with the following code:
class LinkTextBrowser(QTextBrowser):
def contextMenuEvent(self, event):
menu = self.createStandardContextMenu(event.pos())
# do stuff to menu here
menu.popup(event.globalPos())
However, this does not work for location-sensitive clicks. The case in question is the "Copy Link Location" item in a QTextBrowser's right click menu, which is only enabled if you right click on a link, for obvious reasons. I can't get it to ever be enabled. I suspect I am passing the wrong position to createStandardContextMenu, but I can't figure out the correct position to feed it.
I have tried both event.globalPos() and event.pos(), neither of which work. I also looked at the source code for QTextEdit, but didn't get anywhere. What position is it expecting?
Edit: Update: It appears the problem is the scrolling in the TextBrowser; if I scroll to the top of the window and use event.pos() it behaves. I don't have working code yet, but correcting for the scroll is the solution.
(Specifically, I want to disconnect the signal emitted by the Copy Link Location action and connect it to my own function so I can adjust the URL before copying it to the clipboard, allowing me to make links absolute and so forth before copying, and I have no particular desire to re-write the working bits.)
Here is the working transform of the coordinates:
class LinkTextBrowser(QTextBrowser):
def contextMenuEvent(self, event):
self.link_pos = event.pos()
# correct for scrolling
self.link_pos.setX(self.link_pos.x() + self.horizontalScrollBar().value())
self.link_pos.setY(self.link_pos.y() + self.verticalScrollBar().value())
menu = self.createStandardContextMenu(self.link_pos)
# do stuff to menu
menu.popup(event.globalPos())
Try self.mapToGlobal(event.pos()), it should take into account scroll position.
Maybe you can try something like:
QMenu *menu = new QMenu();
menu->addAction(...);
menu->exec(textEdit->mapToGlobal(pos));
It's C++ but I'm sure that you can easy convert it to python.