- 论坛徽章:
- 0
|
控制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
![]()
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93291/showart_2100270.html |
|