Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to check if the Cellwidget of QTableWidget is QLineEdit



  • I have

    table = QtGui.QtableWidget(3,2)

    table.setCellWidget(0,0,QtGui.QLabel("Name"))
    table.setCellWidget(0,1,QtGui.QTextBrowserl())

    table.setCellWidget(1,0,QtGui.QLabel("item1))

    table.setCellWidget(1,1,QtGui.QLabel("item1_label))

    table.setCellWidget(2,0,QtGui.QLabel("item2"))
    table.setCellWidget(2,1,QtGui.QLabel("QLineEdit))

    how to check table.cellWidget(0,1) is a QtextBrowser and can be edited


  • Qt Champions 2019


  • Banned

    Curious @Qt-Enthusiast why would you not know what is in table(0, 1) being that you implemented what was put there?


  • Lifetime Qt Champion

    @Denni-0
    Hi
    She means from code since the interface returns the cell widget as QWidget * if asked.
    So the question could also have been "what to use in python when we use qobject_cast<type *>
    in c++"


  • Banned

    @mrjj actually this should make no difference as if you need to reference things like this you ought to have designed a structure that gives you quick and easy look up. If you are actually digging through your design and trying to decipher it then the design should be revisited and a more efficient means should be employed. I state this from experience as well as helping others implement similar designs. K.I.S.S. is always the best approach and doing it this way is by no means Simple. Heck I have even created database driven designs similar to this where the code would not have any idea of what might be contained in the row until it is placed there but in that situation as in this one I have a object that maps this out for quick and easy referencing. Thus making your additional question meaningless since you should not need to use it if your code was in control of creating it.

    So the question still stands and was aimed at:
    @Qt-Enthusiast why would you not know what is in table(0, 1) being that you implemented what was put there?


  • Lifetime Qt Champion

    @Denni-0
    Well a lookup would be great for her also for reading data later.

    Anyway, i realized the answer with isinstance is not complete.

    What would be the proper way to call a QtextBrowser function via widget pointer
    in python ?

    In c++ it goes like

    QTextBrowser * browser= qobject_cast<QTextBrowser *> ( pointer );
    if(browser)
    browser->SomeBrowserFunction()

    so in python ?

    if isinstance(obj, QTextBrowser ) :
    super(obj,self).SomeBrowserFunction()

    or what is the correct way ?

    can you simply try to call it ? ( duck typing )

    obj.SomeBrowserFunction();



  • @mrjj
    You can go

    if isinstance(obj, QTextBrowser):
        obj.SomeBrowserFunction()
    

    You could also test method presence on the object:

    if hasattr(obj, "SomeBrowserFunction"):
        obj.SomeBrowserFunction()
    

    Or, not particularly my taste, but:

    try:
        obj.SomeBrowserFunction()
    except AttributeError:
        pass
    

    There is no "object-type-casting" is Python. As @jsulm said, isinstance(object, type) is what you have, as a boolean test.

    Also, if you don't do the isinstance() test, the second two examples above will work if obj has some method named SomeBrowserFunction on it, but it may not be a QTextBrowser instance, just something which happens to have a function of that name, which might do anything. Which may or may not bother you... :)


  • Lifetime Qt Champion

    @JonB
    Ah, so you dont need to cast base pointer to gain access to derived child methods.
    Thank you.
    and its cool you can check for functions too.

    The try-catch seems a bit overkill but could be handy with multiple calls.



  • @mrjj
    I've edited more, to scare or amuse you, depending on your inclinations :)

    The try ... except is a debate we have had elsewhere before. It is much more common in Python than C++. Some people will prefer to use that over an if test.


  • Lifetime Qt Champion

    Hi,
    @Qt-Enthusiast said in how to check if the Cellwidget of QTableWidget is QLineEdit:

    I have

    table = QtGui.QtableWidget(3,2)

    table.setCellWidget(0,0,QtGui.QLabel("Name"))
    table.setCellWidget(0,1,QtGui.QTextBrowserl())

    table.setCellWidget(1,0,QtGui.QLabel("item1))

    table.setCellWidget(1,1,QtGui.QLabel("item1_label))

    table.setCellWidget(2,0,QtGui.QLabel("item2"))
    table.setCellWidget(2,1,QtGui.QLabel("QLineEdit))

    how to check table.cellWidget(0,1) is a QtextBrowser and can be edited
    Why are you using cell widgets to show text in a QTableWidget ?

    From the looks of it, you should use a QFormLayout to show that stuff.


Log in to reply