#pyqt | Logs for 2019-04-14
Back
[00:09:38] -!- minto has quit [Ping timeout: 250 seconds]
[00:48:58] -!- mandeep has joined #pyqt
[01:36:53] -!- altendky has quit [Quit: Connection closed for inactivity]
[03:23:53] -!- argoneus has quit [Quit: No Ping reply in 180 seconds.]
[03:49:58] -!- argoneus has joined #pyqt
[07:04:59] -!- mintograde has joined #pyqt
[07:44:22] -!- cottoneyejim has joined #pyqt
[09:25:28] -!- frispete has quit [Quit: Konversation terminated!]
[09:30:56] -!- frispete has joined #pyqt
[09:46:50] -!- cottoneyejim has quit [Ping timeout: 250 seconds]
[10:26:24] -!- altendky has joined #pyqt
[11:13:00] -!- cottoneyejim has joined #pyqt
[11:29:23] -!- nilshi has joined #pyqt
[11:30:32] <nilshi> Where do I report bugs?
[11:30:44] <nilshi> PyQt5
[11:30:45] <altendky> nilshi: the maillist
[11:31:24] -!- cottoneyejim has quit [Quit: cottoneyejim]
[11:32:02] <nilshi> thx
[11:32:17] -!- cottoneyejim has joined #pyqt
[11:37:21] -!- nilshi has parted #pyqt
[14:02:00] -!- countfenring has joined #pyqt
[14:06:56] <countfenring> (qt5) I'm trying to set a QAction to call another function (within the same class) and also pass a reference to a widget that is a child of the class. But I can't seem to get the reference right
[14:07:46] <Avaris> countfenring: code?
[14:07:53] <countfenring> https://pastebin.com
[14:08:28] <Avaris> can you use dpaste.de? i can't open pastebin :|
[14:09:28] <countfenring> sure
[14:09:32] <altendky> https://dpaste.de
[14:09:45] <Avaris> thanks :)
[14:09:59] <countfenring> https://dpaste.de
[14:10:54] <Avaris> well, your 'editor' is local and will be deleted
[14:11:01] <Avaris> but you need a function there
[14:11:47] <altendky> countfenring: the connect isn't being passed the method, you are calling the setBold method and then passing the result to connect.
[14:11:52] <Avaris> ...connect(lambda: self.setBold(editor))
[14:12:19] <Avaris> or ...connect(functools.partial(self.setBold, editor=editor))
[14:12:21] <altendky> but why doesn't the class just store the editor?
[14:12:43] <altendky> also, setBold() isn't taking self
[14:12:45] <countfenring> that's what I was trying to do ... I'm definitely not used to how python does garbage collection
[14:12:48] <altendky> nor setEditorFormat
[14:13:55] <Avaris> (oh, yeah, needs 'self')
[14:14:12] <countfenring> with the instance initialization, right?
[14:14:24] <countfenring> i.e. editor = QTextEdit() --> self.editor = QTextEdit()?
[14:14:28] <Avaris> do you have multiple 'editor's?
[14:14:44] <countfenring> not right now, no
[14:15:32] <Avaris> ok, then do `self.editor` and skip the passing of editor
[14:16:12] <Avaris> (also, kinda confused why setBold etc is taking `parent=None`)
[14:16:29] <countfenring> I am too, to be honest
[14:17:15] <countfenring> but if I only pass one parameter, I was getting an error about 1 parameter being expected, but 2 sent
[14:17:27] <altendky> because no self on a method
[14:17:31] <countfenring> I think it was because I didn't initialize 'editor' as a child of the main class?
[14:17:40] <Avaris> right because you forgot self and passed something
[14:17:52] <altendky> countfenring: instance.method() -> Class.method(instance)
[14:18:04] <countfenring> ok, let me try that
[14:18:14] <altendky> so there's an extra hidden implicit argument and you need an explicit parameter (self) for it to go into
[14:18:18] <countfenring> I'm coming from C++, where you almost never use 'self', so this is taking some getting used to :)
[14:18:44] <altendky> countfenring: yeah, and when reading the code you are left clueless as to whether a variable is a parameter, global, or attribute
[14:18:52] <countfenring> yup
[14:18:57] <countfenring> so to see if I understand
[14:19:11] <Avaris> countfenring: c++ has `this` implicitly, python passes that as first parameter which is called self conventionally
[14:19:46] <countfenring> if I instantiate an object (a widget in this case) without .self, its scope is only the function in which its created. But if I include .self, then it exists as long as the instance of the class exists?
[14:19:52] <altendky> countfenring: maybe go through the official python tutorial. it's supposed to be good at teaching python, rather than programming. so a good choice for people familiar with other languages.
[14:20:27] <altendky> countfenring: if you have a reference to it somewhere you can get to it. whether python gets rid of it or not when you no longer have any references is an implementation detail
[14:20:29] <Avaris> countfenring: objects live as long as something refers to them. any local variable will be deleted after function ends
[14:20:42] <countfenring> alright
[14:20:45] <altendky> variable yes, object... probably?
[14:21:03] <Avaris> s/variable/name
[14:21:26] <altendky> they seem to be interchanged in the docs iirc :|
[14:22:36] <Avaris> yeah, and kinda confusing for c++ people :)
[14:23:01] <altendky> countfenring: coming from c++... yeah obviously the explicit self. also, you rarely actually care about indexes so don't have them (though in pyqt, yeah, the c++ leaks into the python in this regard at times. still, resist) and, everything (on the variable/name/attribute side) is a reference and everything on the 'value' side is an object
[14:24:20] <altendky> countfenring: there's a couple of us here for pyqt stuff but for general python questions, #python is _really_ good
[14:25:29] <countfenring> ok cool, and I may head there ... wasn't sure how pyqt-specific this issue was
[14:26:23] <countfenring> and this may be more general, but does my QAction need to attach to something? Because right now the keyboard shortcut doesn't seem to work
[14:26:35] <Avaris> yup
[14:26:44] <Avaris> somewidget.addAction(...)
[14:27:00] <Avaris> probably self.addAction here
[14:28:10] <countfenring> self rather than adding it to a specific widget?
[14:28:16] <Avaris> which widget you attach it to will determine when it'll activate (when that widget (or children) is focused)
[14:28:16] <countfenring> or would that mess up the scope again?
[14:28:33] <countfenring> hmm
[14:28:59] <countfenring> so it's triggering now, but I'm getting an error that it takes 1 positional argument but 2 were given (using the labmda example y'all gave)
[14:29:48] <Avaris> what's the setBold def?
[14:31:27] <countfenring> self.boldAction.triggered.connect(lambda: self.setBold(self.editor))
[14:31:51] <Avaris> `def setBold...` line?
[14:32:32] <countfenring> def setBold(edit):
[14:32:43] <Avaris> def setBold(self, edit):
[14:33:21] <Avaris> but since you store the editor, you probably don't need to pass it
[14:33:55] <Avaris> def setBold(self): self.editor.setFontWeight(75)
[14:34:08] <Avaris> and `.connect(self.setBold)`
[14:34:56] <countfenring> that's got it!
[14:35:33] <countfenring> hopefully this isn't too broad, but why does the function definition need 'self' as a parameter?
[14:36:07] <Avaris> because python likes explicit
[14:37:06] <countfenring> but if I weren't referencing an object created elsewhere, I could leave it out?
[14:37:13] <Avaris> compared to the "magical" `this` pointer that appears out of thin air :)
[14:37:24] <Avaris> nope
[14:37:48] <Avaris> instance.method(args...) will be passed as method(self, args...)
[14:39:23] <countfenring> what is 'self' actually referencing in that case? The class instance within which the function was written?
[14:39:41] <Avaris> instance that called the method
[14:40:31] <countfenring> ah ok, that makes sense...so it always wants the caller to be stated explicitly?
[14:40:38] <Avaris> as altendky wrote above: `someinstance.somemethod(args...)` is same as `TheClass.somemethod(someinstance, args...)`
[14:41:29] <Avaris> you don't state it when calling, but it is stated explicitly in the receiving end
[14:42:17] <countfenring> oh right, yeah
[14:42:32] <countfenring> so would there ever be a time that you'd define a function without 'self' as the first argument?
[14:43:16] <Avaris> there is `@staticmethod` decorator
[14:43:44] <countfenring> ok, but as a general thing the answer's 'no'?
[14:43:46] <Avaris> it won't receive the instance, but generally you would write regular functions instead
[14:45:49] <Avaris> @staticmethod and @classmethod are exceptions, otherwise you always get self in a method
[14:45:49] <countfenring> so if I had a function whose purpose was, say, to do some math and return an integer, would I still include 'self' in the definition?
[14:46:44] <Avaris> other question first: why is that function attached to a class as opposed to being a regular function?
[14:47:18] <altendky> countfenring: sure you write a _function_ it wouldn't take self. but methods :] do except the caveats above
[14:47:34] <altendky> and @staticmethod is mostly not great
[14:48:14] <countfenring> I gotcha
[14:48:45] <countfenring> and avaris, I think for whatever reason I'm used to compartmentalizing stuff that way. Plus I wanted to see if I could get a better understanding of how scope works (and I have)
[14:49:07] <Avaris> countfenring: use modules for that, not classes :)
[14:50:47] <countfenring> gotcha...yeah, I've got a lot more to play with
[14:53:05] <Avaris> but yes, as with everything there are exceptions. sometimes it's sensible, scope-wise, to have function attached to a class and not need instance.
[14:54:59] <countfenring> I gotcha...my thought was that tying classes to windows in this case, it'd be easier to handle having multiple windows open without things getting crazy
[14:57:42] <Avaris> scope isn't the best word, probably. let's say, logically, a function can belong to a class but not need instance
[14:58:19] <countfenring> so are classes automatically static?
[14:58:28] <Avaris> huh?
[14:59:16] <Avaris> what do you mean by "static"?
[14:59:36] <countfenring> I'm thinking like in C# ... if you define a class as static, you don't need to instantiate it and it basically works as a namespace
[15:01:56] <Avaris> well, kinda
[15:03:20] <Avaris> class itself is an object
[15:11:30] <countfenring> hmm...definitely need to look into this a lot more
[15:11:40] <countfenring> but y'all have been a big help, I really appreciate it
[15:18:45] <altendky> countfenring: classes are a tool for creating objects, not for holding arbitrary stuff in a namespace
[15:21:17] <countfenring> right, that makes sense
[15:23:42] <altendky> countfenring: also be wary that class attributes (assigned in class namespace or as Class.x =) are just globals in the class 'namespace' as opposed to instance attributes (self.x =) that are a specific to that instance.
[15:28:17] <countfenring> ah gotcha...definitely different from C
[15:34:59] <altendky> countfenring: sure. a given instance of a class may or may not have a given attribute at any given point in time. but in general we assign them all in __init__
[16:16:58] -!- cottoneyejim has quit [Quit: cottoneyejim]
[16:54:33] -!- countfenring has quit [Quit: leaving]
[16:55:30] -!- mandeep has quit [Ping timeout: 255 seconds]
[17:11:15] -!- kallesbar has quit [Quit: Konversation terminated!]
[17:22:46] -!- mandeep has joined #pyqt
[18:30:28] -!- mandeep has quit [Remote host closed the connection]
[18:30:48] -!- mandeep has joined #pyqt