Qt3代码移植到Qt5要注意的问题总结(未完待续)

一半是参考了https://download.csdn.net/download/zhujianhuaqqa/9418427 。然后在此基础上补充和修改了很多。

推荐网站:http://qt.apidoc.info/4.7.4/qtcore.html

http://doc.qt.io/archives/qt-4.8/porting4.html#

1.由于ACE库的原因,使用qt自带的mingw编译器会导致ACE编译出现若干各种错误。
  解决方法:将qtcreater中的编译器设置成2013的编译器(相应的QT版本也要安装成2013版的)。
  windows下安装包  qt-opensource-windows-x86-msvc2013_opengl-5.4.2.exe
  linux下安装包    暂定
  VS插件           qt-vs-addin-1.2.4-opensource.exe,该插件可将qt5作为菜单集成到VS2013中进行开发。
2.QString 转 char*
  Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,
  而且函数参数类型五花八门,  经常需要今年新那个类型转换
  Qt再使用第三方开源库时,由于库的类型基本上都是标准的类型,字符串遇的多的就是Char*类型
  在Qt下怎样将QString转char*呢,需要用到QByteArray类,QByteArray类的说明详见Qt帮助文档。
  因为char*最后都有一个‘/0’作为结束符,而采用QString::toLatin1()时会在字符串后面加上‘/0’
  方法如下:
  Qstring  str;
  char*  ch;
  QByteArray ba = str.toLatin1(); 
  ch=ba.data();
  这样就完成了QString向char*的转化。经测试程序运行时不会出现bug
  注意第三行,一定要加上,不可以str.toLatin1().data()这样一部完成,可能会出错。
  补充:以上方法当QString里不含中文时,没有问题,但是QString内含有中文时,转换为char*就是乱码,采用如下方法解决:
  方法1:
  添加GBK编码支持:
  #include <QTextCodec>
  QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
  QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
  然后改变上面的第三行为:QByteArray ba = str.toLoacl8Bit();      toLoacl8Bit支持中文
  方法2:
  先将QString转为标准库中的string类型,然后将string转为char*,如下:
  std::string str = filename.toStdString();
  const char* ch = str.c_str();
3.link工程时遇到ACE相关link错误时,只需在.pro文件中添加如下内容:
  debug {
   CONFIG -= release
   win32: LIBS+= $(ACE_ROOT)/ace/aced.lib
  }
  release {
     CONFIG -= debug
  #   CONFIG -= console
     win32: LIBS+= $(ACE_ROOT)/ace/ace.lib
  }
  是由于没有包含lib库的原因.
4.#include <qapplication.h> 编译错误
  只需在pro中添加:
  QT       += core gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
5.QPtrList等已经修改为QList,
QList<QLabel> lines; //QPtrList modified
for (i = lines.count(); i > addLines; i--)
{
            QLabel* l = lines.value(i); //QPtrList 的take()
l->close();
}
6.QList中的next函数已经取消了,使用it迭代器访问list吧
7.QString的 simplifyWhiteSpace() 改成simplified()
8.QList中的remove()改成了removeone()
9.QIntDick被删除,暂时用QList代替
10.QList中的resize()改成了reserve()
11.QWMatrix转换成了QMatrix,二维坐标转换类
12.文件读取Flag 由IO_WriteOnly变为QIODevice:: WriteOnly,需要从QIODevice获得
13. QFileInfo
absFilePath()变成了absoluteFilePath() ;
dirPath()变成了path(),获取当前文件的目录,不包括文件名;
extension()变成了completeSuffix(),返回名字后缀,例如tar.gz
14.QFile中writeBlock()改成了write()
15. 
QStringList QStringList::split ( const QRegExp & sep, const QString & str, bool allowEmptyEntries = FALSE ) [static]
QStringList split(QChar sep, SplitBehavior behavior = KeepEmptyParts,Qt::CaseSensitivity cs = Qt::CaseSensitive) const;


QStringList::split已经被删除,移动到了QString中
    旧的用法用法:
    QString data = "XXXX";
    QStringList pathlist = QStringList::split( " ",data,false);
    改为:
    QString data = "XXXX";
    QStringList pathlist = data.split(" ", QString::SkipEmptyParts);
