I am trying to store string data within a QAbstractButton.text().
Why?
I want to display the short name in the text() itself, but be able to call the long name via the text() "comment" through code.
You are able to write "comments" within QT Designer, but I have been unable to replicate this in Python.
Looking at the code in notepad, it appears the "comment" text is created within the text string itself:
<property name="text">
<string extracomment="toast">Select object and click here</string>
What I currently have in python is:
Xsl = cmds.ls(sl=1)[0]
Xbutton.setText(Xsl)
How can I also set and get the comment part of this text?
Any advice would be appreciated!
If you want to add extra data to a widget why not just subclass it and create your own?
class MyCustomButton(QtWidgets.QPushButton):
def __init__(self, parent=None):
super(MyCustomButton, self).__init__(parent)
self.my_variable = None
Now you can continue using MyCustomButton just like a normal button, and also add whatever you like to my_variable.
I have found that every object contains a variable for windowTitle. If this isn't the main window, the window title is generally left blank, therefore I can store data here.
Granted, this probably isn't the cleanest approach, but it'll serve for now.
Green Cell's subclassing is most likely the best way to resolve this issue. However, I am mainly building the UI using Qt Designer, and want to primarily keep any edits within that wrapper.
def store_selected_node_on_button(self):
"""
Changes the text of a given button to store an object's name
As the button isn't a window, I can set the window title to store the long name of the selected object.
:return: None
"""
button = self.sender()
sl = cmds.ls(sl=1, long=True)
if not sl:
button.setText("Select object and click here")
button.setWindowTitle("")
else:
button.setText(sl[0].split("|")[-1])
button.setWindowTitle(sl[0])
return
Related
i'm making a sort of android lock thing on kivy, and to draw the line, I need to get the id of the widget the mouse is on, so I assing an id to each one like this in the .kv file:
ClickableImage:
id: one
source: 'button.png'
etc.
and I know I can get all the ids (I have 9, of course), with the
self.parent.ids.id
or
self.parent.ids['id']
but is there a way to get the ID the mouse is in? or the one I click? I have a hoverable class so it detects when it enters in a Widget, but I don't really know how to get its position, or change its source.
Is there any:
self.parten.ids.current
or something like that?
thanks for the help
You can use collide_widget or collide_point and in the widget set a method that will change a variable in the parent, let's say selected_widget to the current widget's like this:
if self.collide_point(*Window.mouse_pos):
self.parent.selected_widget = self # or its id
Then you can do with it anything. Maybe it'd be even better to put your logic into the widget itself and handle collision directly there. Obviously you'll need to bind a method you create with that if block above to an event such as on_release or on_press so run the method, otherwise it won't do a thing.
You can also get a hoverable behavior from this PR or even from this snippet.
Edit:
Please note that the id will not be available in the widget instance
Which means self.ids.my_id.id == None and therefore to actually get id you need to do this:
def find(self, parent, widget):
for id, obj in parent.ids.items():
if obj == widget:
print id
return id
Ok, this might be a duplicate, but as I couldn't really get anything out of (possibly) similar questions, here is mine: I'm working on a small PyQt4 program where I can enter the name of a song in a QLineEdit and then add a QLabel of it beneath it. I want a button beside each one of these labels that deletes the label when clicked. Relevant code:
def Add(self):
self.rf= QtGui.QLabel(self.le1.text(),self)
self.rf.move(45,30)
self.rf.resize(450,30)
self.rf.show()
self.x = QtGui.QPushButton("X",self)
self.x.move(10,30)
self.x.resize(30,30)
self.x.show()
self.x.clicked.connect(self.Del)
def Del(self):
self.rf.close()
self.x.close()
Now, what I'm not understanding is how I can assign a different instance to each of these dynamically added Qlabels, in order to delete the specific one when the button is clicked.
The best idea I had was creating a variable containing a number that would change with each added QLabel, something like var = rf+str(num) and num = 0, then adding 1 to num for each QLabel and then using getattr for the instances, so getattr(self, var) = Qtgui.QLabel(...), which unfortunately gives me an error that I can't assign that value to the function. And I can't create a dictionary since I have to have different instances for that.
Any ideas would be highly appreciated, thanks a lot.
You could keep them all in a dict and then key that off of the label text. It also provides a quick way to check for duplicates.
CODE: http://pastebin.com/W4uXmazw
I would like to memorize how to get values from any wx widget with event handling after clicking a wx.Button.
In my program i have two fields, the new filename and the contents.
What are the steps i have to take in order to get the values from each field?
From there, i can use pythons f.open and f.write methods to complete my application.
Thanks!
If you want to get value of a widget, then you need to make that widget accessible throughout the entire class. To do that, you need to make the variable for the widget into an instance variable. So instead of adding the text control directly to the sizer, you'll want to do something like this:
self.newfilename = wx.TextCtrl(panel,-1), 0, wx.TOP, 5)
self.contents = wx.TextCtrl(panel,-1,size=(390,150),style = wx.TE_MULTILINE|wx.TE_PROCESS_TAB)
Then in your button's event handler, you can just do something like this:
valueOne = self.newfilename.GetValue()
contents = self.contents.GetValue()
The other way to do it would be to use your panel. If you use "self.panel", then you could grab all its children via its GetChildren method and then iterate over the list and use Python's "isinstance" builtin to check what kind of widget you're accessing. If you have set the widget's name, you can check that too.
I am trying to code the following: Two Columns. One contains a itemId, the other one contains a typeId. I want to render the itemId only when the typeId equals a specific value.
class IDRenderer(gtk.CellRendererText):
def __init__(self):
gtk.CellRendererText.__init__(self)
def do_render(self,window, widget, background_area, cell_area, expose_area, flags):
if ----} Condition to ask for value of the typeId - Cell {-----:
gtk.CellRendererText.do_render(self, window, widget, background_area, cell_area,
expose_area, flags)
gobject.type_register(IDRenderer)
I don't know how to get the iter of the currently rendered row which i need to determine the value of the typeId. Is this even possible?
I now found out, thanks to a nice guy on #pygtk on gimpIRC:
You can do that, with binding so called cell data functions to the corresponding gtk.TreeViewColumn as done here in this example
def renderId(celllayout, cell, model, iter):
if model.get_value(iter,1) == 3:
cell.set_property('visible',True)
else:
cell.set_property('visible',False)
treeviewcolumn = gtk.TreeViewColumn()
renderer = gtk.CellRendererText()
treeviewcolumn.add_attribute(renderer,'text',0)
treeviewcolumn.set_cell_data_func(renderer,renderId)
I ommited some code relevant to render a complete treeview, but i think it shows what i wanted to do and how to do it.
The column renderes the value in the first column (0) of the model only if the value in the second modelcolumn (1) equals 3
I hope this could help someone some time.
It's not possible as far as I know. You need to use properties of the custom renderer which will be set automatically by the code calling the rendering function. (Like the text property of CellRendererText -- the rendering code doesn't get the text from the tree model, but the tree model sets the text property of the renderer before calling the rendering code.)
I've been stumped for more than an hour on how to pass a variable, specifically an ID in sqlite which was recently accessed, to be used to another UI generated. I'm using eric4 (with python, pyqt, qtdesigner and sqlite).
Basically the program I'm coding makes members and each member has a unique ID when the information of the member is generated. When there is a new member made, the ID assigned to the new member must be passed to another part of the program. But there are also instances where there must be modifications made in the member, the ID must be known to display the right information of the member.
Basically, when a new member is added, it first inputs the the information in the database. What I did is like this in the dialog code of that ui named newmember.py:
def on_button_Save_released(self):
Nik = unicode(self.LineEdit_Nickname.text())
self.NMem = NewMem()
self.NMem.input_data(Nik)
self.close()
The NewMem is a class in another py file which has access to the database. Parts of the input_data method goes like this:
cur.execute("insert into Members (Nick) values (?)",(Nik))
I added this code so that it will know what ID the new member is assigned::
CurrentID = cur.lastrowid
return CurrentID
So I changed this line self.NMem.input_data(Nik) in the ui dialog code newmember.py into this
ID = self.NMem.input_data(Nik)
so that the ID will be passed.
Now, the dialog code will open another window and I want the returned ID be used to another ui. Basically the whole method in ui dialog code in newmember.py is like this so far:
def on_button_Save_released(self):
Nik = unicode(self.LineEdit_Nickname.text())
self.NMem = NewMem()
ID = self.NMem.input_data(Nik)
self.close()
self.Pts = Points()
self.Pts.show()
The Points() is a class in another ui dialog code which will show in the Points ui the information of the member. But the returned ID information must be passed to the Points() ui so that the right information be displayed. I have a hunch that I would be needing to change some parts in the compiled ui form so that it knows the ID to be displayed but how will I pass it in there compiled ui form?
Any help is very much appreciated bows deeply
Edit: Is there a way in this code -
self.Pts = Points()
self.Pts.show()
be the variable ID be also incorporated and will pass into the ui? Like
self.Pts = Points
self.Pts.show(ID)
that the ID will be going to the ui also?
Edit2: Is there a way how to pass variables in classes of a ui in python just like how it was shown here - http://www.daniweb.com/forums/thread64758.html
I believe using a Qt model with a QDataWidgetMapper is the Qt way of solving this problem. An example here. (QSLTableModel or subclass QAbstractTableModel if you want to program the database communication yourself).
You can also check SQL Widget mapper example in the Qt docs.
Personally I prefer to use a custom model because I've previously stumbled upon deployment problems using Qt's SQL, but your mileage may vary.