Important: Please read the Qt Code of Conduct -

[SOLVED] QGraphicsView promotion

  • Hi.
    I have created child class inherited from QGraphicsView. It works fine when I adding it to my MainWindow manually in editor. But when I'm trying to promote it from QGraphicsView in form designer, my application doesn't show it (all other elements on form work). Can anybody help me to solve this problem?

  • Moderators

    Hi, welcome to devnet.

    Subclassing is not enough for a custom widget to show up in the editor. It's ok to promote to it but if you want the editor to show your custom widget you need to create a "designer plugin": for that widget.

  • Sorry, I said imprecisely. I launch the application I'm working on, it's window contains some buttons, menu toolbar & my object which inherited from QGraphicsView. All elements except my class' object are visible on window and work. More shortly) My object is visible in editor but not visible when I launch program. I know about designer plugin, but it is another problem.

  • Moderators

    It's hard to say without more details. What's the layout situation? Does your class set any sizes, size policies, visibility flags etc. If it's possible provide some code samples.

  • I don't set any object properties. I tried to set isEnabled(true), it's not helped. Also there is no any layouts which I set manually.
    My class is quiet simple. Here is header:
    @#ifndef HEXVIEW_H
    #define HEXVIEW_H

    #include <QGraphicsView>
    #include <QObject>

    class HexView : public QGraphicsView
    HexView(QObject *parent = 0);

    void wheelEvent(QWheelEvent *event);

    private slots:
    void setupMatrix();

    int viewZoom;

    #endif // HEXVIEW_H

    And source:
    @#include "hexview.h"
    #include <QDebug>
    #include <QWheelEvent>
    #include <qmath.h>

    #define D qDebug()

    HexView::HexView(QObject *parent) :

    void HexView::wheelEvent(QWheelEvent *event)
    viewZoom += (event->delta() / 40);

    void HexView::setupMatrix()
    qreal scale = qPow( qreal(2), viewZoom / qreal(50) );
    QMatrix matrix;
    matrix.scale(scale, scale);

    In mainwindow.cpp:
    @scene = new HexScene ();

    ! form)!


  • Moderators

    One error is that you're not passing the parent parameter to the QGraphicsView constructor.
    Apart from that you're setting some custom scene. Does it have a transparent background brush? Is it the same if you don't set scene at all?

    EDIT: And are you sure the widget is not there? Maybe just the color is different. Try to draw something.

  • If I understand it correctly parent parameter is set to pass automatically, here is a part of ui_mainwindow.h related with graphicsView object:
    @graphicsView = new HexView(centralWidget);
    graphicsView->setGeometry(QRect(110, 110, 581, 361));@

    Yes, I'm creating custom scene too. These code:
    @qDebug() << endl << scene->backgroundBrush();@

    says: "QBrush(QColor(ARGB 1, 0, 0, 0) , NoBrush ) "
    I also tried to set my brush with scene->setBackgroundBrush() - no changes.
    Also, nothing is changed when I had set no scene for graphicsView.

    There is a screenshot with standard QGraphicsView, I didn't change any properties.

    Anyway, thanks for your help Chris.

  • Moderators

    bq. If I understand it correctly parent parameter is set to pass automatically

    There's no such thing in c++. How is compiler suppose to know something like that?

    @HexView::HexView(QObject *parent) : QGraphicsView(), viewZoom(0) { @
    you are explicitly calling a parameterless version of the QGraphicsView base constructor which gets nullptr as a parent. You should pass here what you get in the parameter of your constructor:
    @HexView::HexView(QObject *parent) : QGraphicsView(parent), viewZoom(0) { @
    This is always true when inheriting QObjects. What you've got is a real error, but I doubt it's the cause of this particular problem.

    Well there are no miracles so there must be some cause behind this. I just tested it and it works as expected. Can you maybe pack the whole project (or just stripped to the reproducible minimum) and share it somewhere?

  • It seems you are right. However an error occurs with your proposition:
    "error: call of overloaded 'QGraphicsView(QObject*&)' is ambiguous",
    "no known conversion for argument 1 from 'QObject*' to 'const QGraphicsView&'"
    That error points on my poor c++ understanding.
    I shall continue this number tomorrow. Thanks again, Chris.

  • Moderators

    Ah yes, I missed it previously. The QGraphicsView is actually a QWidget, so it's a more derived class of QObject. Therefore your constructor signature is wrong.

    It should be
    //.h file
    HexView(QWidget *parent = 0);

    //.cpp file
    HexView::HexView(QWidget *parent) : QGraphicsView(parent), viewZoom(0) {

    With this error I can actually reproduce your problem so fixing the constructor signature should fix the issue.

  • You are completely right. Problem solved.

Log in to reply