0

我使用 Qt 设计器创建了一个人工地平线仪器。我使用 pyuic 将 .ui 文件转换为 python。转换后的 python 文件依赖于 C++ 和 .h 文件。但是c++和.h文件中的头文件在vscode中是不兼容的。

下面是我的python代码:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.widget_2 = QtWidgets.QWidget(self.centralwidget)
        self.widget_2.setObjectName("widget_2")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.widget_2)
        self.gridLayout_2.setObjectName("gridLayout_2")
        spacerItem = QtWidgets.QSpacerItem(20, 460, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_2.addItem(spacerItem, 3, 0, 1, 1)
        self.label = QtWidgets.QLabel(self.widget_2)
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)
        self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.widget_2)
        self.doubleSpinBox.setMinimum(-90.0)
        self.doubleSpinBox.setMaximum(90.0)
        self.doubleSpinBox.setObjectName("doubleSpinBox")
        self.gridLayout_2.addWidget(self.doubleSpinBox, 0, 1, 1, 1)
        self.doubleSpinBox_2 = QtWidgets.QDoubleSpinBox(self.widget_2)
        self.doubleSpinBox_2.setMinimum(-180.0)
        self.doubleSpinBox_2.setMaximum(180.0)
        self.doubleSpinBox_2.setObjectName("doubleSpinBox_2")
        self.gridLayout_2.addWidget(self.doubleSpinBox_2, 1, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.widget_2)
        self.label_2.setObjectName("label_2")
        self.gridLayout_2.addWidget(self.label_2, 1, 0, 1, 1)
        self.gridLayout_3.addWidget(self.widget_2, 0, 0, 1, 1)
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setObjectName("widget")
        self.gridLayout = QtWidgets.QGridLayout(self.widget)
        self.gridLayout.setObjectName("gridLayout")
        spacerItem1 = QtWidgets.QSpacerItem(20, 102, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem1, 0, 1, 1, 1)
        spacerItem2 = QtWidgets.QSpacerItem(164, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem2, 1, 0, 1, 1)
        self.graphicsEADI = qfi_EADI(self.widget)
        self.graphicsEADI.setEnabled(False)
        self.graphicsEADI.setMinimumSize(QtCore.QSize(300, 300))
        self.graphicsEADI.setMaximumSize(QtCore.QSize(300, 300))
        self.graphicsEADI.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.graphicsEADI.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.graphicsEADI.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.graphicsEADI.setInteractive(False)
        self.graphicsEADI.setObjectName("graphicsEADI")
        self.gridLayout.addWidget(self.graphicsEADI, 1, 1, 1, 1)
        spacerItem3 = QtWidgets.QSpacerItem(163, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem3, 1, 2, 1, 1)
        spacerItem4 = QtWidgets.QSpacerItem(20, 101, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem4, 2, 1, 1, 1)
        self.gridLayout_3.addWidget(self.widget, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "Pitch"))
        self.label_2.setText(_translate("MainWindow", "Roll"))
        
from qfi.qfi_EADI import qfi_EADI        #it needs to import qfi_EADI which is a cpp file


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

下面是我的 qfi_EADI cpp 文件

#include <qfi/qfi_AI.h>

#ifdef WIN32
#   include <float.h>
#endif

#include <cmath>     //gives error

////////////////////////////////////////////////////////////////////////////////

qfi_AI::qfi_AI( QWidget *parent ) :
    QGraphicsView ( parent ),

    _scene ( Q_NULLPTR ),

    _itemBack ( Q_NULLPTR ),
    _itemFace ( Q_NULLPTR ),
    _itemRing ( Q_NULLPTR ),
    _itemCase ( Q_NULLPTR ),

    _roll  ( 0.0 ),
    _pitch ( 0.0 ),

    _faceDeltaX_new ( 0.0 ),
    _faceDeltaX_old ( 0.0 ),
    _faceDeltaY_new ( 0.0 ),
    _faceDeltaY_old ( 0.0 ),

    _scaleX ( 1.0 ),
    _scaleY ( 1.0 ),

    _originalHeight ( 240 ),
    _originalWidth  ( 240 ),

    _originalPixPerDeg ( 1.7 ),

    _originalAdiCtr ( 120.0 , 120.0 ),

    _backZ ( -30 ),
    _faceZ ( -20 ),
    _ringZ ( -10 ),
    _caseZ (  10 )
{
    reset();

    _scene = new QGraphicsScene( this );
    setScene( _scene );

    _scene->clear();

    init();
}

////////////////////////////////////////////////////////////////////////////////

