Results 1 to 4 of 4

Thread: Executing a C file from Qt

  1. #1
    Join Date
    May 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Executing a C file from Qt

    Hello everyone

    I am using Qt 5.3.2 and have also installed Qtcreator 3.2.1 in my Raspberry pi 2 running Linux.

    I have a C code which continously toggles the GPIO pin no. 17. Here the code is
    Qt Code:
    1. //it is saved as blink..c
    2.  
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include <errno.h>
    6. #include <fcntl.h>
    7. #include <sys/mman.h>
    8. #include <errno.h>
    9. #include <stdint.h>
    10.  
    11. static volatile uint32_t *gpio;
    12.  
    13. int main(int argc, char **argv)
    14. {
    15. int fd ;
    16.  
    17. //Obtain handle to physical memory
    18. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
    19. printf("Unable to open /dev/mem: %s\n", strerror(errno));
    20. return -1;
    21. }
    22.  
    23.  
    24. //map a page of memory to gpio at offset 0x3F200000 which is where GPIO goodnessstarts
    25. gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x3F200000);
    26.  
    27. if ((int32_t)gpio < 0){
    28. printf("Mmap failed: %s\n", strerror(errno));
    29. return -1;
    30. }
    31.  
    32. //set gpio17 as an output
    33.  
    34. //set the value through a little bit twiddling where we only modify the bits 21-23 in the register
    35. *(gpio + 1) = (*(gpio + 1) & ~(7 << 21)) | (1 << 21);
    36.  
    37. //toggle gpio17 every second
    38. while(1){
    39. //set the pin high
    40. //increment the pointer to 0x3F20001C
    41. *(gpio + 7) = 1 << 17;
    42.  
    43. //sleep
    44. // sleep(1);
    45.  
    46. //set the pin to low
    47. //increment the pointer to 0x3F200028
    48. *(gpio + 10) = 1 << 17;
    49.  
    50. //sleep(1);
    51. }
    52. }
    To copy to clipboard, switch view to plain text mode 

    this code is saved as blink.c

    And I can compile it through following code in terminal (not in Qt)
    Qt Code:
    1. gcc blink.c
    To copy to clipboard, switch view to plain text mode 

    And it is executing through following code in terminal (not in Qt)
    Qt Code:
    1. sudo ./a.out
    To copy to clipboard, switch view to plain text mode 

    The above compilation and execution I am doing is without Qt.

    I want to execute the same code When I push the button created in QtCreator. I know how to create button as I had gonr through this linkhttp://http://www2.cs.uic.edu/~i340/...gner_Tutorial/

    How should I move futher. I just want my file to execute when I press button.

    A detailed help will be very useful.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Executing a C file from Qt

    Executing an external process is a matter of using QProcess.

    Cheers,
    _

  3. #3
    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: Executing a C file from Qt

    Or you can embed your C code in your Qt C++ code and not have a separate process to toggle a couple of bits.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Executing a C file from Qt

    I just want my file to execute when I press button.
    Just to be clear - your file will not be the thing executing. It will be the code from that file that you compile and link into something executable.

    Or, as ChrisW67 suggests, if you want this code to execute from within a Qt program, then you put it into a slot that will be called when your button is clicked. But be careful - your while loop is an infinite loop, and will cause your Qt program to hang as it blinks the light every second.

    Better to use a QTimer that gets started when you click the button. Set the timer to fire every second, and in the slot you connect to the timeout() signal, execute the code inside the while loop once, then set the timer to fire again. You can add another button ("Stop") that stops the timer (and the blinking) through another slot.

    Signals, slots, and timers are covered very well in the Qt tutorials, so I don't think you need to be spoon-fed code. Go read.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. executing .py file from qt
    By prachi kamble in forum Qt Programming
    Replies: 6
    Last Post: 25th June 2015, 14:16
  2. doubt in by executing script file in qt
    By iswaryasenthilkumar in forum Newbie
    Replies: 5
    Last Post: 20th June 2015, 11:44
  3. Error while executing the exe release file
    By harmodrew in forum Newbie
    Replies: 7
    Last Post: 21st September 2010, 10:21
  4. Problems with executing the *.exe file
    By iksarp in forum Newbie
    Replies: 14
    Last Post: 28th February 2010, 05:49
  5. Executing SQL scripts from a file
    By William Wilson in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2007, 19:28

Tags for this Thread

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.