Unsolved 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-Enthusiast Not a Qt question, but Python
isinstance(obj, QLineEdit)
See https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python
-
@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++" -
@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 goif 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 ifobj
has some method namedSomeBrowserFunction
on it, but it may not be aQTextBrowser
instance, just something which happens to have a function of that name, which might do anything. Which may or may not bother you... :) -
@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 anif
test. -
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.