【问题标题】:Equivalent PyQt code for C++ Qt SnippetC++ Qt Snippet 的等效 PyQt 代码
【发布时间】:2016-10-11 14:18:33
【问题描述】:

我正在使用 PyQt,并且了解足够多的 OOP 以在 Python 中轻松应对。但是,文档和有用的论坛帖子都是用 C++ 编写的。我知道最好的方法可能是重新学习 C++。我正在尝试,但筛选教程并找到我需要的信息需要很长时间,主要是因为我对术语的理解不够,不知道在哪里查找。

在一个特定的论坛post 中有一个类实现的方法中的部分内容如下:

    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();
    }
}

不明白的部分在这里:

QTextCursor c = textCursor();
c.select(QTextCursor::Document);
setTextCursor(c);

这个特定部分的等效 Python 代码是什么?出于某种原因,我认为第一行可能是c = QTextCursor.textCursor(),因为QTextCursor 类中的方法textCursor 被分配给c,但似乎没有textCursor 方法。我也无法理解这里发生了什么:

 QTextCursor c = this->textCursor();
 c.clearSelection();
 this->setTextCursor(c);

用文字解释正在发生的事情会很有用,因为这将有助于术语位。如果您能推荐一些资源来理解这些特定的代码,我们也将不胜感激。

【问题讨论】:

  • C++ 中的 this -> Python 中的 self

标签: python c++ qt pyqt code-translation


【解决方案1】:

我的 Python 和 PyQt 生锈了,但这里有一个翻译可能有语法上的小错误:

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

您对链接到的代码中发生的事情感到困惑有两个原因:

  1. C++ 在编译时处理隐式范围解析; Python 需要显式声明。首先检查本地范围(成员函数),然后是周围的类,然后(我相信)本地翻译单元/本地非成员函数,最后是命名空间/范围层次结构,直到找到被引用的函数或变量。

    textCursorTextItem 的父类的成员函数。调用textCursor() 与调用this->textCursor() 相同,在Python 中为self.textCursor()

  2. 提供的代码将this 的显式使用与隐式调用混合在一起。在没有必要的情况下使用 this 在 C++ 中被认为是错误的形式,并使其看起来好像 textCursor()this->textCursor() 不同。希望在阅读我提供的 Python 版本时,您会发现没有任何区别。

未来的资源
C++ tag 有很好的 C++ 常见问题链接。我建议通过C++ Super-FAQ 漫步。你会学到你没想到你需要知道的东西,你不知道的东西会被澄清。 SO上还有The Definitive C++ Book Guide and List

对于 PyQt 开发,Mark Summerfield 的 Rapid GUI Programming with Python and Qt 是一个很好的工作代码参考。

【讨论】:

  • 这很有意义。看到this 在一行中使用而不是在另一行中,这有点令人困惑。我希望从这个答案和您推荐的资源中,我可以了解更多 C++ Qt 示例。
  • 一个小编辑。 elif not on and textInteractionFlags() 应该是 elif not on and self.textInteractionFlags()
  • 我推荐 Mark Summerfield 的 Qt 和 PyQt 书籍,用于工作代码示例和解释。由于他同时是 Qt 和 PyQt 开发人员,因此他的工作特别吸引人。
  • 好电话。我昨天下载了他的书《Rapid GUI Programming with Python and Qt》,真的很棒。
【解决方案2】:

原代码中SetTextInteractionQGraphicsTextItem子类的方法,textCursor()方法继承自QGraphicsTextItem。翻译成 PyQt 是字面意思:

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

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

在这段代码中,我们使用QGraphicsTextItem::textCursor 获得一个光标对象,修改它并使用QGraphicsTextItem::setTextCursor 设置它以应用更改。

【讨论】:

  • 很高兴知道 SetTextInteraction 在这里被覆盖。考虑到它在我喜欢的帖子中的使用方式,我不会知道。
  • SetTextInteraction 没有被覆盖,因为基类中没有这样的方法。这只是一种新方法。
猜你喜欢
  • 2014-03-21
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2015-10-05
相关资源
最近更新 更多