QT中label显示变量问题!

来源:互联网 发布:des算法例题 编辑:程序博客网 时间:2024/06/10 11:48
class Display : public QWidget // cursor view
{
public:
    Display();
};

Display::Display() // construct view
{
    /* 打开gpio设备*/
    int dev_fd = open("/dev/gpio", O_RDWR | O_NONBLOCK);
    if ( dev_fd == -1 ) 
    {
        printf("Cann't open gpio device file\n");
        exit(1);
    }

    time_t rawtime; 
    time ( &rawtime );
    unsigned long atime = (unsigned long)rawtime;
    unsigned long ptime = (unsigned long)rawtime;

    //for(int i = 0; i < 2; i ++)
   // {
int count=0,low=0;
float fnd , maxlz, minlz, flz;
long data;
while((atime - ptime) != 10)   
{
    count += 1;
    read(dev_fd, &data, sizeof(data)); 

    //printf("%d\n",data); 

    if((int)data<1700)
    {
     low += 1;
    }
    time ( &rawtime ); 
    atime = (unsigned long)rawtime;
     }

//ptime = atime;
fnd = ( (float)low / (float)count ) * 100;
maxlz = fnd / 2.0;
minlz = (fnd -0.83) / 1.17;
flz = (maxlz + minlz) / 2.0;
     //printf("nongdu:%f\n", fnd); 
//printf("lizishu:%f\n",flz); 
    }

    setCaption( "Display" );
    QLabel *label;
    QString snd1,snd2;
    int ind1 = (int)fnd;
    int ind2 = ((int)(fnd*100)%100);
    snd1.setNum(ind1);
    snd2.setNum(ind2);

    QString slz1,slz2;
    int ilz1 = (int)flz;
    int ilz2 = ((int)(flz*100)%100);
    slz1.setNum(ilz1);
    slz2.setNum(ilz2);

QGridLayout *layout = new QGridLayout(this,2,5);
    QString nd = "nongdu:";
    label = new QLabel(this);
    label->setText(tr(nd));
layout->addWidget(label,0,0);
    label = new QLabel( this );
    label->setText( tr(snd1) );
layout->addWidget(label,0,1);
    label = new QLabel( this );
    label->setText( tr(".") );
layout->addWidget(label,0,2);
    label = new QLabel( this );
    label->setText( tr(snd2) );
layout->addWidget(label,0,3);
    label = new QLabel( this );
    label->setText( tr("%") );
layout->addWidget(label,0,4);

    QString lz = "lizishu:";
    label = new QLabel(this);
    label->setText(tr(lz));
layout->addWidget(label,1,0);
    label = new QLabel( this );
    label->setText( tr(slz1) );
layout->addWidget(label,1,1);
    label = new QLabel( this );
    label->setText( tr(".") );
layout->addWidget(label,1,2);
    label = new QLabel( this );
    label->setText( tr(slz2) );
layout->addWidget(label,1,3);
    label = new QLabel( this );
    label->setText( tr("Kpcs") );
layout->addWidget(label,1,4);
        //close(dev_fd);
}



int main( int argc, char **argv ) 

QApplication app( argc, argv );
Display w;
app.setMainWidget( &w ); 
w.show(); 
return app.exec();
0 0