【发布时间】: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