📄 c1036.htm
字号:
<HTML><HEAD><TITLE>Qt Concepts</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="PyQt fundamentals"HREF="p1032.htm"><LINKREL="PREVIOUS"TITLE="PyQt fundamentals"HREF="p1032.htm"><LINKREL="NEXT"TITLE=" As simple as they come "HREF="x1067.htm"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="p1032.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x1067.htm"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH1">Chapter 6. Qt Concepts</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><AHREF="c1036.htm#AEN1040">Python, Qt and PyQt</A></DT><DT><AHREF="x1067.htm">As simple as they come</A></DT><DT><AHREF="x1149.htm">A better Hello World</A></DT><DT><AHREF="x1214.htm">Designing forms</A></DT><DT><AHREF="x1263.htm">Conclusion</A></DT></DL></DIV><P>This chapter describes the way Python and Qt bind together, using the PyQt wrapper library. Concepts peculiar to PyQt and Qt such as signals and slots, inheritance and gui design, are introduced by building steadily more complex versions of the hackneyed <SPAN><ICLASS="EMPHASIS">hello world</I></SPAN> example application.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN1040">Python, Qt and PyQt</A></H1><P>Unlike a tool like Visual Basic, which consists of a GUI engine with a scripting language built-in, Python does not have a native GUI interface. But there are many GUI libraries available for Python — examples are wxPython, Tkinter, PyGTK, PyFLTK, FoxPy, and PyQt. PyQt is based on Qt, an advanced GUI library for Windows and Unix written in C++ by Eirik Eng and Arnt Gulbrantsen of Trolltech in Norway. It's quite easy to wrap C++ or C libraries so they can be used from Python — and when Phil Thompson was looking around for a good GUI library for Python he decided to wrap Qt, producing PyQt. PyQt forms the basis for the BlackAdder rapid development environment.</P><P>Qt is very advanced: the library offers a large set of well-designed screen objects, or widgets, and many utility classes. In addition, Qt has a clean object-oriented design that is easy to grasp and intuitive to use. PyQt applications can run without any change, without recompiling, both on Windows and Unix/X11 systems — and soon on Apple's OS X, too.</P><P>PyQt widgets can be drawn in <SPAN><ICLASS="EMPHASIS">styles</I></SPAN>, to make them appear exactly like the native widgets of the operating system the application runs on (or like something different altogether, if you want).</P><P>There are two kinds of objects in the Qt library— visual and non-visual. The mother of all visual objects is QWidget, widget being the term Qt uses for what the Windows world usually calls <SPAN><ICLASS="EMPHASIS">control</I></SPAN>. There are simple widgets such as labels and buttons, and complex widgets such as canvas views. Even dialog windows are descended from <TTCLASS="CLASSNAME">QWidget</TT>.</P><P><TTCLASS="CLASSNAME">QWidget</TT> and many other, but not all, Qt classes derive from the <TTCLASS="CLASSNAME">QObject</TT> base class — a class like <TTCLASS="CLASSNAME">QLayout</TT> <SPAN><ICLASS="EMPHASIS">is</I></SPAN> derived from <TTCLASS="CLASSNAME">QObject</TT>, but <TTCLASS="CLASSNAME">QPixmap</TT> is not. Whether a class is derived from <TTCLASS="CLASSNAME">QObject</TT> is determined by whether there is a need for signals and slots, and whether the created objects must be placed in an ownership hierarchy.</P><P>Scripting languages like Python have a reputation for bad performance, but PyQt applications perform very well indeed; there is just a thin wrapper between the GUI objects and Python. Those GUI objects do most of the heavy work of pixel shifting, and they are written in well-optimized C++. If you try to do things like writing your own DTP layout engine from scratch using the Qt drawing primitives, you might be hindered by the slowness inherent in a byte-code interpreted language like Python, but on the whole, your application will be as responsive as one written in pure C++, and you'll have a working application where you would still be hacking the first prototype in C++.</P><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>A note on versions: </B>PyQt consists of at least three components, Python, Qt and PyQt itself. Additionally, there is PyKDE, the bindings to the KDE Desktop Environment for Unix. That's four components with almost unrelated version numbering. Qt has been through three versions to date, just like PyQt. However, PyQt 3.x can be compiled against Qt 1.x, Qt 2.x <SPAN><ICLASS="EMPHASIS">and</I></SPAN> Qt 3.x. </P><P>The differences between versions of PyQt consist of the range of versions of Qt supported, and certain matters of organization and some implementation details.</P><P>The relation between PyQt and Python is even more nebulous: PyQt works best with the latest version of Python, but earlier versions are known to work, too.</P><P>Anyway, just keep in mind that PyQt 3.x can use Qt 2.x or Qt 3.x. This book was mostly written using PyQt 2.x for Qt 2.x, but I've tried to include as much information about PyQt 3.x and Qt 3.x as I could.</P></BLOCKQUOTE></DIV></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="p1032.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="book1.htm"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="x1067.htm"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">PyQt fundamentals</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="p1032.htm"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">As simple as they come</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -