dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)? [solved]

Thu Sep 12, 2019 5:52 pm

hello,
with a little help from my friends I finally managed to install qtcreator (feat. qt5, qtdesigner) and can now create a form providing buttons and build+start it as a window application.
now how to access the GPIOs , e.g. when buttonPress then digitalWrite GPIO 23, HIGH?
or show digitalRead of GPIO 24 (input_pullup) in a label?
or control e.g. a PCF8574 or an ADS1115 or UART (SerialRead/Write) with wiringPi Libs (using buttons and labels)?

in other words:
I know how to #include wiringPi into 1 single C program, the Geany IDE, and how to add the wiringPi compile and build parameters, but how can I get the wiringPi libs into the almost dozens of qt project files and set the correct compile and build parameters for qtcreator/designer?
Last edited by dsyleixa123 on Sun Oct 06, 2019 6:55 pm, edited 1 time in total.

User avatar
topguy
Posts: 5767
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Fri Sep 13, 2019 11:49 am

in other words:
To me it feels you are asking two very different questions. The second one is probably best handled by reading the documentaion:
https://doc.qt.io/qtcreator/creator-pro ... aries.html

To answer you first question its is also best to start with learning the basic UI mechanisms in Qt5:
https://www.bogotobogo.com/Qt/Qt5_SignalsSlotsGui.php

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Fri Sep 13, 2019 2:07 pm

@dsyleixa123

Hi. I don't use WiringPi but instead pigpiod. If you agree to use pigpiod this could help you to start:

In the .pro file add the following lines (to add the include path and the library path necessary to find the needed files):

Code: Select all

INCLUDEPATH += /usr/local/include
LIBS += -lpigpiod_if2 # To include libpigpiod_if2.so from /usr/local/lib
In the appropriate .cpp file insert the following:

Code: Select all

 #include "pigpiod_if2.h"// The header for using GPIO pins on Raspberry
 
 ...
 
    int gpioHostHandle = pigpio_start((char*)"localhost", (char*)"8888");
    if(gpioHostHandle >= 0) {
        if(set_mode(gpioHostHandle, gpioLEDpin, PI_OUTPUT) < 0) {
            ui->statusBar->showMessage(QString("Unable to initialize GPIO%1 as Output")
                                       .arg(gpioLEDpin));
            gpioHostHandle = -1;
        }
        else if(set_pull_up_down(gpioHostHandle, gpioLEDpin, PI_PUD_UP) < 0) {
            ui->statusBar->showMessage(QString("Unable to set GPIO%1 Pull-Up")
                                       .arg(gpioLEDpin));
            gpioHostHandle = -1;
        }
    }
    else {
        ui->statusBar->showMessage(QString("Unable to initialize the Pi GPIO."));
    }
 
This last is just an example that obviously must be adapted to your need.

Then when you are done with GPIO:

Code: Select all

    
if(gpioHostHandle >= 0)
    pigpio_stop(gpioHostHandle);
Check the documentation of pigpiod at http://abyz.me.uk/rpi/pigpio/pigpiod.html

Gabriele

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Fri Sep 13, 2019 2:50 pm

thank you,
unfortunately pigpio is very hard to use for me, I don't understand the functions, the syntax, and everything. So tbh, I do not understand what your pigpio code actually means.
Fortunately, the wiringPi functions remind me of Arduino functions in some respect (even if the syntax and parameters are divergent) and so it fits more to what I am used to.
Additionally, all my present GPIO device libs use wiringPi code which I want to #include in the same manner, e.g.
#include <mcp23017_lib.h>

After all, it sounds that I cannot just put at the top of any or a specific qt file (e.g., main.cpp)
#include <wiringPi.h>
compile the projects with the additional compile/build parameter -lwiringPi (ftm, no idea where to add this exactly)

then call the wiringPi functions arbitrarily in my source code just the way I usually do (in Geany), e.g.
pinMode(24, INPUT_PULLUP);
int ival=digitalRead(24);

and then display ival in a qt label in my qt form


- or will it work like that way though?

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Fri Sep 13, 2019 3:01 pm

Never tried but perhaps you can just insert into the .pro file the following three lines:

Code: Select all

INCLUDEPATH += /usr/local/include
LIBS += -L"/usr/local/lib"
LIBS += -lwiringPi
No harm...

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Fri Sep 13, 2019 3:03 pm

ah, ok, thanks a lot, I'll try that ASAP!

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Mon Sep 16, 2019 12:49 pm

update:
I included those lines in the .pro file and #included <wiringPi.h> in main.cpp, and it can be compiled.
When editing the main.cpp e.g. by
pinMo
it even is suggesting pinMode for auto-complete, so the qt editor obviously knows the correct wiringPi function.

Then I added a couple ofs labels, but now I have no idea how to make 1 of them (label_03) always show the updated value of a pinstate.
If I open the label_03 slot, it doesn't show any option for a label signal or a label slot (e.g. as a placeholder which can be edited) into either program file

