尝试在 qt 中建立访问器功能
Trying to make an accessor funtion in qt
你应该能够通过查看称为 motorcontroll 的功能来了解我要做什么 我正在尝试制作一个功能,该功能可以通过调用一个名为 motorcontrol 的功能来设置几个旋转盒的值 如果我把函数放在:
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
}
我收到错误,我无法在那里声明一个函数,但是如果我把它放在括号之外,我得到错误 ui 没有在这个范围内声明。我明白为什么我会收到此错误,但我不知道解决方法。我可以将 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
您需要
使motorcontroll()
成为Widget
类的成员函数。然后在 cpp 文件中将其实现为 Widget
的成员:
void Widget::motorcontroll(double x1, double x2, double y1, double y2,
double z1, double z2, double l1)
{
// ...
}
我收到错误,我无法在那里声明一个函数,但是如果我把它放在括号之外,我得到错误 ui 没有在这个范围内声明。
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++
之间的区别的更多信息。
相关文章:
- 如何从其他功能C++访问参数?
- 如何使用 MPI 的远程内存访问 (RMA) 功能并行化数据聚合?
- 访问我以前可以访问的功能时出现分段错误
- 好友功能 - 成员无法访问
- 如何启用友元类的友元功能直接在C++中访问其私有成员
- 无法从好友功能访问类的私有成员?"ostream"不是"std"的成员?
- 无法访问公共功能? 没有指定成员?
- 好友功能仍无法访问私人会员
- 我无法获得好友会员功能以实际访问私人会员
- GPU设备函数如何访问主机功能中定义的对象
- 派生类中的成员字段别名(无访问器功能)
- 从C文件访问C 类成员功能
- 访问嵌套结构的数据成员并将结构传递到功能C
- 使用宏访问功能
- QT:类的访问功能,该功能使用构造函数外的GUI小部件
- 在其他源文件中没有通用的头文件访问功能
- 这是使用访问功能的好习惯
- 从C 访问功能环境中的LUA变量
- 如何在右键单击链接并从上下文菜单访问功能后获取链接的 DOM
- QObject信号和插槽未访问功能