Home > Software design >  How to promote QGraphicView to custom widget in .ui file?
How to promote QGraphicView to custom widget in .ui file?

Time:10-19

I need to create my own widget class and place it on a graphical form (.ui). In order to achieve this, I borrowed from the examples that come with the qt delivery this class:

#ifndef CHARTVIEW_H
#define CHARTVIEW_H

#include <QChartView>
#include <QtWidgets/QRubberBand>

QT_CHARTS_USE_NAMESPACE

//![1]
class ChartView : public QChartView
//![1]  
{
public:
    ChartView(QChart *chart, QWidget *parent = 0);

//![2]
protected:
    bool viewportEvent(QEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
//![2]

private:
    bool m_isTouching;
};

#endif

After that i checked enter image description here

In ui class sources for my widget were generated:

graphicsView_5 = new QChartView(page_5);
    graphicsView_5->setObjectName(QString::fromUtf8("graphicsView_5"));
    graphicsView_5->setGeometry(QRect(9, 9, 256, 192));
    pushButton_5 = new QPushButton(page_5);
    pushButton_5->setObjectName(QString::fromUtf8("pushButton_5"));
    pushButton_5->setGeometry(QRect(903, 264, 80, 21));
    graphicsView_6 = new ChartView(page_5);
    graphicsView_6->setObjectName(QString::fromUtf8("graphicsView_6"));
    graphicsView_6->setGeometry(QRect(470, 240, 256, 192));

My header file is included with above class:

#include <chartview.h>
//...
ChartView *graphicsView_6;
//...
class Ui_Form
{
public:

But with build i get following error:

Cannot convert argument 'QWidget *' to 'QtCharts :: QChart *'

How to fix it?

CodePudding user response:

As you write in your ChartView class constructor, ChartView needs to get QChart as its parent. when you are trying to promote it you didn't set QChart as the parent of ChartView. so it didn't display.

and the second mistake is that you choose QWidget and you are trying to promote the chart view to it, choose one GraphicsView Item instead of QWidget.

But you shouldn't add your chart by promoting.

enter image description here

  • Related