int i=digitalRead(24);
label_03::print(i) ?????

(e.g., in Borland C++ Builder it's like Label3->Caption=String(i); )

But tbh, I do not find any a print or write method or a label->caption in a qt label_03 slot...

:shock: :roll:

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 7:51 am

Code: Select all

label_03.setText(QString("%1").arg(i));
should do the work.

Meaning...

You should convert the numerical value of i into a String (QString in Qt) and then assign the string to the QLabel.

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 8:27 am

salvato wrote:
Tue Sep 17, 2019 7:51 am

Code: Select all

label_03.setText(QString("%1").arg(i));
should do the work.

thank you, but into which source code file and into which quickly updated loop function will I have to write this line?

In Borland C++ Builder I have a unit1.cpp with functions like

Code: Select all

void __fastcall TForm1::XYZ( ) { }
(XYZ == FormCreate, Button1Click, Button2Click, ComPort1RxChar, Edit1Change, Edit2Change, ScrollBar1Change ...)
which are automatically updated for either event and where I can also write my label code into either one, e.g.

Code: Select all

void __fastcall TForm1::ComPort1RxChar(TObject *Sender, int Count)
{
  Form1->Caption="UART character detected";
  AnsiString rcvStr;
  ComPort1->ReadStr(rcvStr, 250);

  Label2->Caption=String(rcvStr);
}

but I do not even see such a qt C file for my qt Quit Button to close the main window when pressed (actually it works, but no code at all to find about that functionality),
not to talk about where to put digitalRead(pin) and then printing quickly updated values into labels...

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 8:54 am

Sorry... I'm not able to help you since there are too many unknown that I should imagine...
If you are able to place your project into a github repository I can try to help you.

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 9:12 am

thank you very much, here it is:
https://github.com/dsyleixa/RaspberryPi ... t_wiringPi

(strangely the executable of the project quit_wiringPi in the directory quit_wiringPi/
is called quit_btn, not quit_wiringPi as expected,
probably due to a crappy and broken "save project as... " maneuver before, but why that happened is far beyond me - anyway, it at least can be started)


dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 11:05 am

thank you very much!
I dowloaded the zip file, extracted, and renamed the folder btn_wiringPi_master to btn_wiringPi
and copied the folder
btn_wiringPi/* contents into my destination folder
/home/pi/raspiProgs/Qt/btn_wiringPi/* (deleted all my old files in there before).

I loaded the .pro file in qtcreator,
pressed the green play button, and it started to build
- but always hung up creating the makefile in the build-btn_wiringPi... folder,
so unfortunately it cannot be built and started :(

what am I missing?

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 11:19 am

Please do this:

Create a new project in QtCreator Without adding a single line and try to run it and see the MainWindow Appear.
It is this working ?

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 11:52 am

yes it does (empty main window)!
project name: voidtest

source files in folder
/home/pi/raspiProgs/Qt/voidtest/

executable built in folder
/home/pi/raspiProgs/Qt/build-voidtest-..../
executable name: voidtest

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 1:37 pm

Then the Qt configuration seem ok.
Then:
1) replace the files MainWindow.cpp and MainWindow.h with those in my project
2) in the .pro file insert the lines we discussed before in order to link the right libraries and add the include path.

Run the program and look at the output in the "Compile Output" window.

Tell me the result.

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Tue Sep 17, 2019 2:50 pm

before I did that, I moved your folder to /home/pi/
then I again created a new project like before, now also named quit_wiringPi in my programs subfolder.
build => again correctly an empty main window as before, as expected.

Unfortunately I cannot access this forum on my Pi, it's a torture to start the browser - so I can't c+p anything from qt here (e.g. from "Compile Output" window).

Now I copied your mainwindow.cpp + .h files and patched the pro file.
build => again an empty main window as before, unexpectedly.

trying to load mainwindow.ui threw an error (file does not exist or I don't have the rights)
=> marked all files and changed modify rights for owner -> everyone)
now I can load the ui file, but it's blank, no buttons, no labels.

then I renamed mainwindow.ui to mainwindow.ui.void
and copied your mainwindow.ui into this folder, changed rights,
opened qtcreator,
loaded the project,
mainwindow.ui now provides buttons and labels.
but after build => still an empty window

then I reloaded your files from your directory, but surpringly now also building an empty main window, whilst the log file now referred to MY directory instead :evil:

now I again completely overwrote my files in my folder with ALL of your files,
again changed all rights for modify to everyone,
again reloaded your project
build => now main window appears, but AGAIN EMPTY.

then I checked the other folder build-quit_wiringPi-....
and surpringly now the executable is called
quit_btn
(like my old project before, not quit_wiringPi as intended)
and when starting THAT directly, then your correct new main window appears...
WTF... :shock:
BUT
now all buttons show up and work correctly, both for the LED and for the switch!!! :geek:

again restarting the project out of the qtcreator (green run button)
=> also fine now, all buttons and labels now work ! :ugeek:
=> in the log message the target is called quit_btn, not quit_wiringPi though
:?

but what a mess about the file names!
how can that be corrected to have an executable correctly named like the project?

what also makes me wonder:
in your mainwindow.ui I only see the buttons and the constant label "pin24",
but the output labels for pin23-state and pin24-state don't show up:
WHY? How can I also see that in my form design (e.g. to change size, position,...)?

an extra warning in the output log:
libEGL warning: DRI2: failed to authenticate :roll:


But nonetheless:
GREAT WORK! Thank you very much for your efforts for now! :D 8-)

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Wed Sep 18, 2019 7:58 am

again tried to reproduce the failure from yesterday but couldn't actually reproduce the hang-up again :

I deleted all the build-.../ folders and also the current project project folder and c+p'ed just your project folder from github to my working dir.
It does not content a .pro.user file so far.
starting qtcreator...open project... choose quit_wiringPi.pro ... opening the mainwindow.ui ... (shows up correctly, no errors any more)

pressing the start button: suprisingly now the correct main window appears how it was expected, no hang-ups any more,
buttons and labels fully functioning.
Nonetheless, the new executable file is called quit_btn, not quit_wiringPi!
Why is that so?

PS,
I could copy the output messages to an USB drive and now can show the output via this Windows PC. It says:
Starte /home/pi/raspiProgs/Qt/build-quit_wiringPi-QtKit-Debug/quit_btn...
libEGL warning: DRI2: failed to authenticate
qt5ct: using qt5ct plugin
qt5ct: D-Bus global menu: no
/home/pi/raspiProgs/Qt/build-quit_wiringPi-QtKit-Debug/quit_btn beendet, Rückgabewert 0

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Wed Sep 18, 2019 8:15 am

The problem probably started when you tried to rename your project.
into the .pro file you will find the following line:

Code: Select all

TARGET = quit_btn
change it in

Code: Select all

TARGET = quit_wiringPi

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Wed Sep 18, 2019 8:55 am

ah yes!!! Now it's correct! Thank you! :)
Actually that was already the case in my first .pro file version from github which you once have forked! 8-)

about the 2nd problem I couldn't get further...
where can I see the labels in mainwindow.ui where the pin output states are written to?

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Wed Sep 18, 2019 10:08 am

The labels are there but since I've set an initial empty text they are not shown.
Open the form editor and look in the Object-Class window.
the labels are named:

Code: Select all

pin23Label
pin24Label
into the Property-Value Window look for the property
frameShape
and play with the possible values

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Wed Sep 18, 2019 10:20 am

thanks, but when I design a form, I will d+d a label into the form (automatically named, probably sth like label_01, label_02, label_03).
let's say, label _01 should show the LED state (right side of the buttons),
label_02 caption is "pin24" constantly,
and label_03 should show the button switch pinstate (right side of the label_02).

Currently I cannot see the labels for the pinstates, so how can I delete or change them in the form directly, just using the qt designer window?

salvato
Posts: 72
Joined: Tue Jan 15, 2013 9:21 pm

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Wed Sep 18, 2019 12:24 pm

Even if you don't see it try to click in its position.

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Wed Sep 18, 2019 2:40 pm

yes - surprise! - it's there! :o
Now I see and will try to understand more of the details when tinkering with your code and expanding it.

Thank you very very much, you really helped me a lot! 8-)

dsyleixa123
Posts: 405
Joined: Mon Jun 11, 2018 11:22 am

Re: qtcreator (qt5, qtdesigner): how to access GPIOs (r/w pins, i2c devices, UART,...)?

Sun Sep 29, 2019 11:18 am

hi,
another question:
having added a button or a drop-down-menu option:
after having selected this widget in the qt designer by a mouse-left-click
=> how can I directly jump to the text code entry in order to add or change related events or actions?

pressing shift-F4 just shows me a text file (main.cpp ?) , but does not show up and focus on a specific function about this button or widget.

How can I access or create a specific action (e.g. "close main window") for this dropdown option and jump to the related text in the related text file, whilst qt should create all the function wrapper around that action I would need, without having to write the entire code for all that from the scratch by my own?

e.g., having added a new button in Borland C++ Builder in double-clicking on it in the design window, then BCB immediately creates a specific source code wrapper function and focusses on it:

Code: Select all

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 
}
and then immediately jumps to the place in the related file text code window, where I only have to fill in the required function, e.g.Application->terminate();

How can I get this automatic functionality (create the function wrapper code automatically and jump to this function in the text file automatically) in qtcreator?
Instead, in qtdesigner, when I click (left, right, or double-click) on my quit button or my GPIO-High/Low-Buttons then I do not get anywhere where the related function (action, event) code is written or has to be written.

Return to “C/C++”