Disable Rubberband selection in QGraphicsView and only have single item selction - pyside

How can i disable the Rubberband selection in QGraphiscView and only allow users to click select single items as a time in the tool?
Thanks

If I understand correcly, you want to disable the rubber band selection and still be able to left-click for select items (allowing the Ctrl modifier in order to select multiple items, one at the time).
So if this is the case, you need to use the QGraphicsView::setDragMode method and set QGraphicsView::NoDrag option. You can achieve this directly from your QGraphicsView object or subclassing QGraphicsView and adding the call to method on the constructor, like this (PySide):
from PySide.QtGui import *
from PySide.QtCore import *
class MyGraphicsView(QGraphicsView):
def __init__(self, parent = None):
super(MyGraphicsView, self).__init__(parent = parent)
self.setDragMode(QGraphicsView.NoDrag)
If your graphic items have the Qt::ItemIsSelectable flag enabled, then you will still be able to select them as usual.
Hope it helps.

Related

How do I add a small button to a ScatterLayout in Kivy? Label works auto, Button does not

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.scatterlayout import ScatterLayout
class ZoomWidget(ScatterLayout):
def __init__(self, **kwargs):
super(ZoomWidget, self).__init__(**kwargs)
self.add_widget(Label(text='Label'))
def on_touch_down(self, touch):
if touch.is_double_tap:
button = Button(text='Button')
self.add_widget(button)
return True
else:
return super().on_touch_down(touch)
class ZoomSpaceApp(App):
def build(self):
return ZoomWidget()
if __name__ == '__main__':
ZoomSpaceApp().run()
So, the label seems to work, I can right-click (on desktop OS) to create a second touch location, then left dragging scales & rotates the label. However, the button just seems to fill up the whole layout, and I can't scale or rotate it. So how do I get the Button to act as the Label does by default, within a ScatterLayout?
Also, I want to do something similar using other widgets, so the most general answer will do. For example, do I encapsulate the Button in some type of widget such as a layout? What shows up in the ZoomWidget will be other widgets. The point of my application is for the user to create their own apps by touches and decide what to put there (with a context menu at first). Knowing that, is there a better way to approach this?
It doesn't work, because Button works with touches too, therefore it'll most likely eat all your touches and won't let anything for ScatterLayout to work with. To get it work you need to have an area that belongs to ScatterLayout alone a user can work with.
As Label doesn't work with touches as Button does, it passes the touch to the first widget it can use it i.e. in this case the ScatterLayout.
def on_touch_down(self, touch):
if touch.is_double_tap:
button = Button(text='Button', size_hint=(0.9, 0.9))
self.add_widget(button)
return True
else:
return super(ZoomWidget, self).on_touch_down(touch)

Continuous calls of the <Configure> event in tkinter

I am trying to write a function to dynamically resize an image displayed in a tkinter window.
Therefore I bound this function to the Configure event:
connroot.bind( "<Configure>", connresiz)
My problems are:
That the connresiz() function gets called 3 times (why 3?) at program start, and
More troublesome, that dynamically resizing the window calls the function continuously as I drag the mouse! How can avoid this?
I thought about checking at the simultaneous presence of a <Configure> and <ButtonRelease-1> events, but I don't know how to code it.
1) We don't know that, since we can't see your code...
2) Short answer is: you can't, because that's exactly what <Configure> event does! Long answer, you can, with a little trick/hack. Since anytime the window is changing, it will call all the binded functions to <Configure>, and the same happens anytime as the mouse button released (right after the last <Configure> call) we can create a flag/switch which will tell us, if the window was "configured" then we can check that switch anytime the mouse button is released, and switch it back to the default value after we ran some actions.
So if you want the image to resized only, when the mouse was released and the window was changed this is the code you need:
from tkinter import *
class Run:
def __init__(self):
self.root = Tk()
self.clicked = False
self.root.bind('<ButtonRelease-1>', self.image_resize)
self.root.bind('<Configure>', lambda e: self.click(True))
def image_resize(self, event):
if self.clicked:
print("I'm printed after <Configure>.") # the action goes here!
self.click(False)
def click(self, value):
self.clicked = value
app = Run()
app.root.mainloop()
According to the official tk documentation, <Configure> events fire "whenever its size, position, or border width changes, and sometimes when it has changed position in the stacking order." This can happen several times during startup.
It is called continuously while you resize the window because the size of the widget is changing. That's what it's defined to do. You can't prevent it from being called, though you can certainly modify what you do in the callback. For example, you could delay resizing the image until you've not received another <Configure> event for a second or two -- which likely means the user has stopped interactive resizing.

