Попытка сделать функцию доступа в qt - PullRequest
4 голосов
/ 31 мая 2019

вы сможете увидеть, что я пытаюсь сделать, посмотрев на функцию, называемую motorcontrol. Я пытаюсь создать функцию, которая может устанавливать значения нескольких спин-боксов с помощью вызова одной функции, называемой motorcontrol. Если я поставлю функциювнутри:

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
} 

Я получаю сообщение об ошибке, что не могу объявить функцию там, но если поставить ее за скобки, я получаю сообщение об ошибке, которое не было объявлено в этой области.Я понимаю, почему я получаю эту ошибку, но я не знаю, как ее обойти.Могу ли я сделать UI глобальной переменной / объектом, и если да, то как мне это сделать, или есть другой способ решить / обойти эту проблему.вот мой код:

#include "widget.h"
#include "ui_widget.h"
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QVBoxLayout>
#include <QMenu>
#include <QAction>
#include <QFileDialog>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    mCamera = new QCamera(this);
    mCameraViewfinder = new QCameraViewfinder(this);
    mCameraImageCapture = new QCameraImageCapture(mCamera,this);
    mLayout = new QVBoxLayout;
    mOptionsMenu = new QMenu("Options",this);
    mCameraAction = new QAction("Camera",this);
    mGantryAction = new QAction("Gantry", this);
    mCaptureAction = new QAction("Capture",this);
    mLazerAction = new QAction("Lazer",this);
    mOptionsMenu->addActions({mCameraAction,mGantryAction,
                               mLazerAction});
    ui->optionsPushButton->setMenu(mOptionsMenu);
    ui->lazerDistance->setLayout(mLayout);

    ui->xAxis->setLayout(mLayout);
    ui->yAxis->setLayout(mLayout);
    ui->zAxis->setLayout(mLayout);
    ui->xRotate->setLayout(mLayout);
    ui->yRotate->setLayout(mLayout);
    ui->zRotate->setLayout(mLayout);
    ui->x1Label->setLayout(mLayout);
    ui->y1Label->setLayout(mLayout);
    ui->z1Label->setLayout(mLayout);
    ui->x2Label->setLayout(mLayout);
    ui->y2Label->setLayout(mLayout);
    ui->z2Label->setLayout(mLayout);
    ui->xAxis->hide();
    ui->yAxis->hide();
    ui->zAxis->hide();
    ui->xRotate->hide();
    ui->yRotate->hide();
    ui->zRotate->hide();
    ui->x1Label->hide();
    ui->y1Label->hide();
    ui->z1Label->hide();
    ui->x2Label->hide();
    ui->y2Label->hide();
    ui->z2Label->hide();
    ui->lazerDistance->hide();
    ui->scanButton->hide();
    ui->moterL->hide();
    ui->moterLLable->hide();

    connect(mCameraAction, &QAction::triggered, [&](){
        ui->moterL->hide();
        ui->moterLLable->hide();
        ui->xAxis->hide();
        ui->yAxis->hide();
        ui->zAxis->hide();
        ui->xRotate->hide();
        ui->yRotate->hide();
        ui->zRotate->hide();
        ui->x1Label->hide();
        ui->y1Label->hide();
        ui->z1Label->hide();
        ui->x2Label->hide();
        ui->y2Label->hide();
        ui->z2Label->hide();
        ui->lazerDistance->hide();
        ui->scanButton->hide();
        mCamera->setViewfinder(mCameraViewfinder);
        mLayout->addWidget(mCameraViewfinder);
        mLayout->setMargin(0);
        ui->ScrollArea->setLayout(mLayout);
        ui->ScrollArea->show();
        ui->scrollAreaWidgetContents->show();
        mCameraViewfinder->show();
        mCamera->start();

      });

        connect(mGantryAction,&QAction::triggered, [&](){
           mCamera->stop();
         ui->ScrollArea->hide();
          ui->scrollAreaWidgetContents->hide();
          mCameraViewfinder->hide();
          ui->lazerDistance->hide();
          ui->scanButton->hide();



            ui->x1Label->show();
            ui->y1Label->show();
            ui->z1Label->show();
            ui->x2Label->show();
            ui->y2Label->show();
            ui->z2Label->show();
            ui->moterLLable->show();
            ui->xAxis->show();
            ui->yAxis->show();
            ui->zAxis->show();
            ui->xRotate->show();
            ui->yRotate->show();
            ui->zRotate->show();
            ui->moterL->show();

            ui->scanButton->show();




        });

    connect(mLazerAction, &QAction::triggered, [&](){



        mCamera->stop();
        ui->ScrollArea->hide();
        ui->scrollAreaWidgetContents->hide();
        mCameraViewfinder->hide();
        ui->scanButton->hide();
        ui->moterL->hide();
        ui->xAxis->hide();
        ui->yAxis->hide();
        ui->zAxis->hide();
        ui->xRotate->hide();
        ui->yRotate->hide();
        ui->zRotate->hide();
        ui->x1Label->hide();
        ui->y1Label->hide();
        ui->z1Label->hide();
        ui->x2Label->hide();
        ui->y2Label->hide();
        ui->z2Label->hide();
        ui->moterLLable->hide();
        ui->lazerDistance->show();






    });

    //to change the value of the lazer distance
    //have ros input a new string
    ui->lazerDistance->setText("0");
    //funtions to set the values of the moters




    }


void motorcontroll(double x1,double x2, double y1, double y2, double z1, double z2,double l1)
{
    ui->xAxis->setValue(x1);
    ui->yAxis->setValue(y1);
    ui->zAxis->setValue(z1);
    ui->xRotate->setValue(x2);
    ui->yRotate->setValue(y2);
    ui->zRotate->setValue(z2);
    ui->moterL->setValue(l1);
}

заголовочный файл:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}
class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
class QVBoxLayout;
class QMenu;
class QAction;





class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    void motorcontroll(double x1,double x2, double y1, double y2, double z1, double z2,double l1);



private:
    Ui::Widget *ui;
    QCamera * mCamera;
    QCameraViewfinder *mCameraViewfinder;
    QCameraImageCapture *mCameraImageCapture;
    QVBoxLayout *mLayout;
    QMenu *mOptionsMenu;
    QAction *mCameraAction;
    QAction *mGantryAction;
    QAction *mCaptureAction;
    QAction *mLazerAction;
};

#endif // WIDGET_H

Ответы [ 2 ]

3 голосов
/ 31 мая 2019

Вам необходимо сделать motorcontroll() функцией-членом класса Widget. Затем в файле cpp внедрите его как член Widget:

void Widget::motorcontroll(double x1, double x2, double y1, double y2,
                           double z1, double z2, double l1)
{
    // ...
}
2 голосов
/ 31 мая 2019

Я получаю сообщение об ошибке, что не могу объявить функцию там, но если я поставлю ее за скобки, я получу сообщение об ошибке, пользовательский интерфейс не был объявлен в этой области.

Member functions может быть определено в определении класса или отдельно с помощью оператора разрешения области действия, ::. Определение функции-члена в определении класса объявляет функцию встроенной, даже если вы не используете встроенный спецификатор.

class XYZ {
public:
    void mymethod(int arg)
    {
        // Doing Something
    }
};

Вы можете определить ту же функцию вне класса, используя оператор разрешения области видимости (::).

void Widget::motorcontroll(double x1, double x2, double y1, double y2,
                       double z1, double z2, double l1)
{
    // Do Something
}

Вы можете взглянуть на эту ссылку, чтобы узнать больше о разнице между функциями-членами и свободными функциями в C++.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...