C++QT code to PYQT code guide
Table of Contents
1 a chestnut.
#include <QApplication>
#include <QtGui>
#include <QtCore>
C + + code
int main (int argc, char *argv[])
{
Qapplication app (argc, argv);
Qsplitter *splitter = new Qsplitter;//qsplitter user split two widgets
Qfilesystemmodel *model = new Qfilesystemmodel;
Model->setrootpath (Qdir::currentpath ());
Qtreeview *tree = new Qtreeview (splitter);
Tree->setmodel (model); Set up a model for a view
Tree->setrootindex (Model->index (Qdir::currentpath ()));
Qlistview *list = new Qlistview (splitter);
List->setmodel (model);
List->setrootindex (Model->index (Qdir::currentpath ()));
Splitter->setwindowtitle ("the onto the same file system model");
Splitter->show ();
return App.exec ();
}
From PYQT4 import Qtgui
From PYQT4 import Qtcore
Import Sys
After conversion
def main (argv):
App = Qtgui.qapplication (argv)
Splitter = Qtgui.qsplitter ()
Model = Qtgui.qfilesystemmodel ()
Model.setrootpath (QtCore.QDir.currentPath ())
Tree = Qtgui.qtreeview (splitter)
Tree.setmodel (model)
Tree.setrootindex (Model.index (QtCore.QDir.currentPath ()))
Qlv_list = Qtgui.qlistview (splitter)
Qlv_list.setmodel (model)
Qlv_list.setrootindex (Model.index (QtCore.QDir.currentPath ()))
Splitter.setwindowtitle ("onto the same file system model")
Splitter.show ()
Return App.exec_ ()
if __name__ = = ' __main__ ':
Main (SYS.ARGV)
Run
2 Variable life cycle
- C + + uses Rtti for memory management, or manually manages
- Python uses reference counting to automatically manage memory
- QT uses its tree model to manage memory, and when the parent node is refactored, all child nodes are automatically refactored
Therefore, when programming with C++/QT, almost all of the Qobject instance objects are constructed with a reference to their parent to prevent memory leaks, and the same is true in pyqt, not to prevent their memory leaks, because Python automatically manages class instances based on the reference count. Once the variable is outside its scope without any references, it is automatically destroyed, but the objects that live in the QT tree model are spared, see the following code
3 Tool Use
Ipython's automatic hints and Python's introspection
4 Memo List
- In PyQt, all new objects appear in the form of a stack object, but pay attention to the lifetime of their objects, and notice when the object is "deconstructed"
- Python has no "stack object" and "heap object", so you can almost ignore the difference between a C + + pointer and a reference
- Thanks to a little bit, you can ignore the difference between a C + + instance reference (.) and a pointer reference (.), in Python, use the (.) Dot number instead
- Python's static method invocation and instance method invocation are all using the (.) dot number, while C + + uses a namespace character (::), which is still used in the PYQT (.) Dot number substitution
- There is no enum (enum) type in C + + in Python, so the enumeration type has different references
- Python uses the concept of a package, library, and C + + uses the header file concept, QT does not use C + + namespace (namespace)
- Notice how some special objects are generated, such as Qicon, which in most cases is a stack object in c++/qt, not a tree model that lives in Qt.
- For some special objects, such as the qstring of C + + and the Python string and the corresponding Unicode, please understand the platform encoding (in general, Win32 use GB series, *nix use UTF-8), command line display encoding ( Refers to Win32 's CMD, generally uses the GB series), the terminal code (if you use a tool such as putty or Xshell, please pay attention to the terminal code and the shell $lang environment variable), file encoding, memory encoding (if possible, in-memory string, please use all Unicode
- The object conversion relationships of the standard library, such as Qlist and std::list and Python list, I've started a little dizzy, with this derivative such as qlistview,qstringlist and so on, another std::map,qmap,dict for Python will not go into detail, or look at the document, Qt's documentation is great.
- Keep an eye out for objects of non-qobject subclasses, such as Qt's Qgraphicsitem in a graphical view frame, don't get caught in the hole.
- The Python2.7 keyword EXEC,PYQT is replaced with EXEC_, which is also true in Qdialog, which has been improved in py3k
date:2013-12-04t14:39+0800
C++QT code to PYQT code guide