Chinaunix首页 | 论坛 | 博客
  • 博客访问: 407804
  • 博文数量: 62
  • 博客积分: 1483
  • 博客等级: 上尉
  • 技术积分: 779
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-24 12:25
文章分类

全部博文(62)

文章存档

2012年(2)

2011年(6)

2010年(6)

2009年(48)

我的朋友

分类: LINUX

2009-11-22 10:55:26

控制led的qt实验,加上布局管理器后效果好多了。
hello.h--------------------------
#include
#include
#include
#include
#include
#include

class myMainWindow : public QWidget
{
    Q_OBJECT
    public:
        myMainWindow();
    public slots:
        void ledOn1();
        void ledOff1();
        void ledOn2();
        void ledOff2();
        void ledOn3();
        void ledOff3();
        void ledOn4();
        void ledOff4();
        void ledOnAll();
        void ledOffAll();
    private:
        int fd;
        QPushButton *b_on1;
        QPushButton *b_off1;
        QPushButton *b_on2;
        QPushButton *b_off2;
        QPushButton *b_on3;
        QPushButton *b_off3;
        QPushButton *b_on4;
        QPushButton *b_off4;
        QPushButton *b_onAll;
        QPushButton *b_offAll;

        QLabel *label;
};
hello.cpp--------------------------
#include
#include
#include
#include
#include
#include

class myMainWindow : public QWidget
{
    Q_OBJECT
    public:
        myMainWindow();
    public slots:
        void ledOn1();
        void ledOff1();
        void ledOn2();
        void ledOff2();
        void ledOn3();
        void ledOff3();
        void ledOn4();
        void ledOff4();
        void ledOnAll();
        void ledOffAll();
    private:
        int fd;
        QPushButton *b_on1;
        QPushButton *b_off1;
        QPushButton *b_on2;
        QPushButton *b_off2;
        QPushButton *b_on3;
        QPushButton *b_off3;
        QPushButton *b_on4;
        QPushButton *b_off4;
        QPushButton *b_onAll;
        QPushButton *b_offAll;

        QLabel *label;
};
hello.cpp------------------------
#include "hello.h"

myMainWindow::myMainWindow()
{
    setMinimumSize(200,200);

    label = new QLabel(this);
    label->setMinimumSize(150,30);
    label->setMaximumSize(240,30);
    label->setText("press these buttons to\n"
                    "control the leds on or off");
    label->setAlignment(AlignCenter);

    b_on1 = new QPushButton("on 1",this);
    b_on1->setMinimumSize(50,20);
    b_on1->setFont(QFont("Times",15,QFont::Bold));

    b_off1 = new QPushButton("off 1",this);
    b_off1->setMinimumSize(50,20);
    b_off1->setFont(QFont("Times",15,QFont::Bold));

    b_on2 = new QPushButton("on 2",this);
    b_on2->setMinimumSize(50,20);
    b_on2->setFont(QFont("Times",15,QFont::Bold));

    b_off2 = new QPushButton("off 2",this);
    b_off2->setMinimumSize(50,20);
    b_off2->setFont(QFont("Times",15,QFont::Bold));

    b_on3 = new QPushButton("on 3",this);
    b_on3->setMinimumSize(50,20);
    b_on3->setFont(QFont("Times",15,QFont::Bold));

    b_off3 = new QPushButton("off 3",this);
    b_off3->setMinimumSize(50,20);
    b_off3->setFont(QFont("Times",15,QFont::Bold));

    b_on4 = new QPushButton("on 4",this);
    b_on4->setMinimumSize(50,20);
    b_on4->setFont(QFont("Times",15,QFont::Bold));

    b_off4 = new QPushButton("off 4",this);
    b_off4->setMinimumSize(50,20);
    b_off4->setFont(QFont("Times",15,QFont::Bold));

    b_onAll = new QPushButton("on All",this);
    b_onAll->setMinimumSize(50,20);
    b_onAll->setFont(QFont("Times",15,QFont::Bold));

    b_offAll = new QPushButton("off All",this);
    b_offAll->setMinimumSize(50,20);
    b_offAll->setFont(QFont("Times",15,QFont::Bold));

    QVBoxLayout *vbox = new QVBoxLayout(this);
    QHBoxLayout *hbox1 = new QHBoxLayout();
    QHBoxLayout *hbox2 = new QHBoxLayout();
    QHBoxLayout *hbox3 = new QHBoxLayout();
    QHBoxLayout *hbox4 = new QHBoxLayout();
    QHBoxLayout *hbox5 = new QHBoxLayout();
    QHBoxLayout *hbox6 = new QHBoxLayout();

    vbox->addLayout(hbox1);
    vbox->addLayout(hbox2);
    vbox->addLayout(hbox3);
    vbox->addLayout(hbox4);
    vbox->addLayout(hbox5);
    vbox->addLayout(hbox6);

    hbox1->addWidget(label);
    hbox2->addWidget(b_on1);
    hbox2->addWidget(b_off1);
    hbox3->addWidget(b_on2);
    hbox3->addWidget(b_off2);
    hbox4->addWidget(b_on3);
    hbox4->addWidget(b_off3);
    hbox5->addWidget(b_on4);
    hbox5->addWidget(b_off4);
    hbox6->addWidget(b_onAll);
    hbox6->addWidget(b_offAll);

    connect(b_on1,SIGNAL(clicked()),this,SLOT(ledOn1()));
    connect(b_off1,SIGNAL(clicked()),this,SLOT(ledOff1()));

    connect(b_on2,SIGNAL(clicked()),this,SLOT(ledOn2()));
    connect(b_off2,SIGNAL(clicked()),this,SLOT(ledOff2()));

    connect(b_on3,SIGNAL(clicked()),this,SLOT(ledOn3()));
    connect(b_off3,SIGNAL(clicked()),this,SLOT(ledOff3()));

    connect(b_on4,SIGNAL(clicked()),this,SLOT(ledOn4()));
    connect(b_off4,SIGNAL(clicked()),this,SLOT(ledOff4()));

    connect(b_onAll,SIGNAL(clicked()),this,SLOT(ledOnAll()));
    connect(b_offAll,SIGNAL(clicked()),this,SLOT(ledOffAll()));
}