16. QRadioButton的构造函数,将name和parent两个参数位置调换了.Qlabel也是的
17.如果需要使用QList中的removeOne()函数,需要手动重载list类型中的==操作符,不然编译会报QT库编译错误。
18.QImage中的smoothScale改变为scaled。
19.QImage中的reset暂时使用一个空image赋值。
20. QImage中的setAlphaBuffer,暂时注释掉,找不到合适的API替代。
21. qpopupmenu.h改成了qmenu.h
22. WFlags变成了Qt::WindowFlags
23.
24. QPtrCollection变成QCollection
25.QIconSet变成QIcon
26.QDockArea变成QDockWidget
27.qpointarray.h变成qpolygon.h
28.QSocket变成QNetworkAccessManager
29.qheader.h变成qheaderview.h
30.qwidgetfactory.h变成qwidget.h
31.qlistviewitem已经被删除
The QListView, QListViewItem, QCheckListItem, and QListViewItemIterator 
classes have been renamed Q3ListView, Q3ListViewItem, Q3CheckListItem, 
and Q3ListViewItemIterator, and have been moved to the Qt3Support library. 
New Qt applications should use one of the following four classes instead: 
QTreeView or QTreeWidget for tree-like structures; QListWidget or the 
new QListView class for one-dimensional lists.


32.caption变成了windowTitle
33.QScrollView变成了QScrollArea
34.QObject的setname变成了setObjectName
35.QPushButton构造函数两个参数调换位置了,多数控件参数都调换了位置
36.QButtonGroup构造函数只剩QWidget一个参数,原来是两个。QButtonGroup改为直接继承QObject,
简单的用法就是用addButton函数,将button追加进去,详细用法需要参照例子。
37、QButtonGroup和QGroupBox有点区别,如果是qt3.3的QButtonGroup,官方建议是将QButtonGroup改为QGroupBox。
38.QPixmap中的resize改为scaled,功能更加强大。
39.QPixmap中的枚举已经被删除,直接使用QT提供的枚举,例:Qt::AutoColor
40.QPushButton中的setToggleButton以及seton已经改为setCheckable和setChecked
41.QComboBox的构造函数三个参数改成了一个参数,只剩QWidget一个参数,另外两个参数需要使用
   setWindowTitle设置标题,用setEditable设置是否可编辑
42.QWidget的setCaption函数已经改为setWindowTitle,同样Caption也改成了windowTitle
43.QButtonGroup中的setTitle改为setObjectName
44.qt 设置背景色
   qt 之前版本中有关背景色设置的函数如setBackgroundColor() 或是前景色设置函数如setForegroundColor()
   在qt4中都被废止,统一由QPalette类进行管理. 如 setBackgroundColor()函数可由以下语句代替: 
   xxx -> setAutoFillBackground(true);
   QPalette p = xxx ->palette();
   p.setColor(QPalette::Window,color); 
   //p.setBrush(QPalette::Window,brush);
   xxx -> setPalette(p);


   注:设置背景色也可用 xxx->setStyleSheet(QString("background-color:%1").arg(_color.name()));


45.QSpinBox中的setMinValue修改为setMinimum,同样setMaxValue修改为setMaximum
46.QDialog中的setCaption已经修改为setWindowTitle,即是QWidget提供的函数
47.QCombox中的insertItem参数添加了元素pos位置
48.QString
simplifyWhiteSpace已经修改为simplified,
rightJustify修改为rightJustified
rightJustify修改为leftJustified


QString line1;
QStringList list1 = QStringList::split(QRegExp("[ \n\r][ \n\r]*"), line1, TRUE); 
修改为 
QStringList list1 = line1.split(QRegExp("[ \n\r][ \n\r]*"), QString::KeepEmptyParts); //modified
或者
QStringList list1 = line1.split(QRegExp("[ \n\r][ \n\r]*")); //QString::KeepEmptyParts是默认值




QConstString更改为 QString::fromRawData()。
QCString更改诶QString。
49.QXXX的UI相关的类,既有参数传name的参数,现在修正为 XXX->setObjectName("name");
50.QPopupMenu改为QMenu
51.QT5中自定义菜单的创建方法 
   //创建action
   QAction *freshAction = new QAction(QIcon(":/images/fresh.png"), tr("刷新"), parent);
   //创建菜单项
   QMenu *fileMenu = menuBar()->addMenu(tr("XX"));
   //将action添加到菜单中
   fileMenu->addAction(freshAction);
   //添加分隔符
   fileMenu->addSeparator();
   
