Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Gui for project



  • Hello everyone ,
    I have already a working project and I need to create a gui .
    The interface It should have more buttons, every button has to perform a function.
    How should I proceed to create this ?
    The program is complex has many headers and a single main.
    Can you tell me some examples? thank you


  • Moderators

    @jackhyde said in Gui for project:

    The interface It should have more buttons, every button has to perform a function.
    How should I proceed to create this ?
    Can you tell me some examples?

    Yes, here you go.

    You can either create the UI yourself or use the QtDesigner to create a GUI.
    Take a look at Signals/Slots to trigger actions when they are pressed.



  • Thanks for the response.
    I made a successful example. I press a button, the button changes, and I carry out a function (the structure of my code should be so)
    writing this:
    MAINWINDOWS.H

    public slots :
    
        void push()
        {
         ....
        }
    

    MAINWINDOWS.CPP

    void MainWindow::on_pushButton_clicked()
    {
        QPixmap pix(":/rover.png");
        QIcon icon(pix);
        ui->pushButton->setIcon(icon);
        ui->pushButton->setIconSize(QSize(200,200));
        push();
    }
    

    and simple main.

    My problem is how integrate my code .....
    The part of heartbeat must be a button

    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <time.h>
    #include <unistd.h>
    #include <termios.h>
    #include <string.h>
    #if (defined __QNX__) | (defined __QNXNTO__)
    /* QNX specific headers */
    #include <unix.h>
    #else
    /* Linux / MacOS POSIX timer headers */
    #include <sys/time.h>
    #include <time.h>
    #include <arpa/inet.h>
    #endif
    
    /* This assumes you have the mavlink headers on your include path
    or in the same folder as this source file */
    #include "c_library_v1-master/common/mavlink.h"
    
    #define BUFFER_LENGTH 2041 // minimum buffer size that can be used with qnx (I don't know why)
    
    uint64_t microsSinceEpoch();
    
    void pause_tasto();
    int main(int argc, char* argv[])
    {
           char help[] = "--help";
           char target_ip[100];
           float position[6] = {};
           int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
           struct sockaddr_in gcAddr;
           struct sockaddr_in locAddr;
           //struct sockaddr_in fromAddr;
           uint8_t buf[BUFFER_LENGTH];
           ssize_t recsize;
           socklen_t fromlen;
           int bytes_sent;
           mavlink_message_t msg;
           uint16_t len;
           int i = 0;
           //int success = 0;
           unsigned int temp = 0;
           // Check if --help flag was used
           if ((argc == 2) && (strcmp(argv[1], help) == 0))
        {
                 printf("\n");
                 printf("\tUsage:\n\n");
                 printf("\t");
                 printf("%s", argv[0]);
                 printf(" <ip address of QGroundControl>\n");
                 printf("\tDefault for localhost: udp-server 127.0.0.1\n\n");
                 exit(EXIT_FAILURE);
    
        }
           // Change the target ip if parameter was given
           strcpy(target_ip, "127.0.0.1");
           if (argc == 2)
    
        {
                 strcpy(target_ip, argv[1]);
        }
    
           memset(&locAddr, 0, sizeof(locAddr));
           locAddr.sin_family = AF_INET;
           locAddr.sin_addr.s_addr = INADDR_ANY;
           locAddr.sin_port = htons(14551);
           /* Bind the socket to port 14551 - necessary to receive packets from qgroundcontrol */
           if (-1 == bind(sock,(struct sockaddr *)&locAddr, sizeof(struct sockaddr)))
        {
                 perror("error bind failed");
                 close(sock);
                 exit(EXIT_FAILURE);
        }
           /* Attempt to make it non blocking */
           if (fcntl(sock, F_SETFL, O_NONBLOCK | FASYNC) < 0)
        {
                 fprintf(stderr, "error setting nonblocking: %s\n", strerror(errno));
                 close(sock);
                 exit(EXIT_FAILURE);
        }
    
           memset(&gcAddr, 0, sizeof(gcAddr));
           gcAddr.sin_family = AF_INET;
           gcAddr.sin_addr.s_addr = inet_addr(target_ip);
           gcAddr.sin_port = htons(14550);
    
    
    		/*****************/
    
    		/* START COMMAND */
    
    		/*****************/
    
    		///////////////////////////////////////////////////////////
    		/////////////////  HEARTBEAT      /////////////////////////
    		///////////////////////////////////////////////////////////
    
    		printf("\nHeartbeat");
    
    		mavlink_msg_heartbeat_pack(5, 1, &msg, 2, 3, 1, 4, 3);
    		len = mavlink_msg_to_send_buffer(buf, &msg);
    		bytes_sent = sendto(sock, buf, len, 0, (struct sockaddr*)&gcAddr, sizeof(struct sockaddr_in));
    		sleep(2);
    
    		memset(buf, 0, BUFFER_LENGTH);
    		recsize = recvfrom(sock, (void *)buf, BUFFER_LENGTH, 0, (struct sockaddr *)&gcAddr, &fromlen);
    		if (recsize > 0)
    		{
    			// Something received - print out all bytes and parse packet
    			mavlink_message_t msg;
    			mavlink_status_t status;
    
    			printf("Bytes Received: %d\nDatagram: ", (int)recsize);
    			for (i = 0; i < recsize; ++i)
    			{
    				   temp = buf[i];
    				   printf("%02x ", (unsigned char)temp);
    				   if (mavlink_parse_char(MAVLINK_COMM_0, buf[i], &msg, &status))
    				   {
    						  // Packet received
    						  printf("\nReceived packet: SYS: %d, COMP: %d, LEN: %d, MSG ID: %d\n", msg.sysid, msg.compid, msg.len, msg.msgid);
    				   }
    			}
    			printf("\n");
    		}
    		memset(buf, 0, BUFFER_LENGTH);
    		sleep(2); // Sleep 2 seconds
    
    
    		///////////////////////////////////////////////////////////
    		/////////////////  END HEART     /////////////////////////
    		///////////////////////////////////////////////////////////
    ......
    ......
    ///////////////////////////////////////////////////////////
    ///////////////////   END /////////////////////////////////
    ///////////////////////////////////////////////////////////
    
    pause_tasto();
    }
    
    void pause_tasto()
    {
     printf("\n\nPremi un tasto\n");
     struct termios trm;
     memset(&trm, 0, sizeof(struct termios));
     cfmakeraw(&trm);
     struct termios old;
     tcgetattr(STDIN_FILENO, &old);
     tcsetattr(STDIN_FILENO, TCSAFLUSH, &trm);
     char buf;
     read(STDIN_FILENO, &buf, 1);
     tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
    }
    
    /* QNX timer version */
    
    #if (defined __QNX__) | (defined __QNXNTO__)
    uint64_t microsSinceEpoch()
    {
          struct timespec time;
          uint64_t micros = 0;
          clock_gettime(CLOCK_REALTIME, &time);
          micros = (uint64_t)time.tv_sec * 1000000 + time.tv_nsec/1000;
          return micros;
    
    }
    
    #else
    uint64_t microsSinceEpoch()
    {
         struct timeval tv;
          uint64_t micros = 0;
          gettimeofday(&tv, NULL);
          micros =  ((uint64_t)tv.tv_sec) * 1000000 + tv.tv_usec;
          return micros;
    }
    
    #endif
    

  • Moderators

    @jackhyde
    sorry, but you should start leaning the basics before going to for such a task, when i see this code.
    You wont gain any benefits from letting others write your code. Even such big amount of code.



  • Hello ,
    I posted the code (not completely) to make the idea of ​​what I should do.
    I do not want that someone will write the code for me I would like to learn.
    I began to see qt and doing simple little programs with buttons.
    Those for the moment work ...
    But I am in difficulty to integrate my code ...
    I would like to put part of my code in different buttons but going to define functions in mainwindow.h and then recalling them in mainwindows.c (as I do for the buttons of the most simple programs) I can not ...
    I apologize if it appeared that I wanted the code already written ... I just want advice or to know if you know of similar examples so that following them can learn.
    The error are:

    1. linker command failed with exit code 1
    2. duplicate symbols for architecture
      thank you


  • the code works but now I have another problem :(
    I have developed the code of ios I would need a well executable in Windows environment.
    there is a way to do it from mac?
    if I try to compile the same code in windows I mabiente this error
    "Needs a compiler to Set build"
    I followed some guide on the internet to fix it but did not succeeded.
    you can advise me a way? thank you


  • Lifetime Qt Champion

    Hi,

    No, you can't cross-compile from macOS to Windows.

    Also, you already have thread about your compiler setup here.


Log in to reply