I'm using PyQt and understand enough OOP to get by comfortably in Python. However, the documentation and useful forum posts are all in C++. I know that the best route would probably be to just re-learn C++. I'm trying but It's taking a long time to sift through tutorials and find the information that I need, mostly because I don't understand enough of the terminology to know where to look.
In a particular forum post there is a section in a method of a class implementation that reads:
void SetTextInteraction(bool on, bool selectAll = false)
{
if(on && textInteractionFlags() == Qt::NoTextInteraction)
{
// switch on editor mode:
setTextInteractionFlags(Qt::TextEditorInteraction);
// manually do what a mouse click would do else:
setFocus(Qt::MouseFocusReason); // this gives the item keyboard focus
setSelected(true); // this ensures that itemChange() gets called when we click out of the item
if(selectAll) // option to select the whole text (e.g. after creation of the TextItem)
{
QTextCursor c = textCursor();
c.select(QTextCursor::Document);
setTextCursor(c);
}
}
else if(!on && textInteractionFlags() == Qt::TextEditorInteraction)
{
// turn off editor mode:
setTextInteractionFlags(Qt::NoTextInteraction);
// deselect text (else it keeps gray shade):
QTextCursor c = this->textCursor();
c.clearSelection();
this->setTextCursor(c);
clearFocus();
}
}
The part I don't understand is here:
QTextCursor c = textCursor();
c.select(QTextCursor::Document);
setTextCursor(c);
What would be the equivalent Python code for this specific section? For some reason I thought the first line might be c = QTextCursor.textCursor()
in that result of the method textCursor
from the QTextCursor
class is being assigned to c
, but it seems that there is no textCursor
method. Also I'm having trouble understanding what is going on here:
QTextCursor c = this->textCursor();
c.clearSelection();
this->setTextCursor(c);
An explanation of what is going on in words would be useful as this would help with the terminology bit. A recommendation on some resources to go through to understand these specific pieces of code would be appreciated as well.
My Python and PyQt is rusty, but here's a translation with possible small errors in syntax:
def SetTextInteraction(self, on, selectAll):
if on and self.textInteractionFlags() == Qt.NoTextInteraction:
self.setTextInteractionFlags(Qt.TextEditorInteraction)
self.setFocus(Qt.MouseFocusReason)
self.setSelected(True)
if selectAll:
c = self.textCursor()
c.select(QTextCursor.Document)
self.setTextCursor(c)
elif not on and self.textInteractionFlags() == Qt.TextEditorInteraction:
self.setTextInteractionFlags(Qt.NoTextInteraction)
c = self.textCursor()
c.clearSelection()
self.setTextCursor(c)
self.clearFocus()
There are two reasons you're confused about what's happening in the code you linked to:
C++ handles implicit scope resolution at compile time; Python requires explicit declarations. Local scope (the member function) is checked first, then the surrounding class, then (I believe) the local translation unit/local non-member functions, and finally up the namespace/scope heirarchy until it finds the function or variable being referenced.
textCursor
is a member function of the parent class for TextItem
. Calling textCursor()
is the same as calling this->textCursor()
, which in Python would be self.textCursor()
.
The code provided intermingles explicit usage of this
with implicit calls. Using this
where not necessary is considered bad form in C++ and made it appear as if textCursor()
is not the same as this->textCursor()
. Hopefully in reading through the Python version I provided, you'll see there is no difference.
Resources for the future
The C++ tag has great links to FAQs on C++. I recommend a stroll through the C++ Super-FAQ. You'll learn things you didn't expect you needed to know and things you didn't know were not clear will be clarified. There is also The Definitive C++ Book Guide and List here on SO.
For PyQt development, Mark Summerfield's Rapid GUI Programming with Python and Qt is a great reference with working code.