52.QApplication中的argc()和argv()合并成了arguments(),取消了argc()函数。
53.QString类型的数据不能直接作为if语句的判断条件了,需要调用QString的isEmpty()函数返回bool型才能使用。
54.
55.setBackgroundMode该法等同于第44项
56.QWidget::StrongFocus转移到了qnamespace.h下面了
   使用方法,即 Qt::FocusPolicy::StrongFocus
57.QWidget中的repaint函数,参数由int型变为QRect,QRegion,或者int x, int y, int w, int h
58.QPixmap中resize暂时改为scaled。注意!!!!
59.QMemArray和QValueVector改为QVector替换
60.QStrList改为QStringList
61.QVariant中的toColor()函数改为 QVariant::value() or the qvariant_cast()。
   用法:QVariant variant;
         ...
         QColor color = variant.value<QColor>();
   详细的可参考Qt助手
62.colorGroup()使用QPalette下的相关替代
63.QWidget的setShown(bool)函数变为hide()
64.QWidget的resizeContents变为resize;
65.bitBlt函数被删除,使用QPainter下的drawPixmap代替
   用法:QPainter pa(widget);
         pa.drawPixMap(XXX);//XXX是若干参数
66.QPointArray已经修改为QPolygon,功能:Point的vector
67.#include "qdom.h"变成了#include "QtXml/qdom.h",或者在pro中添加QT += xml
68.#include "qstrlist.h"变成了#include "qstringlist.h"
69.#include <qobjectlist.h>变成了#include <QObjectList>
70.#include <qlistbox.h>使用#include <qlistview.h>替换
71.#include <qdragobject.h>变成了#include <qdrag.h>
72.QRegExp中的search改为indexIn函数
73.QDir中的entryInfoList返回值类型由QFileInfoList*指针型转换为QFileInfoList对象型
74.QFileInfoList只能使用正常迭代器的方法取值,原QFileInfoListIterator已经被删除
  const QFileInfoList *files = dir.entryInfoList();  
  if (files != NULL)
{
        // 这些QFileInfoListIterator的是无法使用了
        QFileInfoListIterator it(*files);  //QFileInfoListIterator no longer exists in Qt4-Qt5
        QFileInfo* fi = NULL;
        while ((fi = it.current()) != NULL)
{
          
//更改为下面的for循环
        for(int i = 0; i< files->count(); i++)  //modified by CharlesCao
        {
            QFileInfo fi = files->at(i);


75.QDir中setNameFilter改为setNameFilters,参数由QString变成了QStringList,所以用法如下
   用法:
   QStringList filters;
   filters << "*.cpp" << "*.cxx" << "*.cc";
   dir.setNameFilters(filters);
76.使用#include <QNetworkAccessManager>头文件,必须在pro文件中添加QT += network,否则link不通过
77.使用#include <QDomElement>头文件,必须在pro文件中添加QT += xml,否则link不通过
78.QPainter中的setRasterOp函数改为setCompositionMode,详细的参数解释请参照qt助手。
   以下两种使用的最多,Qt::CopyROP和Qt::XorROP
   用法:
   ①  painter.setRasterOp(Qt::CopyROP);
   改为painter.setCompositionMode(QPainter::CompositionMode_Source);
   ②  painter.setRasterOp(Qt::XorROP);
   改为painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination);
   ③  painter.setRasterOp(Qt::OrROP);
   改为painter.setCompositionMode(QPainter::RasterOp_SourceOrDestination);      
   其他用法参照qt助手。
79.QListView改为QTreeWidget,或者QListWidget,看情况。
80.QTable改为QTableWidget
81.QImage中convertDepth()变为convertToFormat()
82.QImage中scale变为scaled
83.QString中fromAscii变为fromLatin1
84.QPainter中drawPolygon参数变了,
   用法:   
   qt3定义:QPainter::drawPolygon ( const QPointArray & a, bool winding = FALSE, int index = 0, int npoints = -1 ) 
   qt3使用:painter.drawPolygon(*pa,TRUE,0,i);
   
   qt5定义:QPainter::drawPolygon(const QPolygon & points, Qt::FillRule fillRule = Qt::OddEvenFill)
   qt5使用:painter.drawPolygon(*pa, Qt::WindingFill );
85.QStatusBar中的message改为showMessage
86.QMouseEvent中的state改为button,另QMouseEvent中的stateAfter改为buttons
87.Qt::ControlButton枚举改为Qt::ControlModifier
88.QKeyEvent中的state改为modifiers
89.枚举类型Qt::arrowCursor改为Qt::ArrowCursor
           Qt::pointingHandCursor改为Qt::PointingHandCursor
   注:qt5中基本上都是第一个字母改为大写。详细参照qt助手
90.QString中的find改为indexOf 
91.原先控件能直接使用名字,现在需要在控件名称前面添加指针,
   即:ui->XXX,ui的定义为Ui::logindialog *ui;
   在构造函数处初始化
92.QComboBox的insertItem的函数参数调换位置了,即index移到了QString的前面。
   用法:
   QComboBox::insertItem(int aindex, const QString &atext,
                                  const QVariant &auserData)
93.控件在自身cpp以外使用,需要将控件指针定义为public成员,然后在构造函数中将ui->XXX赋值给public指针
94.qhbox.h改成QToolBox
95.QTreeView or QTreeWidget for tree-like structures; QListWidget or the new QListView class for one-dimensional lists.
所以请注意:
如果你的那个QListView是像树类型结构的,才用用QTreeView or QTreeWidget;如果是一列而已,就用QListWidget 或者QListView吧。
QListViewItem中的listView改为QTreeWidgetItem中的treeWidget,注:获取父widget
96.QListViewItem中的setopen改为QTreeWidgetItem中的setExpanded,注:根据bool值展开或关闭tree
97.QListViewItem中的setPixmap改为QTreeWidgetItem中的setIcon
   保留原先pixmap的用法如下:
   旧:setPixmap(0,*(fatherlist->gele_bmp));
   新:setIcon(0,*(QIcon*)(fatherlist->gele_bmp));
98.QListViewItem中的repaint暂无找到方法替换?????????
99.QTextCodec中的toUnicode函数参数由QString改成了QByteArray
   用法:
   旧:codec->toUnicode(string);
   新:codec->toUnicode(string.toLatin1());
100.QFile中的setName改为了setFileName
101.#include <qguardedptr.h> 改为 #include<qpointer.h>或者#include<QPointer>
    即:QGuardedPtr替换为QPointer
102.#include <qaccel.h>改为#include <qshortcut.h>
The QAccel class has been renamed Q3Accel and moved to the Qt3Support module. In new applications, you have three options:
You can use QAction and set a key sequence using QAction::setShortcut().
You can use QShortcut, a class that provides similar functionality to Q3Accel.
You can use QWidget::grabShortcut() and process "shortcut" events by reimplementing QWidget::event().
The Q3Accel class also supports multiple accelerators using the same object, by calling Q3Accel::insertItem() multiple times. 
In Qt 4, the solution is to create multiple QShortcut objects.


103.#include <qworkspace.h>改为#include <qmdiarea.h>
104.#include <qdragobject.h>改为#include <qdrag.h>
105.QObject中的queryList函数改为findChildren
106.QComboBox中的setCurrentItem改为setCurrentIndex,注:同样,取值函数CurrentItem改为CurrentIndex
107.QSpinBox的构造函数取消了传入最大值最小值,需要通过set函数赋值
108.QRect中的moveBy改为moveTo
109.QVariant中的toXXX()函数改为了value。
    用法:
    QVariant v;
    MyCustomStruct c;


    if (v.canConvert<MyCustomStruct>())
        c = v.value<MyCustomStruct>();


    v = 7;
    int i = v.value<int>();                        // same as v.toInt()
    QString s = v.value<QString>();                // same as v.toString(), s is now "7"
    MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
110.颜色枚举类型在使用时需要加上Qt::前缀
    用法举例:
    旧:black
    新:Qt::black
111.QSizePolicy中的horStretch改为horizontalStretch,同样verStretch改为verticalStretch
112.QPalette相关用法
    用法:
    旧:QPalette pal;
        p->setBrush( pal.active().background() );
    新:QPalette pal;
        p->setBrush( pal.color(QPalette::Active,QPalette::Background ));
113.Qt::ButtonState中的枚举类型改为Qt::KeyboardModifier中的枚举类型
114.QKeySequence类型的数据无法直接转换成QString,需要XXX.toString();
115.QWidget中的style()函数由对象型改为指针型
116.QStyle中drawComplexControl参数发生变化,详细用法参照qt助手
117.QStyle中querySubControlMetrics改为subControlRect
118.QStyle中visualRect参数作了修正
    用法:
    旧的:visualRect ( const QRect & logical, const QRect & bounding );
    新的:visualRect ( Qt::RightToLeft, const QRect & boundingRectangle, const QRect & logicalRectangle);
    根据帮助文档,确定新函数中第一个参数枚举值如上所示。
119.QWidget中的setSelectable函数改为setFlags
120.QListView中itemRect改为QTreeWidget中的visualItemRect
121.QListView中ensureItemVisible改为QTreeWidget中的scrollToItem
122.QHeader中的sectionPos改为QHeaderView中的sectionPosition
123.QMAX改为了qMax
124.QComboBox中的text函数改为itemText
125.QScrollView中的setContentsPos改为QAbstractScrollArea中的scrollContentsBy
126.QDialog的构造函数参数发生变化
    用法:
    旧的:ValueDlg* vdlg=new ValueDlg(this,"value dialog",true);
    新的:ValueDlg* vdlg = new ValueDlg(this);
 vdlg->setObjectName("value dialog");
 vdlg->setModal(true);
127.QListView重点setColumnText改为QTreeWidget中的setHeaderLabel,暂定!!!不确定!!!
128.QStringList和QString中的find和findRev改为indexOf。
129.QStringList中的remove改为removeAt
130.QListViewItem中的setVisible改为QTreeWidgetItem中的setHidden
QListViewItem的findItem,这里改为了QTreeWidgetItem中的findItems,然后在count()或者at()
131.QPixmap中的fromMimeSource改为QPixmap的构造函数,路径可能也需要改!!!!!!!
132.QComboBox中的insertStringList改为addItems或者insertItems
133.QComboBox中的pixmap改为itemIcon
134.QString中的lower改为toLower
135.QListBox改为QListWidget
136.QComboBox中的listbox()函数改为view(),然后动态转换为QListWidget可用
137.QHeader中的setMovingEnabled改为QHeaderView中的setSectionsMovable
138.QHeader中的setStretchEnabled改为QHeaderView中的setStretchLastSection
139.QListView中的addColumn改为QTreeWidget中的setHeaderLabel
140.QScrollView中的setResizePolicy改为QAbstractScrollArea中的setSizeAdjustPolicy
141.QListView中的setSorting改为QTreeWidget中的sortItems
142.QScrollView中的setHScrollBarMode改为QAbstractScrollArea中的setHorizontalScrollBarPolicy
143.QScrollView中的setVScrollBarMode改为QAbstractScrollArea中的setVerticalScrollBarPolicy
144.QTabWidget中的page函数改为widget函数
145.QTabWidget中的changeTab改为setTabText,注意参数也发生了变化
146.QTabWidget中的removePage改为removeTab,注意参数也发生了变化
147.QTabWidget中的setCurrentPage改为setCurrentIndex
148.QMetaObject中的propertyNames改为了如下用法:
    const QMetaObject* metaObject = obj->metaObject();
    QStringList methods;
    for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
        methods << QString::fromLatin1(metaObject->method(i).methodSignature());
149.QWidget中的isShown改成了isHidden,注意返回值的不同,与以前相反.
150.QTabWidget中的insertTab参数,将index参数移到了最前面
151.QString中的stripWhiteSpace改为trimmed,注意与simplified区别,
    trimmed只删除前后的空格,而simplified中间的空格也处理(不是删除空格,类似于格式化)
152.QTable中的setNumRows改为了QTableWidget中的setRowCount
153.QCheckTableItem改为QTableWidgetItem,用法如下:
    旧的:
    QCheckTableItem *tb = new QCheckTableItem(bg_group, qstr);
    tb->setChecked(FALSE);
    新的:
    QTableWidgetItem* tb = new QTableWidgetItem();
    tb->setText(qstr);
    tb->setCheckState(Qt::CheckState::Unchecked);
154.QFileDialog中的getOpenFileNames参数发生改变,用法如下:
    旧的:QStringList filenamelist = QFileDialog::getOpenFileNames("G格式图元(*.icn.g)",filename,this, "打开G格式图元文件","请选择" );
    新的:QStringList filenamelist = QFileDialog::getOpenFileNames(this,"打开G格式图元文件" , filename,"G格式图元(*.icn.g)" );
155.QString中的findRev改为lastIndexOf
156.QListViewItem中的takeItem改为QTreeWidgetItem中的removeChild
157.QMap中的data函数改为了value函数
158.QListViewItem中的depth改为QTreeWidgetItem中的type
159.QProgressBar中的setProgress改为setValue
160.QProgressBar中的setPercentageVisible改为setTextVisible
161.QProgressBar中的setTotalSteps改为setMaximum
162. 使用样式表
     pWidget->setStyleSheet("background-color:blue;");  //设置背景颜色
     pWidget->setStyleSheet("background-image:url(:/folder/show.bmp);"); //设置背景图片
163.QStatusBar中的message函数改为showMessage
164.QAction中的isOn改为isChecked, 同setOn改为setChecked
165.QTableWidget中的numRows改为rowCount
166.QTableWidget中的设置表头内容用法
    ①#ifndef QT5_UPDATE
tbDecide->horizontalHeader()->setLabel(0, "");
    #else
QStringList header;
header << "";
tbDecide->setHorizontalHeaderLabels(header);
    #endif
    ②
    #ifndef QT5_UPDATE
tbFields->horizontalHeader()->setLabel(COL_COLOR, codech->toUnicode("颜色"));
    #else
QTableWidgetItem* item1 = new QTableWidgetItem(codech->toUnicode("颜色"));
tbFields->setHorizontalHeaderItem(COL_COLOR, item1);
    #endif
167.QComboTableItem改成QComboBox,然后通过setCellWidget的方式添加到QTableWidget中,其他控件同理
168.QTable::SingleRow改成了QAbstractItemView::SelectionMode::SingleSelection
    即QTable中的关于选择的枚举类型,移到了QAbstractItemView中了
169.QTableView中的text函数,改为通过QTableWidgetItem来获取
    用法:
    #ifndef QT5_UPDATE
QString s = tbFields->text(nrow, COL_EXP);
    #else
QTableWidgetItem* item1 = tbFields->item(nrow, COL_EXP);
QString s = item1->text();
    #endif
170.QAction的响应函数activated改为triggered
171.QToolBar的用法,在toolbar中嵌入widget控件,需要调用addwidget函数
QToolBar的构建,旧版本是QToolBar ( QMainWindow * parent = 0, const char * name = 0 )
现在是explicit QToolBar(const QString &title, QWidget *parent = Q_NULLPTR);
参数位置要更换过来。
没有undock,因为qt5中继承的是QWidget.旧版本继承的是QDockWindow
172.QTreeWidget中单击击事件clicked改为itemClicked
173.在VS的输出窗口,可以显示哪些信号是不存在的,赞这个功能
174.原先旧的访问png的路径方法改变:即"item_lib.png"改为":/images/item_lib.png"
175.注意::ViewListItem的初始化函数的type参数,可能存在问题
176.QColorGroup相关已经改为使用QPalette来管理
177.关于编码:
    ①直接中文的,需要 QTextCodec *codec = QTextCodec::codecForName("GBK");
                       codec->toUnicode("实线");
178.QListView中的rightButtonPressed被删除了,
    QTreeWidget中的contextMenuEvent函数可以用来实现右击菜单,通过重载的方式实现,不需要添加信号和槽
    详细用法参照viewhelp中的用法
179.QListView中的doubleClicked改为QTreeWidget中的itemDoubleClicked
180.QListViewItem中的nextSibling方法,原先用来获取同层次的姐妹节点方法已经被删除
    现改为QTreeWidgetItem中的方法如:先获取父Item,再重新遍历父节点的子节点。用法如下:
    QTreeWidgetItem* parentItem = i->parent();
    if (NULL != parentItem)
   {
for (int index = 0; index < parentItem->childCount(); ++index)
{
XXXX;
}
    }
181.QListViewItem中的setVisible改为了QTreeWidgetItem中的setHidden,特别注意,bool值相反了。
182.QString转换成char[]时,需要使用strcpy(char,QString.toLocal8Bit());
183.QImageIO变成了QPictureIO
184.QFileDialog的构造函数参数顺序发生变化,详细参照qt助手
185.QCustomEvent改为了QEvent
186.#include<qsound.h>现在需要在pro文件中添加QT += multimedia
187.SVG文件保存中出现乱码,首先将保存的地方用 codec->toUnicode()函数转换好,
    在读取的地方将其转成QString,用toLocal8Bit函数来转换,((QString)xxx).toLocal8Bit();


188.QPaintDeviceMetrics更改为QPaintDevice。
189.QMultiLineEdit更改为QTextEdit。
190.QDockWindow更改为QDockWidget,QDockWindow::Place更改为Qt::DockWidgetArea 
191.QMap的replace(key,value),用remove(key);insert(key,value);替换。看qt3.3源码
192.QTextStream的setEncoding改为setCodec;unsetDevice()更改为setDevice(0);
193.QSettings
 bool ok;
 QString str = settings.readEntry("userName", "administrator", &ok);
更改为
 bool ok = settings.contains("userName");
 QString str = settings.value("userName", "administrator").toString();


194.QWidgetListIt也不能用了,用QWidgetList一个个for的来。
195.QObject::isA()已经被删除掉了,可以参考QObject::inherits ()。
196,Qt3:QValueList -->Qt5,QList or QLinkedList
QList<T> has an index-based API and provides very fast random access (QList::operator[]), whereas QLinkedList<T> has an iterator-based API.


197.
widget->setEraseColor(color);
更改为
QPalette palette;
palette.setColor(widget->backgroundRole(), color);
widget->setPalette(palette);


198.QProcess
addArgument()函数不存在了。
可以直接用setArguments()函数。例如
QProcess *proc = new QProcess( this );
proc->setArguments(str.split(QChar(' '),QString::SkipEmptyParts)); //pb
proc->addArgument( "-server" );
可以更改为
QStringList command_list = str.split(QChar(' '),,QString::SkipEmptyParts);
command_list << "-server";
proc->setArguments(command_list); 


199、QWidget
QWidget w;
w.name(),改为w.objectName;


200、QToolButton的setTextLabel ,改为了 setToolTip()
qt3的帮助里是这样说的:
QString textLabel
这个属性保存的是按钮的标签。 
设置这个属性会自动地设置这个文本为工具提示。 
通过setTextLabel()设置属性值并且通过textLabel()来获得属性值。 
因此,就是qt5的工具提示。


201、QKeyEvent中,鼠标和组合键的状态中ControlButton,ShiftButton,AltButton在qt5后都不存在了。Qt::ButtonState
改为了Qt::MouseButton。stateAfter()函数也不存在了。


202、
    QPalette t_back_palette;
    t_back_palette.setColor(contents->backgroundRole(), QBResources::makeColor(parameters["background"]));
    contents->setPalette(t_back_palette);


    QPalette t_fore_palette;
    t_fore_palette.setColor(contents->foregroundRole(), QBResources::makeColor(parameters["foreground"]));
    contents->setPalette(t_fore_palette);


    //contents->setPaletteBackgroundColor(QBResources::makeColor(parameters["background"]));
    //contents->setPaletteForegroundColor(QBResources::makeColor(parameters["foreground"]));

203、QTextEdit,text改为toPlainText。
204、QSpinBox,setSteps( step, 0 )改为setSingleStep(step);
205、 QHButtonGroup, or QVButtonGroup都没有了,直接用QGroupBox来替换
If you use a Q3ButtonGroup, Q3HButtonGroup, or Q3VButtonGroup as a widget 
and want to port to Qt 4, you can replace it with QGroupBox. In Qt 4, 
radio buttons with the same parent are automatically part of an exclusive group, 
so you normally don't need to do anything else. See also the section on QGroupBox below.


206、
QGridLayout *gridLayout = new QGridLayout();
label = new QLabel(this);
//qt3: addMultiCellWidget ( QWidget * w, int fromRow, int toRow, int fromCol, int toCol, int alignment = 0 ),
//旧版本:从第fromRow行,到第toRow行,从第fromCol列,到第toCol列。也就是占据(toRow-fromRow+1)行,(toCol-fromCol+1列)
//qt5:addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
//新版本,从第fromRow行,第fromColumn列开始,占据rowSpan行columnSpan列。      
// gridLayout->addMultiCellWidget(label, 0, 0, 0, 2);
    gridLayout->addWidget(label, 0, 0, 1, 3);

207、deleteAllItems()用下面的替换

QLayoutItem *child;
      while ((child = list.takeAt(i)) != 0) {
            delete child;
    }

207、提示框
// QToolTip::remove(this);
// QToolTip::add(this, title);
改为    setToolTip(title)。

208、
widget->setErasePixmap(pixmap);
可以改为
QPalette palette;
palette.setBrush(widget->backgroundRole(), QBrush(pixmap));
widget->setPalette(palette);

209、QGridLayout的addMultiCellLayout,改为addLayout。和上面的addMultiCellWidget差不多吧。

猜你喜欢

转载自blog.csdn.net/caokunchao/article/details/79853322