'QHeaderView'中没有名为'setResizeMode'的成员 - 将Qt 4.7转换为Qt 5.8

bul*_*lag 4 c++ qt qt5

我需要将Qt遗留代码从4.7转换为5.8,我在Qt Creator 4.2.1 Clang 7.0(Apple)64bit中有编译错误.

查看.cpp文件

#include "frmMainTableView_UI.h"
#include <QHeaderView>

void frmMainTableView_UI::setupUI(const QMap<int, QString> &columnNames_, bool hasRowLabels_, QWidget *parent_)
{
    widget = new QWidget(parent_);

    layout = new QVBoxLayout(widget);
    layout->setSpacing(0);
    layout->setMargin(1);

    frmMainToolbar_UI::setupUI(columnNames_, widget);

    tableSplitter = new QSplitter(widget);

    table = new mpiTableView(hasRowLabels_, widget);
    tableCopy = new QShortcut(Qt::CTRL + Qt::Key_C, table);
    if (!hasRowLabels_)
        table->verticalHeader()->hide();
    table->setSelectionMode(QAbstractItemView::ExtendedSelection);
    table->setSelectionBehavior(QAbstractItemView::SelectRows);
    table->setAlternatingRowColors(true);
    table->horizontalHeader()->setHighlightSections(false);
    table->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);  // Error convert Qt4 to Qt5 ??
    table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);  // Error convert Qt4 to Qt5  ??
    tableSplitter->addWidget(table);
    tableSplitter->setStretchFactor(0, 3);

    layout->addWidget(toolbar);
    layout->addWidget(tableSplitter);
}
Run Code Online (Sandbox Code Playgroud)

.cpp中的2个错误

在../src/ui/frmMainTableView_UI.cpp:1中包含的文件:.../src /ui/frmMainTableView_UI.h:21:18:警告:'frmMainTableView_UI :: setupUI'隐藏重载的虚函数[-Woverloaded-virtual] virtual void setupUI(const QMap&columnNames_,bool hasRowLabels_,QWidget*parent_ = 0); ^

../src/ui/frmMainToolbar_UI.h:31:18:注意:隐藏的重载虚函数'frmMainToolbar_UI :: setupUI'在这里声明:不同数量的参数(2 vs 3)virtual void setupUI(const QMap&columnNames_,QWidget*parent_ = 0); ^

../src/ui/frmMainTableView_UI.cpp:24:30:错误:'QHeaderView'中没有名为'setResizeMode'的成员 ; 你是说'sectionResizeMode'吗?表- > verticalHeader() - > setResizeMode(QHeaderView :: ResizeToContents); // JDL错误将Qt4转换为Qt5 ?? ^ ~~~~~~~~~~~~ sectionResizeMode /Users/john/Qt/5.8/clang_64/lib/QtWidgets.framework/Headers/qheaderview.h:133:16:注意:'sectionResizeMode'在这里声明ResizeMode sectionResizeMode (int logicalIndex)const; ^

../src/ui/frmMainTableView_UI.cpp:25:32:错误:'QHeaderView'中没有名为'setResizeMode'的成员 ; 你是说'sectionResizeMode'吗?表- > horizo​​ntalHeader() - > setResizeMode(QHeaderView :: ResizeToContents); // JDL错误将Qt4转换为Qt5 ?? ^ ~~~~~~~~~~~~ sectionResizeMode /Users/john/Qt/5.8/clang_64/lib/QtWidgets.framework/Headers/qheaderview.h:133:16:注意:'sectionResizeMode'在这里声明ResizeMode sectionResizeMode (int logicalIndex)const; ^

生成1个警告和2个错误 :***[frmMainTableView_UI.o]错误1 18:29:48:进程"/ usr/bin/make"退出代码2.构建/部署项目mypersonalindex时出错(套件:桌面) Qt 5.8.0 clang 64bit)执行步骤"Make"时

Qt5 Docs提到了QHeaderView过时成员QHeaderView类的以下成员已经过时.它们用于保持旧的源代码工作.我们强烈建议不要在新代码中使用它们.

(已废弃)void setResizeMode(ResizeMode模式)

我的C++技能非常有限,您是否看到任何可以将其从Qt4转换为Qt5的小调整.......那么替代品是什么?

dem*_*lus 8

我想你需要更换两条过时的线:

table->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);  
table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); 
Run Code Online (Sandbox Code Playgroud)

使用以下Qt 5代码:

table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); 
table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); 
Run Code Online (Sandbox Code Playgroud)

查看文档.