1. 程式人生 > >樹莓派3B安裝QT5

樹莓派3B安裝QT5

最近筆者接到任務,要在樹莓派上跑QT。百度上一搜有兩種方法,一是用交叉編譯鏈,在pc上編寫程式碼後在樹莓派上執行,二是直接在樹莓派上打碼。鑑於對交叉編譯鏈的“恐懼”,果斷選擇後者。得益於樹莓派的廣泛使用,其受到了QT5的官方支援,所以用起來相對簡單。

第一步

在以下網址下載最新版的raspbian jessie,這裡就不贅述了。 
樹莓派官網 
然後開機,順便裝一下遠端桌面

sudo apt-get install xrdp
  • 1
  • 1

使用者名稱是pi,密碼是raspberry 
然後更新一下(千萬別改官方軟體源,筆者用了一天的光陰作為代價) 
在順便改下鍵盤佈局 
修改鍵盤佈局

sudo apt-get update
sudo
apt-get upgrade sudo rpi-update
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

漫長的更新後reboot一下。

第二步

安裝下面兩個包

sudo apt-get install qt5-default
sudo apt-get install qtcreator
  • 1
  • 2
  • 1
  • 2

安裝完成後,開啟Menu -> Programming -> QtCreator就可以愉快地玩耍了。 
qt5

第三步

由於Qt Creator不能自動識別樹莓派上的工具鏈,因此需要手動新增。點選Tools -> Options開啟配置對話方塊,在Build & Run -> Kits選項卡中點選Add。Compiler設定為/usr/bin/gcc,Debugger可設定為/usr/bin/gdb,Devices type選擇Desktop,Device選擇local,qt version選擇/usr/bin/qmake。

PS:如果選擇qt version時彈出qmake not exetuable,則說明qt4/qt5沒有安裝,需要

 sudo apt-get install qt-sdk
 sudo apt-get install qt5-default
  • 1
  • 2
  • 1
  • 2

然後漫長的等待即可。

第四步

馬上嘗試下hello word吧。 
PS:1、QT編譯生成的資料夾與原始檔的資料夾在同一目錄下,名字是build-xxx 
2、若使用了wiringPi庫,需開啟原始碼.pro檔案,在最後加上一行

LIBS += -lwiringPi
  • 1
  • 1

附一份檔名為helloWP,類名稱也是helloWP,使用wiringPi的小程式。在QT Creator的專案檔案中雙擊介面檔案.ui,拖一個label和兩個push button出來。並修改hellowp.h為

#ifndef HELLOWP_H
#define HELLOWP_H

#include <QMainWindow>
#include <QtCore>

namespace Ui {
class helloWP;
}

class helloWP : public QMainWindow
{
    Q_OBJECT

public:
    explicit helloWP(QWidget *parent = 0);
    ~helloWP();

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void timerUpDate();

private:
    Ui::helloWP *ui;
    QTimer *switch_timer;

};

#endif // HELLOWP_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

將hellowp.cpp修改為:

#include "hellowp.h"
#include "ui_hellowp.h"
#include "wiringPi.h"

helloWP::helloWP(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::helloWP)
{   
    ui->setupUi(this);
    wiringPiSetup();
    pinMode(0, OUTPUT);
    pinMode(1, INPUT);
    digitalWrite(0, HIGH);

    switch_timer = new QTimer();
    connect(switch_timer, SIGNAL(timeout()), this, SLOT(timerUpDate()));
    switch_timer->start(100);

}

helloWP::~helloWP()
{
    delete ui;
}

void helloWP::timerUpDate()
{
    if(digitalRead(1) == LOW)
        ui->label->setText("Switch is low!");
    else
        ui->label->setText("Switch is high!");
}

void helloWP::on_pushButton_clicked()
{
    digitalWrite(0, LOW);
}

void helloWP::on_pushButton_2_clicked()
{
    digitalWrite(0, HIGH);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

這個小程式控制了一盞LED,和通過定時器讀取一個引腳的狀態。LED是wiringPi的0引腳,開關是1引腳。由於使用了wiringPisetup而不是wiringPisetupsys,qt會提示需要root許可權,這時在輸出檔案的目錄中用以下命令執行即可。

sudo ./xxx
  • 1
  • 1

引腳圖