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

标签 c++ qt qt5

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

在 .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);
}

.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 对 3) virtual void setupUI(const QMap &columnNames_, QWidget *parent_ = 0); ^

../src/ui/frmMainTableView_UI.cpp:24:30: 错误:“QHeaderView”中没有名为“setResizeMode”的成员;您是指“sectionResizeMode”吗? table->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);//JDL Error convert Qt4 to 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”吗? table->horizo​​ntalHeader()->setResizeMode(QHeaderView::ResizeToContents);//JDL Error convert Qt4 to 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 个错误 make: *** [frmMainTableView_UI.o] 错误 1 18:29:48:进程“/usr/bin/make”以代码 2 退出。 构建/部署项目 mypersonalindex 时出错(套件:桌面 Qt 5.8.0 clang 64 位) 执行步骤“Make”时

Qt5 文档提到了QHeaderView 的过时成员 QHeaderView 类的以下成员已过时。提供它们是为了保持旧源代码的工作。我们强烈建议不要在新代码中使用它们。

(过时的)void setResizeMode(ResizeMode mode)

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

最佳答案

我猜你需要替换两条过时的线:

table->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);  
table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); 

使用以下 Qt 5 代码:

table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); 
table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); 

参见 docs .

关于c++ - 'setResizeMode' 中没有名为 'QHeaderView' 的成员 - 将 Qt 4.7 转换为 Qt 5.8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42743141/

相关文章:

c++ - 如何使用 qt5 在按键上将一条线旋转 45 度?

qt - 将 Qt 4.8 转换为 5 QPluginLoader->instance 返回 null

compiler-errors - 在64位计算机上编译静态32位Qt

c++ - 使用 QPixmap 填充 QWidget 的有效方法

c++ - 为什么有两个不同的 getline() 函数(如果确实有)?

c++ - Visual C++ 中的 _32_ 和 _64_ 等价物

c++ - std::vector<T> 是否只包含它需要的元素的确切数量或额外的空间?

C++ 构建系统 - 使用什么?

c++ - 如何在 Qt/C++ 中创建属性绑定(bind)?

c++ - 将 C++ 代码( native 客户端)移植到浏览器(Web 应用程序)