Qt4.7应用程序移植到iTOP4412板子上,首先先看成果图:
分为mainwindow led adc buzzer led 四个部分 ,每个部分都包含一个 .cpp .h .ui ,对于资源文件,也可以学我作一个分类
主窗口的设计
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#define Main_App_num 3 //APP数
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
UI_Init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Close_btn_Init()
{
QIcon close_icon(":/Main/close.png");
ui->Close_btn->setIcon(close_icon); //设置按钮图标
ui->Close_btn->setIconSize(QSize(25,25));
ui->Close_btn->setFlat(true); //取消边框
ui->Close_btn->setText(NULL); //取消文字
ui->Close_btn->setFocusPolicy(Qt::NoFocus); //取消注视
ui->Close_btn->setStyleSheet("background: rgb(100,100,100,15)"); //设置背景
//按钮单击时触发Main_close
connect(ui->Close_btn,SIGNAL(clicked(bool)),this, SLOT(Main_close()));
}
void MainWindow::Background_Init() //设置背景
{
QPixmap pixmap = QPixmap(":/Background/background.jpg").scaled(QSize(1024,600));
QPalette palette(this->palette());
palette.setBrush(QPalette::Background, QBrush(pixmap));
this->setPalette(palette);
}
void MainWindow::APP_List_Init()
{
//增加列表APP及图标
QIcon App_icon(":/Main/ADC.png");
ui->APP_List->addItem("ADC");
ui->APP_List->item(0)->setIcon(App_icon);
App_icon.addFile(":/Main/Buzzer.png");
ui->APP_List->addItem("Buzzer");
ui->APP_List->item(1)->setIcon(App_icon);
App_icon.addFile(":/Main/LED_APP.png");
ui->APP_List->addItem("LED");
ui->APP_List->item(2)->setIcon(App_icon);
ui->APP_List->setIconSize(QSize(30,30));
ui->APP_List->setFocusPolicy(Qt::NoFocus);
int APP_num;
for(APP_num=0;APP_numAPP_List->item(APP_num)->setTextAlignment(Qt::AlignCenter);
ui->APP_List->item(APP_num)->setForeground(QColor(Qt::black));
ui->APP_List->item(APP_num)->setSizeHint(QSize(50,65));
}
connect(ui->APP_List,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(App_Open(QListWidgetItem*)));
}
void MainWindow::App_Open(QListWidgetItem* )
{
switch(ui->APP_List->currentRow()) //根据当前的列表行选择显示哪个ui
{
case 0 : { ADC_APP_ui.showFullScreen(); break; }
case 1 : { Buzzer_APP_ui.showFullScreen();break; }
case 2 : { LED_APP_ui.showFullScreen();break; }
}
}
void MainWindow::Main_close()
{
this->close();
}
QPixmap MainWindow::PixmapToRound(const QPixmap &src, int radius) //将图标转化为圆形
{
if (src.isNull()) {
return QPixmap();
}
QSize size(2*radius, 2*radius);
QBitmap mask(size);
QPainter painter(&mask);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
painter.setBrush(QColor(0, 0, 0));
painter.drawRoundedRect(0, 0, size.width(), size.height(), 99, 99);
QPixmap image = src.scaled(size);
image.setMask(mask);
return image;
}
void MainWindow::Logo_Init()
{
QPixmap pixmap = QPixmap(":/Logo/logo.jpg").scaled(30,30,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
pixmap = PixmapToRound(pixmap, 15);
ui->Logo_Lable->setPixmap(pixmap);
}
void MainWindow::UI_Init()
{
APP_List_Init();//初始化app列表
Background_Init();//背景图片初始化
Close_btn_Init();//关闭按键初始化
Logo_Init();//Logo信息初始化
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "led.h"
#include "adc.h"
#include "buzzer.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void Main_close();
void App_Open(QListWidgetItem*);
private:
Ui::MainWindow *ui;
void UI_Init();
void Close_btn_Init();
void Background_Init();
void APP_List_Init();
void Logo_Init();
QPixmap PixmapToRound(const QPixmap &src, int radius);
LED LED_APP_ui;
ADC ADC_APP_ui;
Buzzer Buzzer_APP_ui;
};
#endif // MAINWINDOW_H
adc设计
adc.cpp
#include "adc.h"
#include "ui_adc.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ADC_NUM 1
ADC::ADC(QWidget *parent) :
QWidget(parent),
ui(new Ui::ADC)
{
ui->setupUi(this);
UI_Init();
}
ADC::~ADC()
{
delete ui;
}
void ADC::Close_btn_Init()
{
QIcon close_icon(":/ADC/back.png");
ui->Close_btn->setIcon(close_icon);
ui->Close_btn->setIconSize(QSize(25,25));
ui->Close_btn->setFlat(true);
ui->Close_btn->setText(NULL);
ui->Close_btn->setFocusPolicy(Qt::NoFocus);
ui->Close_btn->setStyleSheet("background: rgb(100,100,100,15)");
connect(ui->Close_btn, SIGNAL(clicked(bool)),this,SLOT(APP_close()));
}
void ADC::Background_Init()
{
QPixmap pixmap = QPixmap(":/Background/background.jpg").scaled(QSize(1024,600));
QPalette palette(this->palette());
palette.setBrush(QPalette::Background, QBrush(pixmap));
this->setPalette(palette);
}
void ADC::APP_close()
{
this->close();
}
void ADC::ADC_Refresh_Init()
{
QIcon Refresh_btn_icon(":/ADC/Refresh.png");
ui->Refresh_btn->setIcon(Refresh_btn_icon);
ui->Refresh_btn->setIconSize(QSize(35,35));
ui->Refresh_btn->setFlat(true);
ui->Refresh_btn->setText(NULL);
ui->Refresh_btn->setFocusPolicy(Qt::NoFocus);
ui->Refresh_btn->setStyleSheet("background: rgb(100,100,100,15)");
connect(ui->Refresh_btn, SIGNAL(clicked(bool)),this,SLOT(ADC_Refresh()));
}
void ADC::ADC_Value_Lable_Init()
{
ui->ADC_Value_Lable->setAlignment(Qt::AlignCenter);
ui->ADC_Value_Lable->setFocusPolicy(Qt::NoFocus);
}
void ADC::ADC_List_Init()
{
QIcon ADC_Status_icon(":/ADC/ADC_List.png");
ui->ADC_List->addItem("ADC_0");
ui->ADC_List->item(0)->setIcon(ADC_Status_icon);
ui->ADC_List->setIconSize(QSize(30,30));
ui->ADC_List->setFocusPolicy(Qt::NoFocus);
int ADC_num;
for(ADC_num=0;ADC_numADC_List->item(ADC_num)->setTextAlignment(Qt::AlignCenter);
ui->ADC_List->item(ADC_num)->setForeground(QColor(Qt::black));
ui->ADC_List->item(ADC_num)->setSizeHint(QSize(50,65));
}
ui->ADC_List->item(0)->setSelected(true);
}
void ADC::ADC_Refresh()
{
int fd = ::open("/dev/adc",0);
if(fd < 0)
{
return;
}
char buffer[30] = "";
int len = ::read(fd,buffer,sizeof(buffer)-1);
if(len > 0)
{
buffer[len] = '\0';
buffer[len-1] = '\0';
ui->ADC_Value_Lable->setText(buffer);
}
::close(fd);
}
QPixmap ADC::PixmapToRound(const QPixmap &src, int radius)
{
if (src.isNull()) {
return QPixmap();
}
QSize size(2*radius, 2*radius);
QBitmap mask(size);
QPainter painter(&mask);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
painter.setBrush(QColor(0, 0, 0));
painter.drawRoundedRect(0, 0, size.width(), size.height(), 99, 99);
QPixmap image = src.scaled(size);
image.setMask(mask);
return image;
}
void ADC::Logo_Init()
{
QPixmap pixmap = QPixmap(":/Logo/logo.jpg").scaled(30,30,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
pixmap = PixmapToRound(pixmap, 15);
ui->Logo_Lable->setPixmap(pixmap);
}
void ADC::UI_Init()
{
ADC_Value_Lable_Init();//ADC数值标签初始化
Close_btn_Init();//关闭按键初始化
ADC_Refresh_Init();//读取按键初始化
Background_Init();//背景图片初始化
ADC_List_Init();//ADC列表初始化
Logo_Init();//Logo信息初始化
}
adc.h
#ifndef ADC_H
#define ADC_H
#include
namespace Ui {
class ADC;
}
class ADC : public QWidget
{
Q_OBJECT
public:
explicit ADC(QWidget *parent = 0);
~ADC();
private slots:
void APP_close();
void ADC_Refresh();
private:
Ui::ADC *ui;
void UI_Init();
void Close_btn_Init();
void Background_Init();
void ADC_Refresh_Init();
void ADC_Value_Lable_Init();
void ADC_List_Init();
void Logo_Init();
QPixmap PixmapToRound(const QPixmap &src, int radius);
};
#endif // ADC_H
这里讲几点注意的点:
1.Qt应用程序也是通过设备节点控制底层,qt应用程序也是利用open ,ioctl,write,read,close等函数对底层进行控制。
2.qss对界面美化有很大帮助
这是我主界面app列表的qss:
#APP_List{
background-color: rgba(67, 67, 67,100);
border:1px solid rgba(20, 20, 20,50);
}
#APP_List::item:selected
{
border: 5px solid rgb(100,100,150,10);
}
#APP_List::item:selected:!active
{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 rgb(67,67,67,10) , stop: 1 rgb(120,120,120,100) );
}
#APP_List::item:selected:active
{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 rgb(67,67,67,10) , stop: 1 rgb(120,120,120,100) );
}
不加qss时的列表
加qss后的列表
在qss中设置了列表的框,列表的背景,列表选择框活动时和非活动时的颜色。
对于label也可以用qss进行美化,比如adc显示值的lable
border-width: 1px;
border-style: solid;
border-color: rgb(67, 67, 67,100);
color:rgba(20, 20,20,220);
background-color: rgb(67, 67, 67,100);
qss未进行美化时
qss美化后
3.在移植时进行调试,make后重新用qtcreator打开工程进行编译时可能会报错
这是原工程文件
qmake后会生成Makefile
再进行make生成移植后的板子上的程序
这时重新打开qtcreator可能会报错
提示你找不到头文件
这时候执行make clean就好了