Search code examples
c++qtqt-creatorqwidget

Promoting QWidget in Qt creator, problems with constructor


I subclassed QGraphicsView:

class CustomGraphicsView : public QGraphicsView
{
public:
    CustomGraphicsView(QWidget *parent = 0);
...
}

The constructor in the .cpp file is then implemented like this:

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

Now I promoted a QGraphicsView Widget via a Qt creator to CustomGraphicsView. But when I want to connect to the promoted widget in the constructor of my ImageWindow class;

ImageWindow::ImageWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ImageWindow)
{
    ui->setupUi(this);

    CustomGraphicsView * view = ui->graphicsView();

}

I get the error message:

term does not evaluate to a function taking 0 arguments.

I specified a default value for the constructor namely QWidget *parent = 0, and in ui_image_window.h an argument is set:

graphicsView = new CustomGraphicsView(ImageWindow);

So what could cause this error then?


Solution

  • This is because graphicsView is a member rather than a method, so you do not need the parentheses. Just access it like view = ui->graphicsView. This is the same for all Qt widgets in your generated UI class - they are just members, not methods.