Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT insert widget inside Mainwindows by using QDesignerCustomWidgetInterface



  • Hi:

    I need to combine some examples of Qt like an analog clock into my qt mainwindow, SO I found QDesignerCustomWidgetCollectionInterface By these site instructions and this , So I have made these codes:

    test2.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2019-05-29T12:23:54
    #
    #-------------------------------------------------
    
    QT       += core gui designer charts
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = test2
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    CONFIG += c++11
    
    SOURCES += \
            #main.cpp \
            mainwindow.cpp \
            ssgraphs.cpp
    
    HEADERS += \
            mainwindow.h \
            ssgraphs.h
    
    FORMS += \
            mainwindow.ui \
            ssgraphs.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /home/pi/Desktop/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QMovie>
    #include <QTimer>
    #include <QTime>
    #include <QLabel>
    #include <QDateEdit>
    #include <QtCharts>
    #include <QBarSet>
    #include <QChartView>
    #include "ssgraphs.h"
    #include <QColor>
    #include <QtDebug>
    #include <QMainWindow>
    #include <QGraphicsPixmapItem>
    
    
    namespace Ui {
    class MainWindow;
    }
    
    #include <QDesignerCustomWidgetCollectionInterface>
    #include <QtDesigner/qtdesigner.h>
    #include <QtCore/qplugin.h>
    #include "ssgraphs.h"
    
    class MainWindow: public QObject, public QDesignerCustomWidgetCollectionInterface
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetCollectionInterface")
        Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
    
    public:
        MainWindow(QObject *parent = 0);
    
        QList<QDesignerCustomWidgetInterface*> customWidgets() const override;
    
    private:
        QList<QDesignerCustomWidgetInterface*> widgets;
    };
    
    #endif // MAINWINDOW_H
    
    

    ssgraphs.h

    #ifndef SSGRAPHS_H
    #define SSGRAPHS_H
    
    #include <QWidget>
    
    #include <QtCharts>
    #include <QChartView>
    
    #include <QBarSet>
    #include <QBarSeries>
    
    #include <QPieSeries>
    #include <QPieSlice>
    namespace Ui {
    class ssGraphs;
    }
    
    class ssGraphs : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit ssGraphs(QWidget *parent = nullptr);
        ~ssGraphs();
    
    
    
    private:
        Ui::ssGraphs *ui;
    };
    
    #endif // SSGRAPHS_H
    
    
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QImage>
    #include <QGraphicsPixmapItem>
    #include <QVBoxLayout>
    #include <QPushButton>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QPixmap>
    
    
    #define IMAGE_COUNT 99
    
    MainWindow::MainWindow(QObject *parent)
            : QObject(parent)
    {
        widgets.append(new ssGraphs(this));
        //widgets.append(new CustomWidgetTwoInterface(this));
        //widgets.append(new CustomWidgetThreeInterface(this));
    }
    
    QList<QDesignerCustomWidgetInterface*> MainWindow::customWidgets() const
    {
        return widgets;
    }
    
    

    ssgraphs.cpp

    #include "ssgraphs.h"
    #include "ui_ssgraphs.h"
    
    ssGraphs::ssGraphs(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ssGraphs)
    {
        ui->setupUi(this);
        ////////PIE CHART///////////////////////////////////////////////////////////////////////
    
            QPieSeries *series = new QPieSeries();
            series->append("Jane", 10);
            series->append("Joe", 20);
            series->append("Andy", 30);
            series->append("Barbara", 40);
            series->append("Jason", 50);
    
            QPieSlice *slice = series->slices().at(1);
            slice->setExploded();
            slice->setLabelVisible();
            slice->setPen(QPen(Qt::darkGreen, 2));
            slice->setBrush(Qt::green);
    
            QChart *chart = new QChart();
            chart->addSeries(series);
            chart->setTitle("Students Performance");
    
            QChartView *chartView = new QChartView(chart);
        chartView->setParent(ui->verticalFrame);
    }
    
    ssGraphs::~ssGraphs()
    {
        delete ui;
    }
    
    

    So in the compiler, I get these errors:

    alt text

    And

    alt text

    alt text

    /home/so/Qt_Test_Projects/test2/mainwindow.h:24: error: QtDesigner/qtdesigner.h: No such file or directory
     #include <QtDesigner/qtdesigner.h>
                                       ^
    
    /home/so/Qt_Test_Projects/test2/mainwindow.h:24: error: QtDesigner/qtdesigner.h: No such file or directory
     #include <QtDesigner/qtdesigner.h>
                                       ^
    
    /home/so/Qt_Test_Projects/test2/ssgraphs.cpp:6: error: invalid use of incomplete type 'class Ui::ssGraphs'
         ui(new Ui::ssGraphs)
                    ^~~~~~~~
    /home/so/Qt_Test_Projects/test2/ssgraphs.cpp:8: error: invalid use of incomplete type 'class Ui::ssGraphs'
         ui->setupUi(this);
           ^~
    
    /home/so/Qt_Test_Projects/test2/ssgraphs.cpp:29: error: invalid use of incomplete type 'class Ui::ssGraphs'
         chartView->setParent(ui->verticalFrame);
                                ^~
    
    /home/so/Qt_Test_Projects/test2/ssgraphs.cpp:6: error: allocation of incomplete type 'Ui::ssGraphs'
    
    
    /home/so/Qt_Test_Projects/test2/ssgraphs.cpp:8: error: member access into incomplete type 'Ui::ssGraphs'
    
    
    /home/so/Qt_Test_Projects/test2/ssgraphs.cpp:29: error: member access into incomplete type 'Ui::ssGraphs'
    
    
    

    So How could I solve this problem?

    My question link via stackoverflwo site:

    https://stackoverflow.com/questions/56777920/qt-insert-widget-inside-mainwindows-by-using-qdesignercustomwidgetinterface


  • Lifetime Qt Champion

    Hi,

    Your MainWindow inherits from QObject and not from QWidget nor QMainWindow for that matter. That's your current issue.


Log in to reply