Cthulhu
23rd July 2015, 20:48
I've started learning Qt with C++ by writning simple GUI. At the beginning, after I had learnt mechanism of signals and slots I've decided to write program which gives us ability to control industrial robot arm. So the idea is simple: We've 6 button and depnds on which one we pressed then appear a text which comment what have we done for example: "Arm moved to the left". I' am going to build it up but first I have some questions to you. Here is my code:
Arm.h:
#ifndef ARM_H
#define ARM_H
#include <QVector>
#include <QString>
#include <QLabel>
class Arm{
public:
Arm();
static void displayMoves(QLabel *ptrQLabel); //function for display QString listMoves
QVector<bool(*)(void)> vctrMovesFun; //contains pointers for function which defines moves of industrial robot
private:
static QString listMoves; //contain every move which industrial robot has done
static bool moveArmForward();
static bool moveArmBackward();
static bool moveArmLeft();
static bool moveArmRight();
static bool spinArmLeft();
static bool spinArmRight(); //all this functions define moves of robot's arm
};
#endif // ARM_H
Arm.cpp:
#include "arm.h"
QString Arm::listMoves = ""; //empty string
//************************************************** *************
Arm::Arm(){
vctrMovesFun = {&moveArmForward, &moveArmBackward, &moveArmLeft,
&moveArmRight, &spinArmLeft, &spinArmRight}; //set reference to functions
}
//************************************************** *************
bool Arm::moveArmForward(){
listMoves+= "Arm moved forward\n";
return true;}
//************************************************** *************
bool Arm::moveArmBackward(){
listMoves+= "Arm moved backward\n";
return true;}
//************************************************** *************
bool Arm::moveArmLeft(){
listMoves+= "Arm moved to the left\n";
return true;}
//************************************************** *************
bool Arm::moveArmRight(){
listMoves+= "Arm moved to the right\n";
return true;}
//************************************************** *************
bool Arm::spinArmLeft(){
listMoves+= "Arm spinned to the left\n";
return true;}
//************************************************** *************
bool Arm::spinArmRight(){
listMoves+= "Arm spinned to the right\n";
return true;}
//************************************************** *************
void Arm::displayMoves(QLabel *ptrQLabel){
ptrQLabel -> setText(listMoves);
}
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "arm.h"
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
class MainWindow;}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QPushButton *button0;
QPushButton *button1;
QPushButton *button2;
QPushButton *button3;
QPushButton *button4;
QPushButton *button5;
QLabel *label;
Arm arm;
private slots:
void useVector0();
void useVector1();
void useVector2();
void useVector3();
void useVector4();
void useVector5();
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui -> setupUi(this);
this -> setGeometry(0,0,800,700);
this -> setStyleSheet("background-color:rgb(188, 198 ,204)");
button0 = new QPushButton("Move forward", this);
button0 -> setGeometry(50,50, 100,50);
button0 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button0, SIGNAL (clicked()), this, SLOT (useVector0()));
button1 = new QPushButton("Move backward", this);
button1 -> setGeometry(50,150, 100,50);
button1 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button1, SIGNAL (clicked()), this, SLOT (useVector1()));
button2 = new QPushButton("Move left", this);
button2 -> setGeometry(50,250, 100,50);
button2 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button2, SIGNAL (clicked()), this, SLOT (useVector2()));
button3 = new QPushButton("Move right", this);
button3 -> setGeometry(50,350, 100,50);
button3 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button3, SIGNAL (clicked()), this, SLOT (useVector3()));
button4 = new QPushButton("Spin left", this);
button4 -> setGeometry(50,450, 100,50);
button4 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button4, SIGNAL (clicked()), this, SLOT (useVector4()));
button5 = new QPushButton("Spin right", this);
button5 -> setGeometry(50,550, 100,50);
button5 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button5, SIGNAL (clicked()), this, SLOT (useVector5()));
label = new QLabel("", this);
label ->setStyleSheet("background-color:rgb(0, 0, 0)");
label -> setGeometry(300,50,300,600);
}
//************************************************** **********************
MainWindow::~MainWindow(){
delete ui;
}
//************************************************** ***********************
void MainWindow::useVector0(){
arm.vctrMovesFun[0]();
arm.displayMoves(label);}
//************************************************** ***********************
void MainWindow::useVector1(){
arm.vctrMovesFun[1]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector2(){
arm.vctrMovesFun[2]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector3(){
arm.vctrMovesFun[3]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector4(){
arm.vctrMovesFun[4]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector5(){
arm.vctrMovesFun[5]();
arm.displayMoves(label);
}
main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
As you can see there is nothing special but I have a hope that you' ll answer my question.
1) I have aproblem to understand ui in MainWindow class. What is it and how can it be helpfull in Qt?
2) When I create vector of pointers to my functions i need to make them static, in another way I can't put them into vector. Why? (class Arm)
3) Constructor in MainWindow. Generally constructor is being called only one time when we are creating out object, so why method connect in MainWindow.cpp work for the whole program?
4) As you can see, there is 6 method to use my own function. I named them for example as: void useVector0(). I am trully sure that it's very bad to do. There should be one method but if I do something like:
void MainWindow::useVector(unsigned short k){
arm.vctrMovesFun[k]();
arm.displayMoves(label);
I can't use it as a slot because signal clicked() has void lists of argument. How to solve it? Overload clicked() method?
5) Maybe you general opinion about my code so write it. I'll be very happy for every words of criticsm.
Rafal
Arm.h:
#ifndef ARM_H
#define ARM_H
#include <QVector>
#include <QString>
#include <QLabel>
class Arm{
public:
Arm();
static void displayMoves(QLabel *ptrQLabel); //function for display QString listMoves
QVector<bool(*)(void)> vctrMovesFun; //contains pointers for function which defines moves of industrial robot
private:
static QString listMoves; //contain every move which industrial robot has done
static bool moveArmForward();
static bool moveArmBackward();
static bool moveArmLeft();
static bool moveArmRight();
static bool spinArmLeft();
static bool spinArmRight(); //all this functions define moves of robot's arm
};
#endif // ARM_H
Arm.cpp:
#include "arm.h"
QString Arm::listMoves = ""; //empty string
//************************************************** *************
Arm::Arm(){
vctrMovesFun = {&moveArmForward, &moveArmBackward, &moveArmLeft,
&moveArmRight, &spinArmLeft, &spinArmRight}; //set reference to functions
}
//************************************************** *************
bool Arm::moveArmForward(){
listMoves+= "Arm moved forward\n";
return true;}
//************************************************** *************
bool Arm::moveArmBackward(){
listMoves+= "Arm moved backward\n";
return true;}
//************************************************** *************
bool Arm::moveArmLeft(){
listMoves+= "Arm moved to the left\n";
return true;}
//************************************************** *************
bool Arm::moveArmRight(){
listMoves+= "Arm moved to the right\n";
return true;}
//************************************************** *************
bool Arm::spinArmLeft(){
listMoves+= "Arm spinned to the left\n";
return true;}
//************************************************** *************
bool Arm::spinArmRight(){
listMoves+= "Arm spinned to the right\n";
return true;}
//************************************************** *************
void Arm::displayMoves(QLabel *ptrQLabel){
ptrQLabel -> setText(listMoves);
}
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "arm.h"
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
class MainWindow;}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QPushButton *button0;
QPushButton *button1;
QPushButton *button2;
QPushButton *button3;
QPushButton *button4;
QPushButton *button5;
QLabel *label;
Arm arm;
private slots:
void useVector0();
void useVector1();
void useVector2();
void useVector3();
void useVector4();
void useVector5();
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui -> setupUi(this);
this -> setGeometry(0,0,800,700);
this -> setStyleSheet("background-color:rgb(188, 198 ,204)");
button0 = new QPushButton("Move forward", this);
button0 -> setGeometry(50,50, 100,50);
button0 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button0, SIGNAL (clicked()), this, SLOT (useVector0()));
button1 = new QPushButton("Move backward", this);
button1 -> setGeometry(50,150, 100,50);
button1 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button1, SIGNAL (clicked()), this, SLOT (useVector1()));
button2 = new QPushButton("Move left", this);
button2 -> setGeometry(50,250, 100,50);
button2 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button2, SIGNAL (clicked()), this, SLOT (useVector2()));
button3 = new QPushButton("Move right", this);
button3 -> setGeometry(50,350, 100,50);
button3 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button3, SIGNAL (clicked()), this, SLOT (useVector3()));
button4 = new QPushButton("Spin left", this);
button4 -> setGeometry(50,450, 100,50);
button4 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button4, SIGNAL (clicked()), this, SLOT (useVector4()));
button5 = new QPushButton("Spin right", this);
button5 -> setGeometry(50,550, 100,50);
button5 -> setStyleSheet("background-color:rgb(108, 118, 143)");
connect(button5, SIGNAL (clicked()), this, SLOT (useVector5()));
label = new QLabel("", this);
label ->setStyleSheet("background-color:rgb(0, 0, 0)");
label -> setGeometry(300,50,300,600);
}
//************************************************** **********************
MainWindow::~MainWindow(){
delete ui;
}
//************************************************** ***********************
void MainWindow::useVector0(){
arm.vctrMovesFun[0]();
arm.displayMoves(label);}
//************************************************** ***********************
void MainWindow::useVector1(){
arm.vctrMovesFun[1]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector2(){
arm.vctrMovesFun[2]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector3(){
arm.vctrMovesFun[3]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector4(){
arm.vctrMovesFun[4]();
arm.displayMoves(label);
}
//************************************************** ***********************
void MainWindow::useVector5(){
arm.vctrMovesFun[5]();
arm.displayMoves(label);
}
main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
As you can see there is nothing special but I have a hope that you' ll answer my question.
1) I have aproblem to understand ui in MainWindow class. What is it and how can it be helpfull in Qt?
2) When I create vector of pointers to my functions i need to make them static, in another way I can't put them into vector. Why? (class Arm)
3) Constructor in MainWindow. Generally constructor is being called only one time when we are creating out object, so why method connect in MainWindow.cpp work for the whole program?
4) As you can see, there is 6 method to use my own function. I named them for example as: void useVector0(). I am trully sure that it's very bad to do. There should be one method but if I do something like:
void MainWindow::useVector(unsigned short k){
arm.vctrMovesFun[k]();
arm.displayMoves(label);
I can't use it as a slot because signal clicked() has void lists of argument. How to solve it? Overload clicked() method?
5) Maybe you general opinion about my code so write it. I'll be very happy for every words of criticsm.
Rafal