Qt World Summit: Register Today!

Qt-creator problem: error: 'gpio26' was not declared in this scope

  • Hello,
    I found some code on web and want to use it in my app.
    But I can figure out where i my mistake.
    I hope someone could have a look for that https://dl.dropboxusercontent.com/u/98706767/LEDControl.zip

    My host is Ubuntu 12.04
    qt 4 and latest qt creator.
    target i.mx6


  • It obviously cant know what gpio26 is. That pointer is only available in your main function

  • How can I make it usable in slot of my buttons ?

  • Put it into your LEDcontrol.h

    @#ifndef LEDCONTROLL_H
    #define LEDCONTROLL_H

    #include <QMainWindow>
    #include "GPIOClass.h"

    namespace Ui {
    class LedControll;

    class LedControll : public QMainWindow

    explicit LedControll(QWidget *parent = 0);

    private slots:
    void on_Button_LED_on_clicked();

    void on_Button_LED_off_clicked();

    Ui::LedControll ui;


    #endif // LEDCONTROLL_H


    @#include "ledcontroll.h"
    #include "ui_ledcontroll.h"
    #include "GPIOClass.h"
    #include <iostream>
    #include <unistd.h>
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    using namespace std;

    LedControll::LedControll(QWidget *parent) :
    ui(new Ui::LedControll)


    gpio26 = new GPIOClass("26"); //create new GPIO object to be attached to  GPIO4
    gpio26->export_gpio(); //export GPIO26
    gpio26->setdir_gpio("out"); //GPIO26 set to output


    delete ui;

    void LedControll::on_Button_LED_on_clicked()
    gpio26->setval_gpio("1"); // turn LED ON

    void LedControll::on_Button_LED_off_clicked()


    @#include "ledcontroll.h"
    #include <QApplication>

    int main(int argc, char *argv[])

    QApplication a(argc, argv);
    LedControll w;
    return a.exec&#40;&#41;;


  • Thank You very much.
    Works fine now !

  • One more question.
    The program needs to be root to access gpio. I think thats not the best way. How to change that?

  • Lifetime Qt Champion


    Depending on how the device presents itself, one possibility would be to change the group of the gpio device and add your user to that group

  • I try that but did not realy works.
    I build a group 'hwuser'
    add the user to the group

    change the group with

    chgrp -R hwuser /sys/class

    with ls -l could see that it is done.

    but after reboot the group is root again.

  • Lifetime Qt Champion

    I thought you were accessing the GPIO through a /dev device or does the sys fs entry point to a device ?

    Anyway, sys (like /dev in general) is generated dynamically so that method won't work.

    If you are indeed accessing a device that can be found in /dev, you could try to setup an udev rule that sets the group of that device.

  • That is more Linux question than Qt specific. If your target Linux uses udev for device handling then you need to specify rules which states you want "hwuser" group for gpio. Example setting udev-rules for gpio in raspberry pi: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=29&t=9667

  • Ok thanks to all.
    I figured out now.

    Thats works for me:
    sudo nano /etc/udev/rules.d/99-gpio.rules
    SUBSYSTEM==”gpio”, ACTION==”add”, PROGRAM=”/bin/sh -c ‘chown -R [user]:[group] /sys%p’”
    PROGRAM=”/bin/sh -c ‘chown -R [user]:[group] /sys/class’”

    save and reboot

  • Lifetime Qt Champion

    That might be a little too radical not to say that it's a security breach. Just modify the file you use not the entire tree

  • ok I will test that.


  • it is mysterious,
    after reboot export gpio works fine but set direction fails.

    the same again on root works fine.

  • Lifetime Qt Champion

    Are you accessing several files ?

  • not sure what You mean.

    thats the init of the gpio

    gpio26 = new GPIOClass("26"); //create new GPIO object

    gpio26->export_gpio(); //export GPIO26
    gpio26->setdir_gpio("out"); //GPIO26 set to output

    the export works, I can see it after start program

    i have made a button to set direction... thats works to

    i have made button for export unexport and set direction.

    thats works fine too

Log in to reply