📄 porting.html
字号:
<h3><a name=QLayout>QLayout</a></h3><p> Calling resize(0,0) or resize(1,1) will no longer work magically.Remove all such calls. The default size of toplevel widgets will be their<a href="qwidget.html#4511d1">sizeHint()</a>.<p> The default implementation of QWidget::sizeHint() will no longerreturn just an invalid size; if the widget has a layout, it will returnthe layout's preferred size.<p> The special maximum MaximumHeight/Width is now QWIDGETSIZE_MAX,not QCOORD_MAX.<p> <a href="qboxlayout.html#ebba99">QBoxLayout::addWidget()</a>now interprets the <em>alignment</em> parameter more aggressively. Anon-default alignment now indicates that the widget should not grow tofill the available space, but should be sized according to sizeHint().If a widget is too small, set the alignment to 0. (Zero indicates noalignment, and is the default.)<p> The class QGManager is removed. Subclasses of QLayout need to be rewrittento use the new, much simpler <a href="qlayout.html">QLayout API</a>.<p> For typical layouts, all use of<a href="qwidget.html#c0b5fb">setMinimumSize()</a>and<a href="qwidget.html#87e3f4">setFixedSize()</a>can be removed.<a href="qlayout.html#1cb33d">activate()</a> is no longer necessary.<p>You might like to look at the QGrid, QVBox, and QHBox widgets - they offera simple way to build nested widget structures.<p><h3><a name=QListView>QListView</a></h3><p>In Qt 1.x mouse events to the viewport where redirected to theevent handlers for the listview; in Qt 2.x, this functionality isin QScrollView where mouse (and other position-oriented) events areredirected to viewportMousePressEvent() etc, which in turn translatethe event to the coordinate system of the contents and callcontentsMousePressEvent() etc, thus providing events in the mostconvenient coordinate system. If you overrode QListView::MouseButtonPress(),QListView::mouseDoubleClickEvent(), QListView::mouseMoveEvent(), orQListView::mouseReleaseEvent() you must instead overrideviewportMousePressEvent(),viewportMouseDoubleClickEvent(), viewportMouseMoveEvent(), orviewportMouseReleaseEvent() respectively. New code will usually overridecontentsMousePressEvent() etc.<p>The signal QListView::selectionChanged(QListViewItem *) can now beemitted with a null pointer as parameter. Programs that use theargument without checking for 0, may crash.<p><h3><a name=QMultiLineEdit>QMultiLineEdit</a></h3><p>The protected function<pre> <a href="qmultilineedit.html#a97ed4">QMultiLineEdit::textWidth</a>(<a href="qstring.html">QString</a>*)</pre><p>changed to<pre> <a href="qmultilineedit.html#a97ed4">QMultiLineEdit::textWidth</a>(const QString&)</pre>.This is unlikely to be a problem, and you'll get a compile errorif you called it.<p><h3><a name=QClipboard>QClipboard</a></h3><p><pre> <a href="qclipboard.html#905cbc">QClipboard::pixmap</a>()</pre> now returns a QPixmap, not a QPixmap*.The pixmapwill be <a href="qpixmap.html#7803c4">null</a> if no pixmap is on theclipboard. QClipboard now offers powerful MIME-based types on theclipboard, just like drag-and-drop (in fact, you can reuse most of yourdrag-and-drop code with clipboard operations).<p><h3><a name=QDropSite>QDropSite</a></h3><p><P>QDropSite is obsolete. If you simply passed <tt>this</tt>, just removethe inheritance of QDropSite and call<a href="qwidget.html#8169cb">setAcceptDrops(TRUE)</a> in the classconstructor.If you passed something other than <tt>this</tt>,your code will not work. A common case is passingthe<a href="qscrollview.html#26f2bf">viewport()</a> of a QListView,in which case,override the<a href="qscrollview.html#f54ca8">viewportDragMoveEvent()</a>,etc.functions rather than QListView's dragMoveEvent() etc. For othercases, you will need to use an event filter to act on the drag/drop eventsof another widget (as is the usual way to intercept foreign events).<p><h3><a name=QScrollView>QScrollView</a></h3><p>The parameters in the signal<a href="qscrollview.html#479944">contentsMoving(int,int)</a>are now positive rather than negative values, coinciding with<a href="qscrollview.html#15a368">setContentsPos()</a>. Search forconnections you make to this signal, and either change the slot they areconnected to such that it also expects positive rather than negativevalues, or introduce an intermediate slot and signal that negates them.<p>If you used drag and drop with QScrollView, you may experience the problemdescribed for <a href=#QDropSite>QDropSite</a>.<p><h3><a name=QTextStream>QTextStream</a></h3><p><pre> operator<<(<a href="qtextstream.html">QTextStream</a>&, QChar&)</pre> does not skip whitespace.<pre> operator<<(<a href="qtextstream.html">QTextStream</a>&, char&)</pre> does, as was the case with Qt 1.x. This is for backward compatibility.<p><h3><a name=QUriDrag>QUriDrag</a></h3><p>The class QUrlDrag is renamed to QUriDrag, and the API has beenbroadened to include additional conversion routines, includingconversions to Unicode filenames (see the class documentationfor details). Note that in Qt 1.xthe QUrlDrag class used the non-standard MIME type "url/url",while QUriDrag uses the standardized "text/uri-list" type. Otheridentifiers affected by the Url to Uri change areQUrlDrag::setUrls() and QUrlDrag::urlToLocalFile().<p><h3><a name=QPainter>QPainter</a></h3><p> The GrayText painter flag has been removed. Use<a href="qpainter.html#0183e4">setPen( palette().disabled().foreground() )</a>instead.<p> The RasterOp enum(CopyROP, OrROP, XorROP, NotAndROP, EraseROP, NotCopyROP, NotOrROP, NotXorROP, AndROP, NotEraseROP, NotROP, ClearROP, SetROP, NopROP, AndNotROP, OrNotROP, NandROP, NorROP, LastROP)is now part of the Qt namespace class, so if youuse it outside a member function, you'll need to prefix with Qt::.<p><h3><a name=QPicture>QPicture</a></h3><p>The binary storage format of QPicture is changed, but the Qt 2.xQPicture class can both read and write Qt 1.x format QPictures. Nospecial handling is required for reading; QPicture will automaticallydetect the version number. In order to write a Qt 1.x format QPicture,set the formatVersion parameter to 1 in the QPicture constructor.<p>For writing Qt 1.x format QPictures, the compatibility issues of <ahref=#QDataStream>QDataStream</a> applies.<p>It is safe to try to read a QPicture file generated with Qt 2.x(without formatVersion set to 1) with a program compiled with Qt1.x. The program will not crash, it will just issue the warning"QPicture::play: Incompatible version 2.x" and refuse to load thepicture.<p><h3><a name=QPoint>QPoint, QPointArray, QSize and QRect</a></h3><p>The basic coordinate datatype in these classes, QCOORD, is now 32bit (int) instead of a 16 bit (short). The const values QCOORD_MIN andQCOORD_MAX have changed accordingly.<p>QPointArray is now actually, not only seemingly, a QArray of QPointobjects. The semi-internal workaround classes QPointData and QPointValare removed since they are no longer needed; QPoint is used directlyinstead. The function <pre> <a href="qpointarray.html#0dbaa7">QPointArray::shortPoints</a>()</pre><p>provides the point array converted to short (16bit) coordinates foruse with external functions that demand that format.<p><h3><a name=QImage>QImage</a></h3><p>QImage uses QRgb for the colors - see <a href=#QRgb>the changes to that</a>.<p><h3><a name=QPixmap>QPixmap</a></h3><p><pre> <a href="qpixmap.html#8c5f15">QPixmap::convertToImage</a>()</pre> with bitmaps now guarantees that color0 pixelsbecome color(0) in the resulting QImage. If you worked around the lack ofthis, you may be able to simplify your code. If you made assumptionsabout the previous undefined behavior, the symptom will be invertedbitmaps (eg. "inside-out" masks).<p><pre> QPixmap::optimize(TRUE)</pre><p>is replaced by<pre> <a href="qpixmap.html#e6ae80">QPixmap::setOptimization</a>(QPixmap::NormalOptim)</pre><p>or<pre> <a href="qpixmap.html#e6ae80">QPixmap::setOptimization</a>(QPixmap::BestOptim)</pre><p>- see the documentationto choose which is best for your application. NormalOptim is most likethe Qt 1.x "TRUE" optimization.<p><h3><a name=QMenuData>QMenuData / QPopupMenu</a></h3><p>In Qt 1.x, new menu items were assigned either an application-wideunique identifier or an identifier equal to the index of the item, depending on the<a href="qmenudata.html#a0155e">insertItem(...)</a> function used.In Qt 2.x this confusingsituation has been cleaned up: generated identifiers are alwaysunique across the entire application.<p>If your code depends on generated idsbeing equal to the item's index, a quick fix is to use<pre> <a href="qmenudata.html#49862d">QMenuData::indexOf</a>(int id)</pre><p>in the handling function instead. You may alternatively pass<pre> <a href="qmenudata.html#e3e249">QMenuData::count</a>()</pre><p>as identifier when you insert the items.<p>Furthermore, QPopupMenus can (and should!) be created with a parentwidget now, for example the main window that is used to display thepopup. This way, the popup will automatically be destroyed togetherwith its main window. Otherwise you'll have to take care of theownership manually.<p>QPopupMenus are also reusable in 2.x. They may occur in differentlocations within one menu structure or be used as both a menubardrop-down and as a context popup-menu. This should make it possible tosignificantly simplify many applications.<p>Last but not least, QPopupMenu no longer inherits QTableView. Instead,it directly inherits QFrame.<p><h3><a name=QValidator>QValidator (QLineEdit, QComboBox, QSpinBox) </a></h3><p><pre> <a href="qvalidator.html#da4f8c">QValidator::validate</a>(...)</pre><p>and<pre> <a href="qvalidator.html#3fa523">QValidator::fixup</a>( <a href="qstring.html">QString</a> & )</pre><p>are now constfunctions. If your subclass reimplements validate() as anon-const function,you will get a compile error (validate was pure virtual).<p>In QLineEdit, QComboBox, and QSpinBox,setValidator(...) now takes a const pointer to a QValidator, andvalidator() returns a const pointer. This change highlights the factthat the widgets do not take the ownership of the validator (a validator isa QObject on its own, with its own parent - you can easily set the same validatorobject on many different widgets), so changing the state ofsuch an object or deleting it is very likely a bug.<p><h3><a name=QFile>QFile, QFileInfo, QDir</a></h3><p>File and directory names are now always Unicode strings (ie. QString). If you used QStringin the past for the simplicity it offers, you'll probably have little consequence. However,if you pass filenames to system functions rather than using Qt functions (eg. if you use theUnix <tt>unlink()</tt> function rather than <tt>QFile::remove()</tt>, your code will probablyonly work for Latin1 locales (eg. Western Europe, the U.S.). To ensure your code will supportfilenames in other locales, either use the Qt functions, or convert the filenames via<pre> QFile::encodeFilename()</pre> and <pre> QFile::decodeFilename()</pre> - but do it<em>just</em> as you call the system function - code that mixes encoded and unencoded filenamesis very error prone. See the comments in QString, such as regarding QT_NO_ASCII_CAST thatcan help find potential problems.<p><h3><a name=QFontMetrics>QFontMetrics</a></h3><p>boundingRect(char) is replaced byboundingRect(QChar), but sincechar auto-converts to QChar, you're not likely to run into problemswith this.<p><h3><a name=QWindow>QWindow</a></h3><p>This class (which was just QWidget under a different name) has beenremoved. If you used it, do a global search-and-replace of the word"QWindow" with "QWidget".<p><h3><a name=QEvent>QEvent</a></h3><p> The global #define macros in qevent.h have been replaced by anenum in QEvent. Use e.g. QEvent::Paint instead of Event_Paint. Samefor all of:Event_None,Event_Timer,Event_MouseButtonPress,Event_MouseButtonRelease,Event_MouseButtonDblClick,Event_MouseMove,Event_KeyPress,Event_KeyRelease,Event_FocusIn,Event_FocusOut,Event_Enter,Event_Leave,Event_Paint,Event_Move,Event_Resize,Event_Create,Event_Destroy,Event_Show,Event_Hide,Event_Close,Event_Quit,Event_Accel,Event_Clipboard,Event_SockAct,Event_DragEnter,Event_DragMove,Event_DragLeave,Event_Drop,Event_DragResponse,Event_ChildInserted,Event_ChildRemoved,Event_LayoutHint,Event_ActivateControl,Event_DeactivateControl,andEvent_User.<p> The Q_*_EVENT macros in qevent.h have been deleted. Use anexplicit cast instead. The macros were:Q_TIMER_EVENT,Q_MOUSE_EVENT,Q_KEY_EVENT,Q_FOCUS_EVENT,Q_PAINT_EVENT,Q_MOVE_EVENT,Q_RESIZE_EVENT,Q_CLOSE_EVENT,Q_SHOW_EVENT,Q_HIDE_EVENT,andQ_CUSTOM_EVENT.<p> QChildEvents are now sent for all QObjects, not just QWidgets.You may need to add extra checking if you use a QChildEvent withoutmuch testing of its values.<p><h3>All the removed functions</h3><p>All <a href="removed20.html">these functions</a> have been removed inQt 2.x. Most are simply cases where "const char*" has changed to"const QString&", or when an enumeration type has moved into the Qt::namespace (which, technically, is a new name, but your code willcompile just the same anyway). This list is provided for completeness.<p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -