4

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.

pbreach
  • 16,049
  • 27
  • 82
  • 120

2 Answers2

2

SetTextInteraction in the original code is a method of a subclass of QGraphicsTextItem, and textCursor() method is inherited from QGraphicsTextItem. Translation to PyQt is literal:

class TextItem(QGraphicsTextItem):
  def __init__(self, parent=None):
    super(TextItem, self).__init__(parent)

  def test(self):
    c = self.textCursor()
    c.clearSelection()
    self.setTextCursor(c)

In this piece of code we get a cursor object using QGraphicsTextItem::textCursor, modify it and set it using QGraphicsTextItem::setTextCursor to apply the changes.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Great to know that `SetTextInteraction` is being overriden here. I would have not know otherwise considering how it is used in the post I like. – pbreach Jun 11 '16 at 16:29
  • 1
    `SetTextInteraction` is not being overriden because there is no such method in the base class. It's just a new method. – Pavel Strakhov Jun 11 '16 at 16:56
2

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:

  1. 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().

  2. 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.

Community
  • 1
  • 1
jonspaceharper
  • 4,207
  • 2
  • 22
  • 42
  • This makes a lot of sense now. It was a bit confusing to see the `this` being used in one line and not the other. I'm hoping from this answer and the resources you've recommended that I can get through more of the C++ Qt examples. – pbreach Jun 11 '16 at 16:32
  • 1
    One small edit. `elif not on and textInteractionFlags()` should be `elif not on and self.textInteractionFlags()` – pbreach Jun 11 '16 at 17:48
  • I recommend Mark Summerfield's Qt and PyQt books for working code examples and explanations. Since he's both a Qt and PyQt developer, his work is particularly appealing. – jonspaceharper Jun 11 '16 at 17:55
  • Good call. I downloaded his book, "Rapid GUI Programming with Python and Qt" yesterday and it is really great. – pbreach Jun 11 '16 at 17:58