ROS_Qt_UI_Coding - 8BitsCoding/RobotMentor GitHub Wiki
// main_window.cpp,
// MainWindow()
ui.request_a_lineEdit->setText("Insert Number");
ui.request_b_lineEdit->setText("Insert Number");
ui.response_result_lineEdit->setEnabled(false);
ui.operator_comboBox->addItem(QString("Select Operator"));
ui.operator_comboBox->addItem(QString("PLUS"));
ui.operator_comboBox->addItem(QString("MINUS"));
ui.operator_comboBox->addItem(QString("MULTIPLICATION"));
ui.operator_comboBox->addItem(QString("DIVISION"));
ui ์ฝ๋ฉ์ SIGNAL AND SLOT์ ๊ธฐ๋ฅ์ ์์์ผ์ง ๊ฐ๋ฅํ๊ธฐ์ SIGNAL AND SLOT์ ์ค๋ช ํ ๋ค์ ์ฝ๋ฉ์ค๋ช ํจ.
QObject::connect(ui.operator_comboBox,SIGNAL(crrentIndexChanged(int)), this, SLOT(changeOperator()));
// ํด์์ ํ์๋ฉด, ui.operator_comboBox ์ค๋ธ์ ํธ์ currentIndexChange ํจ์๊ฐ ํธ์ถ๋ ์ ํ ์ค๋ธ์ ํธ(this)์ changeOperator๋ฅผ ํธ์ถํ๋ผ.
// ๋ง์ด ์ข ์ด๋ ค์ด๊ฑฐ ๊ฐ์.
// QObject::connect(<Object1 ์ฃผ์>, SIGNAL(<Object1๋ด์ ํจ์>, <Object2 ์ฃผ์>, SLOT(<Object2 ๋ด์ ํจ์>)));
// ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ๋ฉด๋๋ค.
๋จ, Object1, 2๋ชจ๋ ํจ์์ ์ ์ธ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์ธ์ ํด์ค์ผํจ.
public Q_SLOTS :
void changeOperator();
public Q_SIGNALS :
void crrentIndexChanged(int);
combobox ์ฒ๋ฆฌํ๋ ๋ถ๋ถ.
// main_window.cpp,
// MainWindow() ํจ์
QObject::connect(ui.operator_comboBox,SIGNAL(crrentIndexChanged(int)), this, SLOT(changeOperator()));
// ํด์์ ํ์๋ฉด, ui.operator_comboBox ์ค๋ธ์ ํธ์ currentIndexChange ํจ์๊ฐ ํธ์ถ๋ ์ ํ ์ค๋ธ์ ํธ(this)์ changeOperator๋ฅผ ํธ์ถํ๋ผ.
// changeOperator() ํจ์
qnode.sendSetOperator(ui.operator_comboBox->currentText().toStdString());
// main_window.hpp
public Q_SLOTS :
void changeOperator();
// ๋ค์์ฒ๋ผ connect์ ๋ฐ๋ ์ชฝ์ Q_SLOT ๋ฐ์ ๋ฃ์ด์ค์ผํจ.
// ๋ฐ๋๋ก connect์ ๋ณด๋ด๋ ์ชฝ์ Q_SIGNALS : ๋ฐ์ ๋ฃ์ด์ผํ๊ณ ๋ณด๋ผ ๋๋ Q_EMIT (ํจ์๋ช
) ์ผ๋ก ํด์ค์ผํ๋ฉฐ, ๋ฐ์์ ๋ค์ ์ค๋ช
ํจ.
// qnode.cpp,
// sendSetOperator(std::string index) ํจ์ -> SLOT์ผ๋ก ๋ฐ๋ ๋ถ๋ถ.
ros::NodeHandle n;
if(!strncmp(index.c_str(), "PLUS", 4))
n.setParam("calculation_method", 1);
if(!strncmp(index.c_str(), "MINUS", 4))
n.setParam("calculation_method", 2);
if(!strncmp(index.c_str(), "MULTIPLICATION", 4))
n.setParam("calculation_method", 3);
if(!strncmp(index.c_str(), "DIVISION", 4))
n.setParam("calculation_method", 4);
๋ฒํผ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ.
// main_window.ui
// SEND push_button์ "Go to slot โฆ" ์ฒ๋ฆฌํ๋ค.
// clicked(bool)
// main_window.cpp,
// on_send_pushButton_clicked(bool check) ํจ์
qnode.sendSetNumber(ui.request_a_lineEdit->text().toInt(), ui.request_b_lineEdit->text().toInt());
// qnode.hpp
# include <oroca_ros_tutorials_msgs/msgsTutorial.h>
// ์ฐธ๊ณ ๋ก ์์ ์ฒ๋ผ ์ธํด๋ฃจ๋ํ๋ฉด ์๋จ. qnode.hppํ์ผ์ ๊ธฐ์ค์ผ๋ก ../../ ํด์ include์ฒ๋ฆฌํด์ผํจ.
void msgCallback(const oroca_ros_tutorials_msgs::msgTutorial::ConstPtr &msg);
ros::Subscriber ros_tutorial_sub;
// qnode.cpp
// QNode::init() ํจ์ ๋ด๋ถ
ros_tutorial_sub = n.subscribe("ros_tutorial_msg", 100, &QNode::msgCallback, this);
// ...
// QNode::msgCallback(~) ํจ์ ๋ด๋ถ
std::string receiveMsg;
std::stringstream ss;
ss << "receive msg: "<< msg->data;
receiveMsg = ss.str();
log(Info, receiveMsg);
// qnode.hpp
ros::ServiceClient ros_tutorial_service_client;
//qnode.cpp,
// init() ํจ์
ros_tutorial_service_client = n.serviceClient<oroca_ros_tutorials_msgs::srvTutorial>("ros_tutorial_srv", 10);
// sendSetNumber(int res_a, int res_b) ํจ์
oroca_ros_tutorials_msgs::srvTutorial srv;
srv.request.a = res_a;
srv.request.b = res_b;
if (ros_tutorial_service_client.call(srv))
Q_EMIT updateServiceResult(srv.response.result);
// main_window.cpp
// MainWindow() ํจ์
QObject::connect(&qnode, SIGNAL(updateServiceResult(int)), this, SLOT(updateResultLineEdit(int)));
// qnode ์ค๋ธ์ ํธ์ updateServiceResult ํจ์๋ฅผ ํธ์ถ ์ ๋ณธ ์ค๋ธ์ ํธ์ updateResultLineEidt๋ฅผ ํธ์ถํ๋ผ
// main_window.hpp
void updateResultLineEdit(int srv_response);
// main_window.cpp
// updateResultLineEdit(int) ํจ์
ui.response_result_lineEdit->setText(QString::number(srv_response);