QT通过使用QDesignerCustomWidgetInterface在Mainwindows中插入小部件

时间:2019-06-26 17:07:15

标签: c++ qt

我需要将Qt的一些示例(例如模拟时钟)组合到我的qt mainwindow中,因此我通过此site instructions发现了QDesignerCustomWidgetCollectionInterface,因此,我编写了以下代码:

  

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;
}

因此在编译器中,出现以下错误:

enter image description here

还有

enter image description here

enter image description here

/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'


那我该如何解决这个问题?

0 个答案:

没有答案