在 Qt 创建者中提升 QWidget,构造函数的问题

Posted

技术标签:

【中文标题】在 Qt 创建者中提升 QWidget,构造函数的问题【英文标题】:Promoting QWidget in Qt creator, problems with constructor 【发布时间】:2015-11-03 09:28:00 【问题描述】:

我继承了QGraphicsView

class CustomGraphicsView : public QGraphicsView

public:
    CustomGraphicsView(QWidget *parent = 0);
...

.cpp 文件中的构造函数然后是这样实现的:

CustomGraphicsView::CustomGraphicsView(QWidget * parent):
    QGraphicsView(parent)


现在我通过 Qt 创建者将 QGraphicsView Widget 提升到 CustomGraphicsView。但是当我想在 ImageWindow 类的构造函数中连接到提升的小部件时;

ImageWindow::ImageWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ImageWindow)

    ui->setupUi(this);

    CustomGraphicsView * view = ui->graphicsView();


我收到错误消息:

term does not evaluate to a function taking 0 arguments.

我为构造函数指定了一个默认值,即 QWidget *parent = 0,并且在ui_image_window.h 中设置了一个参数:

graphicsView = new CustomGraphicsView(ImageWindow);

那么什么会导致这个错误呢?

【问题讨论】:

【参考方案1】:

这是因为graphicsView 是一个成员而不是一个方法,所以你不需要括号。只需像view = ui->graphicsView 一样访问它。这对于生成的 UI 类中的所有 Qt 小部件都是一样的——它们只是成员,而不是方法。

【讨论】:

以上是关于在 Qt 创建者中提升 QWidget,构造函数的问题的主要内容,如果未能解决你的问题,请参考以下文章

在构造函数 QT Creator(继承类)中传递默认参数

Qt编程问题.

在构造函数之外延长 QT 对象的生命周期

ℰ悟透Qt┇Puzzle记录

ℰ悟透Qt┇Puzzle记录

必须在传递给 C 运行时函数的 QWidget 和无效参数之前构造一个 QApplication