Results 1 to 3 of 3

Thread: Qt and C++ my own class and slots implementation

  1. #1
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Qt and C++ my own class and slots implementation

    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:
    Qt Code:
    1. #ifndef ARM_H
    2. #define ARM_H
    3.  
    4. #include <QVector>
    5. #include <QString>
    6. #include <QLabel>
    7.  
    8. class Arm{
    9.  
    10. public:
    11. Arm();
    12. static void displayMoves(QLabel *ptrQLabel); //function for display QString listMoves
    13. QVector<bool(*)(void)> vctrMovesFun; //contains pointers for function which defines moves of industrial robot
    14.  
    15. private:
    16. static QString listMoves; //contain every move which industrial robot has done
    17.  
    18. static bool moveArmForward();
    19. static bool moveArmBackward();
    20. static bool moveArmLeft();
    21. static bool moveArmRight();
    22. static bool spinArmLeft();
    23. static bool spinArmRight(); //all this functions define moves of robot's arm
    24. };
    25.  
    26. #endif // ARM_H
    To copy to clipboard, switch view to plain text mode 

    Arm.cpp:

    Qt Code:
    1. #include "arm.h"
    2.  
    3. QString Arm::listMoves = ""; //empty string
    4. //***************************************************************
    5. Arm::Arm(){
    6. vctrMovesFun = {&moveArmForward, &moveArmBackward, &moveArmLeft,
    7. &moveArmRight, &spinArmLeft, &spinArmRight}; //set reference to functions
    8. }
    9. //***************************************************************
    10. bool Arm::moveArmForward(){
    11. listMoves+= "Arm moved forward\n";
    12. return true;}
    13. //***************************************************************
    14. bool Arm::moveArmBackward(){
    15. listMoves+= "Arm moved backward\n";
    16. return true;}
    17. //***************************************************************
    18. bool Arm::moveArmLeft(){
    19. listMoves+= "Arm moved to the left\n";
    20. return true;}
    21. //***************************************************************
    22. bool Arm::moveArmRight(){
    23. listMoves+= "Arm moved to the right\n";
    24. return true;}
    25. //***************************************************************
    26. bool Arm::spinArmLeft(){
    27. listMoves+= "Arm spinned to the left\n";
    28. return true;}
    29. //***************************************************************
    30. bool Arm::spinArmRight(){
    31. listMoves+= "Arm spinned to the right\n";
    32. return true;}
    33. //***************************************************************
    34. void Arm::displayMoves(QLabel *ptrQLabel){
    35. ptrQLabel -> setText(listMoves);
    36. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include "arm.h"
    5.  
    6. #include <QMainWindow>
    7. #include <QPushButton>
    8.  
    9. namespace Ui {
    10. class MainWindow;}
    11.  
    12. class MainWindow : public QMainWindow{
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. QPushButton *button0;
    22. QPushButton *button1;
    23. QPushButton *button2;
    24. QPushButton *button3;
    25. QPushButton *button4;
    26. QPushButton *button5;
    27.  
    28. QLabel *label;
    29.  
    30. Arm arm;
    31.  
    32. private slots:
    33. void useVector0();
    34. void useVector1();
    35. void useVector2();
    36. void useVector3();
    37. void useVector4();
    38. void useVector5();
    39.  
    40. };
    41.  
    42. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui -> setupUi(this);
    9. this -> setGeometry(0,0,800,700);
    10. this -> setStyleSheet("background-color:rgb(188, 198 ,204)");
    11.  
    12. button0 = new QPushButton("Move forward", this);
    13. button0 -> setGeometry(50,50, 100,50);
    14. button0 -> setStyleSheet("background-color:rgb(108, 118, 143)");
    15. connect(button0, SIGNAL (clicked()), this, SLOT (useVector0()));
    16.  
    17. button1 = new QPushButton("Move backward", this);
    18. button1 -> setGeometry(50,150, 100,50);
    19. button1 -> setStyleSheet("background-color:rgb(108, 118, 143)");
    20. connect(button1, SIGNAL (clicked()), this, SLOT (useVector1()));
    21.  
    22. button2 = new QPushButton("Move left", this);
    23. button2 -> setGeometry(50,250, 100,50);
    24. button2 -> setStyleSheet("background-color:rgb(108, 118, 143)");
    25. connect(button2, SIGNAL (clicked()), this, SLOT (useVector2()));
    26.  
    27. button3 = new QPushButton("Move right", this);
    28. button3 -> setGeometry(50,350, 100,50);
    29. button3 -> setStyleSheet("background-color:rgb(108, 118, 143)");
    30. connect(button3, SIGNAL (clicked()), this, SLOT (useVector3()));
    31.  
    32. button4 = new QPushButton("Spin left", this);
    33. button4 -> setGeometry(50,450, 100,50);
    34. button4 -> setStyleSheet("background-color:rgb(108, 118, 143)");
    35. connect(button4, SIGNAL (clicked()), this, SLOT (useVector4()));
    36.  
    37. button5 = new QPushButton("Spin right", this);
    38. button5 -> setGeometry(50,550, 100,50);
    39. button5 -> setStyleSheet("background-color:rgb(108, 118, 143)");
    40. connect(button5, SIGNAL (clicked()), this, SLOT (useVector5()));
    41.  
    42. label = new QLabel("", this);
    43. label ->setStyleSheet("background-color:rgb(0, 0, 0)");
    44. label -> setGeometry(300,50,300,600);
    45. }
    46.  
    47.  
    48. //************************************************************************
    49. MainWindow::~MainWindow(){
    50. delete ui;
    51. }
    52. //*************************************************************************
    53. void MainWindow::useVector0(){
    54. arm.vctrMovesFun[0]();
    55. arm.displayMoves(label);}
    56. //*************************************************************************
    57. void MainWindow::useVector1(){
    58. arm.vctrMovesFun[1]();
    59. arm.displayMoves(label);
    60. }
    61. //*************************************************************************
    62. void MainWindow::useVector2(){
    63. arm.vctrMovesFun[2]();
    64. arm.displayMoves(label);
    65. }
    66. //*************************************************************************
    67. void MainWindow::useVector3(){
    68. arm.vctrMovesFun[3]();
    69. arm.displayMoves(label);
    70. }
    71. //*************************************************************************
    72. void MainWindow::useVector4(){
    73. arm.vctrMovesFun[4]();
    74. arm.displayMoves(label);
    75. }
    76. //*************************************************************************
    77. void MainWindow::useVector5(){
    78. arm.vctrMovesFun[5]();
    79. arm.displayMoves(label);
    80. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:

    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. MainWindow w;
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt and C++ my own class and slots implementation

    1. Ui is the access point to the generated UI from Qt Designer. Since you are ignoring the Designer UI it is mot useful in your program.

    2. Declaring the functions static removes the need to call them through an instance of the class, i.e. They are class functions. On the other hand the functions do not get a "this" pointer and cannot manipulate member data so your QString member must also be static. To use function pointers with normal member functions you also need to bind the function to a particular instance of the class (std::bind or the like). (Your code contains no objects of class Arm.). This is standard C++ and nothing to do with Qt.

    3. QObjects remember their connections until the object at either end of the connection is destroyed (or you deliberately disconnect). The MainWindow object and its child widgets have the same lifetime so the connections are not broken while your program runs.

    4. QSignalMapper

    5. Use Qt Designer or code your UI with layout management.

  3. #3
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Qt and C++ my own class and slots implementation

    Thank you for reply. That was very helpfull btw, I know standard of C++ so I know about static member variable and functions but in this case i have little attack of stupidity - sorry.

Similar Threads

  1. Replies: 37
    Last Post: 14th December 2012, 09:50
  2. Signals Slots and Class Pointers
    By Atomic_Sheep in forum Newbie
    Replies: 18
    Last Post: 7th September 2012, 09:08
  3. Cannot write a function pointer into a class implementation
    By tonnot in forum General Programming
    Replies: 7
    Last Post: 18th April 2011, 21:22
  4. Access a class without using Signals/Slots
    By impeteperry in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2010, 11:14
  5. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 13:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.