qfi_AI::~qfi_AI()
{
    if ( _scene != Q_NULLPTR )
    {
        _scene->clear();
        delete _scene;
        _scene = Q_NULLPTR;
    }

    reset();
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::reinit()
{
    if ( _scene )
    {
        _scene->clear();

        init();
    }
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::redraw()
{
    if ( isVisible() )
    {
        updateView();

        _faceDeltaX_old  = _faceDeltaX_new;
        _faceDeltaY_old  = _faceDeltaY_new;
    }
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::setRoll( double roll )
{
    _roll = roll;

    if ( _roll < -180.0 ) _roll = -180.0;
    if ( _roll >  180.0 ) _roll =  180.0;
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::setPitch( double pitch )
{
    _pitch = pitch;

    if ( _pitch < -25.0 ) _pitch = -25.0;
    if ( _pitch >  25.0 ) _pitch =  25.0;
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::resizeEvent( QResizeEvent *event )
{
    ////////////////////////////////////
    QGraphicsView::resizeEvent( event );
    ////////////////////////////////////

    reinit();
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::init()
{
    _scaleX = static_cast< double >( width()  ) / static_cast< double >( _originalWidth  );
    _scaleY = static_cast< double >( height() ) / static_cast< double >( _originalHeight );

    reset();

    _itemBack = new QGraphicsSvgItem( ":/qfi/images/ai/ai_back.svg" );
    _itemBack->setCacheMode( QGraphicsItem::NoCache );
    _itemBack->setZValue( _backZ );
    _itemBack->setTransform( QTransform::fromScale( _scaleX, _scaleY ), true );
    _itemBack->setTransformOriginPoint( _originalAdiCtr );
    _scene->addItem( _itemBack );

    _itemFace = new QGraphicsSvgItem( ":/qfi/images/ai/ai_face.svg" );
    _itemFace->setCacheMode( QGraphicsItem::NoCache );
    _itemFace->setZValue( _faceZ );
    _itemFace->setTransform( QTransform::fromScale( _scaleX, _scaleY ), true );
    _itemFace->setTransformOriginPoint( _originalAdiCtr );
    _scene->addItem( _itemFace );

    _itemRing = new QGraphicsSvgItem( ":/qfi/images/ai/ai_ring.svg" );
    _itemRing->setCacheMode( QGraphicsItem::NoCache );
    _itemRing->setZValue( _ringZ );
    _itemRing->setTransform( QTransform::fromScale( _scaleX, _scaleY ), true );
    _itemRing->setTransformOriginPoint( _originalAdiCtr );
    _scene->addItem( _itemRing );

    _itemCase = new QGraphicsSvgItem( ":/qfi/images/ai/ai_case.svg" );
    _itemCase->setCacheMode( QGraphicsItem::NoCache );
    _itemCase->setZValue( _caseZ );
    _itemCase->setTransform( QTransform::fromScale( _scaleX, _scaleY ), true );
    _scene->addItem( _itemCase );

    centerOn( width() / 2.0 , height() / 2.0 );

    updateView();
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::reset()
{
    _itemBack = Q_NULLPTR;
    _itemFace = Q_NULLPTR;
    _itemRing = Q_NULLPTR;
    _itemCase = Q_NULLPTR;

    _roll  = 0.0;
    _pitch = 0.0;

    _faceDeltaX_new = 0.0;
    _faceDeltaX_old = 0.0;
    _faceDeltaY_new = 0.0;
    _faceDeltaY_old = 0.0;
}

////////////////////////////////////////////////////////////////////////////////

void qfi_AI::updateView()
{
    _scaleX = static_cast< double >( width()  ) / static_cast< double >( _originalWidth  );
    _scaleY = static_cast< double >( height() ) / static_cast< double >( _originalHeight );

    _itemBack->setRotation( - _roll );
    _itemFace->setRotation( - _roll );
    _itemRing->setRotation( - _roll );

    double roll_rad = M_PI * _roll / 180.0;

    double delta  = _originalPixPerDeg * _pitch;

    _faceDeltaX_new = _scaleX * delta * sin( roll_rad );
    _faceDeltaY_new = _scaleY * delta * cos( roll_rad );

    _itemFace->moveBy( _faceDeltaX_new - _faceDeltaX_old, _faceDeltaY_new - _faceDeltaY_old );

    _scene->update();
}

下面是我的 qfi_AI.h 文件

#ifndef QFI_AI_H
#define QFI_AI_H

////////////////////////////////////////////////////////////////////////////////

#include <QGraphicsView>        //gives error
#include <QGraphicsSvgItem>     //gives error

////////////////////////////////////////////////////////////////////////////////

/**
 * @brief Attitude Indicator widget class.
 */
class qfi_AI : public QGraphicsView
{
    Q_OBJECT

public:

    /** Constructor. */
    explicit qfi_AI( QWidget *parent = Q_NULLPTR );

    /** Destructor. */
    virtual ~qfi_AI();

    /** Reinitiates widget. */
    void reinit();

    /** Refreshes (redraws) widget. */
    void redraw();

    /** @param roll angle [deg] */
    void setRoll( double roll );

    /** @param pitch angle [deg] */
    void setPitch( double pitch );

protected:

    /** */
    void resizeEvent( QResizeEvent *event );

private:

    QGraphicsScene *_scene;

    QGraphicsSvgItem *_itemBack;
    QGraphicsSvgItem *_itemFace;
    QGraphicsSvgItem *_itemRing;
    QGraphicsSvgItem *_itemCase;

    double _roll;
    double _pitch;

    double _faceDeltaX_new;
    double _faceDeltaX_old;
    double _faceDeltaY_new;
    double _faceDeltaY_old;

    double _scaleX;
    double _scaleY;

    const int _originalHeight;
    const int _originalWidth;

    const double _originalPixPerDeg;

    QPointF _originalAdiCtr;

    const int _backZ;
    const int _faceZ;
    const int _ringZ;
    const int _caseZ;

    void init();

    void reset();

    void updateView();
};

////////////////////////////////////////////////////////////////////////////////

#endif // QFI_AI_H

如何摆脱头文件问题,我应该单独导入每个头文件的源代码还是有快捷方式?

4

0 回答 0