#include
#include
#include
#include
#include
#include
#include

void myMainWindow::ledOn1()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 1, 0);
    ::close(fd); //这里的close()与qt的冲突,用::代表用外部的close(),否则程序直接死掉(为什么呢?)。
}

void myMainWindow::ledOff1()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 0, 0);
    ::close(fd);
}

void myMainWindow::ledOn2()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 1, 1);
    ::close(fd); //这里的close()与qt的冲突,用::代表用外部的close(),否则程序直接死掉(为什么呢?)。
}

void myMainWindow::ledOff2()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 0, 1);
    ::close(fd);
}

void myMainWindow::ledOn3()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 1, 2);
    ::close(fd); //这里的close()与qt的冲突,用::代表用外部的close(),否则程序直接死掉(为什么呢?)。
}

void myMainWindow::ledOff3()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 0, 2);
    ::close(fd);
}

void myMainWindow::ledOn4()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 1, 3);
    ::close(fd); //这里的close()与qt的冲突,用::代表用外部的close(),否则程序直接死掉(为什么呢?)。
}

void myMainWindow::ledOff4()
{
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    ioctl(fd, 0, 3);
    ::close(fd);
}

void myMainWindow::ledOnAll()
{
    int i;
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    for(i=0;i<4;i++){
        ioctl(fd, 1, i);
    }
    ::close(fd); //这里的close()与qt的冲突,用::代表用外部的close(),否则程序直接死掉(为什么呢?)。
}

void myMainWindow::ledOffAll()
{
    int i;
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        fd = open("/dev/leds", 0);
    }
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    for(i=0;i<4;i++){
        ioctl(fd, 0, i);
    }
    ::close(fd);
}

int main(int argc,char **argv)
{
    QApplication a(argc,argv);
    myMainWindow w;
    a.setMainWidget(&w);
    w.show();
    a.exec();
}
阅读(2807) | 评论(1) | 转发(0) |
0

上一篇:第一个qt程序

下一篇:读ad的qt代码

给主人留下些什么吧!~~

lanmanck2010-11-30 12:26:47

close不是直接死掉,而是qt的退出函数。::close就是标准的c语言了