PyQt4: Using a QPushButton widget to delete an item from QList widget

I am learning PyQt4 (I am using version 4.4.4) and I'm pretty new to Python (Python 2.5). I have a GUI with a QListWidget and a QPushButton. I want a user to be able to click to select an entry in the list and then click the QPushButton and have the selected entry go away (be deleted from the QList). I've been banging my head against this problem for over a week and I would deeply appreciate some help.
Currently, my GUI comes up and I can select different list items (only one at a time right now), but when I click the QPushButton, nothing happens. The selection color goes from blue to grey, but the entry is not removed. No error is shown in Command Prompt (Windows 7).
I have defined a function, remove(),which I am using as the slot for the QPushButton. I believe the QPushButton.connect is defined correctly for a Qt Signal to Python Slot, based on what I've seen of answers to similar problems, but the items are not being deleted. However, the remove function is not even being triggered. I have a print statement within the function, but it is not being called when I click the QPushButton, which is how I know that the function is not being called.
Here is my most recent code: (I read a very rant-y post on meta-SO about big blocks of code, so I've cut this down to the bits I think are relevant: the list creation, the button creation and the remove function, which I'm trying to use as a slot. I've left in comments that indicate what the other sections are, so if you think I've left out something that could help, let me know and I'll add it back in)
class questionGUI(QtGui.QWidget):
#This class is the window of the gui.
def __init__(self):
super(questionGUI,self).__init__()
#Layout
grid = QtGui.QGridLayout()
grid.setSpacing(10)
#Labels Needed
...
#Question List
self.qList = QtGui.QListWidget()
#print self.qList
self.qList.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
entries = ['[Pick Image] <Default>','[Slider Question] <Default>', '[Comment Box] <Default>']
for i in entries:
item = QtGui.QListWidgetItem(i)
self.qList.addItem(item)
#Type select
...
#Text insert Needed
...
#Buttons Needed
deleteButton = QtGui.QPushButton('Delete Question')
deleteButton.connect(deleteButton,QtCore.SIGNAL('itemClicked(clicked)'),lambda: self.remove)
addQuestionButton = QtGui.QPushButton('Add Question')
...
doneButton = QtGui.QPushButton('Done')
...
#Parameters Needed
...
#Layout Placement and Window dimensions
...
def addQuestion(self):
...
def remove(self):
print 'remove triggered'
print self.qList.currentItem()
self.qList.removeItemWidget(self.qList.currentItem())
...
I tried to post an image, but I don't have enough reputation. If you think an image would be useful, let me know and I can send it to you.
You mixed the signals:
deleteButton.connect(deleteButton,QtCore.SIGNAL('itemClicked(clicked)'),lambda: self.remove)
deleteButton is a QPushButton, but itemClicked(clicked) looks like the signal from QListWidget with wrong signature. Since, QPushButton doesn't have this signal, no connection is made. Qt doesn't raise errors for failed connections, but .connect method has a bool return value indicating success/failure of the attempted connection.
Also, lambda: self.remove as a slot doesn't make sense. Slot should be a callable that is called upon signal emit. Sure, lambda creates a function, but all you do is reference the method self.remove. lambda will be called, self.remove not. Just self.remove as a slot is enough.
You should use clicked() signal (or clicked(bool), if you care about the checked value) from button:
deleteButton.connect(deleteButton, QtCore.SIGNAL('clicked()'), self.remove)
Edit
Another problem: Your remove method doesn't do what you want. removeItemWidget doesn't remove the item, it removes the widget inside the item (if you set one). It's counterpart to setItemWidget.
You should use takeItem to remove items.
def remove(self):
self.qList.takeItem(self.qList.currentRow())

PyQt graphicsview resize event

I would like to connect a resize event on a graphics view to a function using PyQt. I added the QGraphicsView to the GUI using QtCreator. I have tried a few variations without success:
gv.pyqtConfigure(resize=self.printR)
QtCore.QObject.connect(gv,QtCore.SIGNAL("resized()"),self.printR)
gv.resize.connect(self.printR)
However none of the above work (I have tried using many variations of the event name).
What is the correct way of doing this?
As an expanded question, is there a definitive list anywhere of all the signals available to different widgets in Qt, i.e. all the possible values that could be passed to SIGNAL(), or the "signal" attributes available to a widget (e.g. button.clicked.connect())?
The PyQt class reference is the best place to look for signals and class methods.
Connecting simple signals can be done like this:
self.connect(senderObject, QtCore.SIGNAL("signalName()"), self.slotMethod)
As far as I can tell, however, most widgets do not normally emit a signal when they are resized. In order to do this, you would need to re-implement the QGraphicsView class and its resizeEvent method, modifying it to emit such a signal, like so:
from PyQt4.QtGui import QGraphicsView
from PyQt4.QtCore import SIGNAL
class customGraphicsView(QGraphicsView):
def __init__(self, parent=None):
QGraphicsView.__init__(self, parent)
def resizeEvent(self, evt=None):
self.emit(SIGNAL("resize()"))
Using Qt Designer, you should be able to turn your existing QGraphicsView widget into a placeholder for such a custom widget by right-clicking on it and selecting Promote to.... For help, look here.
I hope this sufficiently answers your question.

What Qt widget(s) to use for read-only, scrollable, collapsible, icon list

I'm relatively new to Qt, and am not entirely familiar with the out-of-the-box widgets. I have a somewhat (but not very) complex widget to create, and don't want to reinvent any wheels. What is the best QWidget to use as a starting point to subclass and/or QWidgets to use to compose my widget. Here is the end-result I am looking for (apologies for the crude drawing):
Key points:
All icons will take up the same size, say 128 x 128. Ignoring the category groupings, they should all align in a nice grid.
The widget should expand to fill all the horizontal and vertical area it can take. Expanding / shrinking horizontally may increase / decrease the number of icons shown in each row.
Icons are grouped, and those groups should be collapsible.
If the height of the widget exceeds its space, a vertical scrollbar should appear.
You're actually looking for some of the more esoteric options for a QListView/QListWidget.
At the top level, add QTreeWidget or QTreeView will give you the hierarchy you're looking for, as well as managing the scrolling area.
Each Listed Item of the (expanded) QTreeXItem will be a QListView/QListWidget, setting setViewMode(QListView::IconMode) on them.
EDIT:
Note that to get the precise look you wanted above, you'll probably have to use QListView and use a custom delegate, handling the drawing yourself (unless you can find a theme that will do exactly what you want). However, I've coded up a short PyQt solution below using the Q*Widget classes because they're shorter, and will still demonstrate how to get the right layout. If you're using C++, the same Qt function calls apply, but obviously you might have more or less bookkeeping.
import sys
from PyQt4 import QtGui, QtCore
class displayItem(QtGui.QWidget): #A simple widget to display, just centers a digit in a 100x100 widget
def __init__(self,num):
QtGui.QWidget.__init__(self)
self.size=100
self.resize(self.size,self.size)
self.setMinimumSize(self.size,self.size)
self.text = num
def paintEvent(self,event):
p = QtGui.QPainter(self)
p.drawText(self.size//2,self.size//2,str(self.text))
app = QtGui.QApplication(sys.argv)
widget = QtGui.QTreeWidget()
widget.setWindowTitle('simple tree')
#Build the list widgets
treeItem1 = QtGui.QTreeWidgetItem(widget)
treeItem1.setText(0,"TreeWidget Parent") #Sets the "header" for your [+] box
list1 = QtGui.QListWidget() #This will contain your icon list
list1.setMovement(QtGui.QListView.Static) #otherwise the icons are draggable
list1.setResizeMode(QtGui.QListView.Adjust) #Redo layout every time we resize
list1.setViewMode(QtGui.QListView.IconMode) #Layout left-to-right, not top-to-bottom
listItem = QtGui.QListWidgetItem(list1)
listItem.setSizeHint(QtCore.QSize(100,100)) #Or else the widget items will overlap (irritating bug)
list1.setItemWidget(listItem,displayItem(1))
listItem = QtGui.QListWidgetItem(list1) #Add a few more items
listItem.setSizeHint(QtCore.QSize(100,100))
list1.setItemWidget(listItem,displayItem(2))
listItem = QtGui.QListWidgetItem(list1)
listItem.setSizeHint(QtCore.QSize(100,100))
list1.setItemWidget(listItem,displayItem(3))
list1.setAutoFillBackground(True) #Required for a widget that will be a QTreeWidgetItem widget
treeSubItem1 = QtGui.QTreeWidgetItem(treeItem1) #Make a subitem to hold our list
widget.setItemWidget(treeSubItem1,0,list1) #Assign this list as a tree item
treeItem2 = QtGui.QTreeWidgetItem(widget) #Make a fake second parent
treeItem2.setText(0,"TreeWidget Parent II")
widget.show() #kick off the app in standard PyQt4 fashion
sys.exit(app.exec_())

Resources