Qt Slot使用

QT4中,Slot無法透過designer去做新的定義,只能手動在原始碼中添加。

以下附上我做的範例來做講解。

 

---rfid-class.h---

#include <termios.h>

#include "qthead.h"

class Makerfid : public QObject

{

      Q_OBJECT

      public:

             Makerfid(QObject *parent = 0);

             void ShowTagInfo();

             struct termios  oldtio, newtio;

            

             struct _Tag_Info

             {

                      unsigned char Flags ;

                      unsigned char InfoFlags;

                      unsigned char UID[8];

                      unsigned char DSFID;

                      unsigned char AFI;

                      unsigned char Other[3];

             }TagInfo;

     public slots:

             void ReadTagInfo();

      private:

              int fd;

};

 

做一個宣告class的標頭檔。

class Makerfid : public QObject ,在此部分我僅作為一個object來用所以宣告Makerfid繼承自QObject

 

Q_OBJECT,只要類別中有包含了signalsslots時,一定要將此macro放進來。而原因就是此macro定義了實做在meta-object檔中的函式。

 

Makerfid(QObject *parent = 0),表示top-level windos,但由於Makerfid只做object,也可以留空白不做任何參數設定。

 

public slots,將自定義的slot放到這個位置。

signals,將自定義的signal放到這個位置。


--- rfid-class.cpp---

#include <QObject>

#include <string.h>

#include <linux/string.h>

#include <stdlib.h>

#include <bits/string2.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <termios.h>

#include <stdio.h>

#include "rfid-class.h"

 

 

#define BAUDRATE        B115200

#define MODEMDEVICE     "/dev/tts/1"

#define _POSIX_SOURCE   1

Makerfid::Makerfid(QObject *parent )

    : QObject(parent)

{

printf("Start...\n");

fd = open(MODEMDEVICE, O_RDWR|O_NOCTTY);

if (fd < 0) {

    perror(MODEMDEVICE);

    exit(1);

 }

printf("Open...\n");

   

tcgetattr(fd, &oldtio);

bzero(&newtio, sizeof(newtio));

newtio.c_cflag = BAUDRATE|CS8|CLOCAL|CREAD |HUPCL;

newtio.c_iflag = IGNPAR;

newtio.c_oflag = 0;

/* 設定為正規模式 */

tcflush(fd, TCIFLUSH);

tcsetattr(fd, TCSANOW, &newtio);

 }

 

void Makerfid::ReadTagInfo()

{

        int i;

        unsigned char DataBuf[19] = {0};

        unsigned char Cmd[4]={0x02,0x00,0x20,0x22};

 

        write(fd,Cmd,4);

        read(fd,DataBuf,19);

       

        /*give data to TagInfo*/

        TagInfo.Flags =DataBuf[3];

        TagInfo.InfoFlags=DataBuf[4];

        for(i=0;i<8;i++)

        {TagInfo.UID[i]=DataBuf[i+5];}

        TagInfo.DSFID=DataBuf[13];

        TagInfo.AFI=DataBuf[14];

    for(i=0;i<3;i++)

        {TagInfo.Other[i]=DataBuf[i+15];}

}

void Makerfid::ShowTagInfo()

{

        int i;

        printf("The TagInfo is\n");

        printf("Flags     : 0x%X\n",TagInfo.Flags);

        printf("InfoFlags : 0x%X\n",TagInfo.InfoFlags);

        printf("UID       : ");

        for(i=0;i<8;i++)

        printf("0x%X ",TagInfo.UID[i]);

        printf("\nDSFID     : 0x%X\n",TagInfo.DSFID);

        printf("AFI       : 0x%X\n",TagInfo.AFI);

        printf("Other     : ");

        for(i=0;i<3;i++)

        printf("0x%X ",TagInfo.Other[i]);

        printf("\n");

}

此部份是作class中建構子(Makerfid() ),及其他函數的設定(ShowTagInfo()ReadTagInfo() )

 

---main.c---

#include <stdio.h>

#include "rfid-class.h"

#include <QApplication>

#include <QPushButton>

#include <QObject>

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

{

    int i;

    Makerfid rfid;

     QApplication app(argc, argv);

    

     QPushButton hello(QObject::tr("Hello world!"));

     QObject::connect(&hello, SIGNAL(clicked()), &rfid, SLOT(ReadTagInfo()));

     hello.show();

     return app.exec();

    }

 

此部分宣告Makerfid的變數rfid,並設定當按鍵按下(clicked)時會執行ReadTagInfo,最後由return app.exec()main()函數將控制權遞送給Qt。當程式結束之後,QCoreApplication::exec()將會回傳。呼叫QCoreApplication::exec()後,Qt會接收並處理使用者以及系統事件(main event),並且傳遞給對應的widget(此例為按鈕)。詳細資訊可參考(http://doc.trolltech.com/4.4/qapplication.html#exec)

 

參考資料:

http://support.oss.org.tw/wiki/index.php/Qt_example_4

http://www.qtcn.org/bbs/read.php?tid=6601&keyword=slot

arrow
arrow
    全站熱搜

    flykof 發表在 痞客邦 留言(0) 人氣()