Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 07-30-2014, 07:55 AM   #1
jackie_w
Grand Sorcerer
jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.
 
Posts: 6,171
Karma: 16228536
Join Date: Sep 2009
Location: UK
Device: Kobo: KA1, ClaraHD, Forma, Libra2, Clara2E. PocketBook: TouchHD3
Simple PyQt help, please

I'm trying to tidy up the widget creating/layout code in a rather large QDialog which contains many QGroupBox widgets each of which contains a list of buttons. For each groupbox the buttons will all be QPushButton or all QCheckBox.
  1. I've managed to code a working method which successfully creates the groupbox/buttonlist pairs but I need a bit of help. The code currently is something like this:
    Code:
    def create_groupbox(self, lvalues, widget='QPushButton'):
        ... ...
        buttonlist = []
        for val in lvalues:
            ... ...
            if widget == 'QPushButton':
                button = QPushButton()
            elif widget == 'QCheckBox':
               button = QCheckBox()
            button.setText(val)
            ... ...
            buttonlist.append(button)
        ... ...
        return buttonlist, groupbox
    Is there better 1-line code for the red bit?
  2. A follow-up question. If I've previously created a groupbox/buttonlist pair using the above method,
    e.g. self.blist, gbox = self.create_groupbox(['1em', '2em', '3em'])
    and elsewhere in the code I need to know whether self.blist was created using QPushButton or QCheckBox, is there a test I could use on self.blist[0] to answer that question?
jackie_w is offline   Reply With Quote
Old 07-30-2014, 08:11 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,776
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Pass in the actual classes rather than strings, like this

Code:
def create_groupbox(self, lvalues, widget=QPushButton):
   button = widget()
and if you want to test whether something is a push button, do this

Code:
if isinstance(button, QPushButton):
   # is a QPushButton
elif isinstance(button, QCheckBox):
   # is a check box
kovidgoyal is offline   Reply With Quote
Advert
Old 07-30-2014, 08:28 AM   #3
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,703
Karma: 6658935
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by jackie_w View Post
I'm trying to tidy up the widget creating/layout code in a rather large QDialog which contains many QGroupBox widgets each of which contains a list of buttons. For each groupbox the buttons will all be QPushButton or all QCheckBox.
  1. I've managed to code a working method which successfully creates the groupbox/buttonlist pairs but I need a bit of help. The code currently is something like this:
    Code:
    def create_groupbox(self, lvalues, widget='QPushButton'):
        ... ...
        buttonlist = []
        for val in lvalues:
            ... ...
            if widget == 'QPushButton':
                button = QPushButton()
            elif widget == 'QCheckBox':
               button = QCheckBox()
            button.setText(val)
            ... ...
            buttonlist.append(button)
        ... ...
        return buttonlist, groupbox
    Is there better 1-line code for the red bit?
  1. I am not sure about "better", but there is a one-line way to do it.
    Code:
    getattr(getattr(__import__('PyQt4'), 'Qt'), widget)()
    This gets the class object for the class named by 'widget', then instantiates it. Be sure to use the right PyQt for the version of calibre you are running.

    You can of course get the Qt module outside the loop, then use it inside the loop. Something like:
    Code:
    def create_groupbox(self, lvalues, widget='QPushButton'):
        ... ...
        buttonlist = []
        module_ = getattr(__import__('PyQt4'), 'Qt')
        for val in lvalues:
            ... ...
            button = getattr(module_, widget)()
            button.setText(val)
            ... ...
            buttonlist.append(button)
        ... ...
        return buttonlist, groupbox
    EDIT: Kovid's solution is clearly better if there is only one class involved.
    Quote:
  2. A follow-up question. If I've previously created a groupbox/buttonlist pair using the above method,
    e.g. self.blist, gbox = self.create_groupbox(['1em', '2em', '3em'])
    and elsewhere in the code I need to know whether self.blist was created using QPushButton or QCheckBox, is there a test I could use on self.blist[0] to answer that question?
I think you are looking for isinstance, as in
Code:
if isinstance(self.blist[0], QPushButton):

Last edited by chaley; 07-30-2014 at 08:30 AM.
chaley is offline   Reply With Quote
Old 07-30-2014, 08:31 AM   #4
jackie_w
Grand Sorcerer
jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.
 
Posts: 6,171
Karma: 16228536
Join Date: Sep 2009
Location: UK
Device: Kobo: KA1, ClaraHD, Forma, Libra2, Clara2E. PocketBook: TouchHD3
Thank you! Simple once you know
jackie_w is offline   Reply With Quote
Old 08-16-2014, 10:38 AM   #5
dundo
Junior Member
dundo began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Aug 2014
Device: Kindle 4
Quote:
Originally Posted by jackie_w View Post
if widget == 'QPushButton':
button = QPushButton()
elif widget == 'QCheckBox':
button = QCheckBox()

You can also do: button = eval(widget + '()')

dundo is offline   Reply With Quote
Advert
Old 08-16-2014, 11:05 AM   #6
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,776
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Avoid eval(), it is a security risk, while in this case it is probably harmless, if you want a one-liner, do this, instead:

from PyQt5 import Qt as QtGui
button = getattr(QtGui, widget)()
kovidgoyal is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PyQt help re: QWebView widget, please jackie_w Development 2 10-28-2013 10:00 AM
Trio of Picture Books - Simple Animals, Simple Shapes, and You're My Baby! Manley Peterson Self-Promotions by Authors and Publishers 5 01-06-2012 08:55 PM
erm, simple question , hope for simple answer! al zymers Amazon Kindle 5 09-25-2010 01:01 PM
Simple question for a simple mind :) PKFFW OpenInkpot 6 08-27-2009 09:00 PM
PyQT warning... alexxxm Calibre 1 07-28-2008 04:18 AM


All times are GMT -4. The time now is 04:35 PM.


MobileRead.com is a privately owned, operated and funded community.