namespace Ui {
class Form_Pollutant;
}
class Form_Pollutant : public QWidget
{
Q_OBJECT
public:
explicit Form_Pollutant(QWidget *parent = 0);
~Form_Pollutant();
void initUI();
QList<Pollutant *> list_plt;
QQueue<int> pointY;
void mouseMoveEvent(QMouseEvent * event);
// bool eventFilter(QObject *watched, QEvent *event);
public slots:
void confirmCheck();
void cancelCheck();
void checkedBtn();
private:
int currentCheckPlt;
QHBoxLayout *bl;
QVBoxLayout *vvmain;
QHBoxLayout *headHLay;
QWidget *mainwd;
QWidget *boxs[20];
QVBoxLayout *mainVLayout;
int tmpposy,y1,y2;
QScrollArea *scrollArea;
QList<QPushButton *> listBtn;
QPushButton *pushButton[100];
private:
Ui::Form_Pollutant *ui;
signals:
void currentCheckdPlt(int id, QString name);
};
#include "form_pollutant.h"
#include "ui_form_pollutant.h"
Form_Pollutant::Form_Pollutant(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form_Pollutant)
{
ui->setupUi(this);
currentCheckPlt = -1;
}
Form_Pollutant::~Form_Pollutant()
{
delete vvmain;
delete mainVLayout;
delete mainwd;
qDeleteAll(list_plt);
delete ui;
}
void Form_Pollutant::initUI()
{
tmpposy=0;
y1=0;
y2=0;
this->setGeometry(0,0,WW, WH);
Pollutant plt;
list_plt = plt.selAll(32);
vvmain = new QVBoxLayout(this);
headHLay = new QHBoxLayout();
headHLay->setSpacing(6);
headHLay->setContentsMargins(11, 11, 11, 11);
headHLay->setObjectName(QStringLiteral("headHLay"));
headHLay->setContentsMargins(0, 0, 0, 0);
QSpacerItem *horizontalSpacer = new QSpacerItem(WW,WH*0.1, QSizePolicy::Expanding, QSizePolicy::Minimum);
headHLay->addItem(horizontalSpacer);
QPushButton *pushButton_btnCancel = new QPushButton();
pushButton_btnCancel->setMinimumSize(WW*0.2,WH*0.1);
pushButton_btnCancel->setObjectName(QStringLiteral("pushButton_btnCancel"));
pushButton_btnCancel->setText("取 消");
connect(pushButton_btnCancel,SIGNAL(clicked()),this,SLOT(cancelCheck()));
headHLay->addWidget(pushButton_btnCancel);
QPushButton *pushButton_btnOK = new QPushButton();
pushButton_btnOK->setMinimumSize(WW*0.2,WH*0.1);
pushButton_btnOK->setObjectName(QStringLiteral("pushButton_btnOK"));
pushButton_btnOK->setText("确认选择");
connect(pushButton_btnOK,SIGNAL(clicked()),this,SLOT(confirmCheck()));
headHLay->addWidget(pushButton_btnOK);
vvmain->addLayout(headHLay);
scrollArea = new QScrollArea();
scrollArea->setGeometry(0,100,800,400);
scrollArea->setMinimumSize(800,480);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mainwd = new QWidget();
mainwd->setObjectName(QStringLiteral("mainwd"));
mainwd->setMinimumWidth(WW-10);
mainVLayout = new QVBoxLayout(mainwd);
mainVLayout->setMargin(0);
int row = list_plt.size()/5;
int endRow =0;
for(int j=0;j<=row;j++)
{
boxs[j] = new QWidget();
boxs[j]->setMinimumSize(WW-20,80);
bl = new QHBoxLayout(boxs[j]);
bl->setMargin(1);
for(int i=0;i<5;i++)
{
if(endRow<list_plt.size())
{
pushButton[i+j*5] = new QPushButton();
pushButton[i+j*5]->setMinimumHeight(75);
pushButton[i+j*5]->setText(list_plt[i+j*5]->name);
pushButton[i+j*5]->setObjectName(QString::number(list_plt[i+j*5]->id));
pushButton[i+j*5]->setStyleSheet("background-color: rgb(94, 211, 51);");
bl->addWidget(pushButton[i+j*5]);
connect(pushButton[i+j*5],SIGNAL(clicked()),this,SLOT(checkedBtn()));
}
endRow++;
}
mainVLayout->addWidget(boxs[j]);
}
mainwd->setLayout(mainVLayout);
scrollArea->setWidget(mainwd);
vvmain->addWidget(scrollArea);
}
void Form_Pollutant::checkedBtn()
{
currentCheckPlt =sender()->objectName().toInt();
qDebug()<<"checked :"<<list_plt[currentCheckPlt]->name;
}
void Form_Pollutant::mouseMoveEvent(QMouseEvent * event)
{
pointY.enqueue(event->globalY());
if(pointY.size()>2)
{
y1 = pointY.dequeue();
y2 = pointY.dequeue();
// if(y1 == 0) y1 = event->globalY();
// else if(y2 == 0) y2 = event->globalY();
qDebug()<<"y="<<event->globalY()<<",scrollArea="<<scrollArea->verticalScrollBar()->value()
<<" y1="<<y1<<",y2="<<y2;
if((y2-y1)>10 || (y1-y2)>10)
{
qDebug()<<"qqqqqqqqqq";
}
else if(y1 != 0 && y2 != 0)
{
if(y2-y1 > 0)
{
qDebug()<<"down.......";
scrollArea->verticalScrollBar()->setSliderPosition(
scrollArea->verticalScrollBar()->value()-((y2-y1)*3));
}else if(y1-y2 > 0)
{
qDebug()<<"up.......";
scrollArea->verticalScrollBar()->setSliderPosition(
scrollArea->verticalScrollBar()->value()+((y1-y2)*3));
}
}
}
}
void Form_Pollutant::cancelCheck()
{
delete vvmain;
delete mainVLayout;
delete mainwd;
qDeleteAll(list_plt);
this->close();
}
void Form_Pollutant::confirmCheck()
{
if(currentCheckPlt>=0)
emit currentCheckdPlt(currentCheckPlt,list_plt[currentCheckPlt]->name);
this->close();
}
arm 电阻屏触摸滑动,因为电阻的滑动效果差,故放弃使用,如使用频率低的话或可尝试