使用qt中的信号/插槽更新gui

时间:2014-10-13 09:05:33

标签: qt

我从套接字中获取数据,其中一些参数每秒都在变化。我正在向我的gui投射数据。我想在每一秒后更新我的gui。

这是我的sockettest类,我正在接收数据,我想在战术小部件中绘制数据。

  void socketTest::readyRead()
  {

    //QByteArray buffer;
    int numRead = 0, numReadTotal = 0;
    char buffer[180];

     socketTest *a = new socketTest();
     tacticalwidget *b = new tacticalwidget();

    forever {
        numRead  = socket->read(buffer, 180);

        // do whatever with array

        numReadTotal += numRead;
        memcpy (detailed_track_data, buffer, sizeof(buffer));
        //qDebug() << sizeof(buffer);
        amp = qToBigEndian(detailed_track_data[0].amplitude);
        theta = qToBigEndian(detailed_track_data[0].doa)*0.1;

        emit SIGNAL(valuechanged());
        connect(a,SIGNAL(valuechanged()), b , SLOT(tacticalwidget::paintEvent()));

        int track_state = qToBigEndian(detailed_track_data[0].track_status);
        int bit1 = 2^5;
        int bit2 = 2^4;

        //check hostility level for hostile
        if(((track_state) & (bit1)) == 0 && ((track_state) & (bit2)) == 1)
            tracktype = 0;// Hostile
            else
               //check hostility level for unknown
                if(((track_state) & (bit1)) == 0 && ((track_state) & (bit2)) == 0)
                        tracktype = 1;//unknown
                 else
                    tracktype = 2;//friendly

        if (numRead == 0 && !socket->waitForReadyRead())
            break;
    }

TacticalWidget代码:

    void tacticalwidget::paintEvent(QPaintEvent */*e*/)
      {

       QSize sz = size();
       int radius;

       QPainter painter(this);
       painter.setRenderHint(QPainter::Antialiasing,true);

       QBrush br;
       br.setColor(Qt::black);

        painter.setBrush(br);
        painter.fillRect(0,0,sz.width(),sz.height(),Qt::SolidPattern);

        int centrex = sz.width()/2 ;
        int centrey = sz.height()/2;

        if (centrex < centrey )
          radius = centrex -4;
         else
         radius = centrey -4;


         painter.setPen(QPen(Qt::green,2,Qt::SolidLine,Qt::RoundCap));
         painter.drawEllipse(centrex - radius,centrey - radius,2*radius,2*radius);

         painter.setPen(QPen(Qt::yellow,2,Qt::SolidLine,Qt::RoundCap));
         painter.drawEllipse(centrex - (radius*2/3),centrey-radius*2/3),(radius*4  /3),(radius*4/3));

       painter.setPen(QPen(Qt::red,2,Qt::SolidLine,Qt::RoundCap));
       painter.drawEllipse(centrex - radius/3,centrey - radius/3,radius*2/3,radius*2/3);


        painter.setPen(QPen(Qt::white,2,Qt::SolidLine,Qt::RoundCap));
       painter.drawLine(centrex,0,centrex,centrey*2);
        painter.drawLine(0,centrey,centrex*2,centrey);

       painter.drawText(centrex - radius + 4,centrey,"270");
    painter.drawText(centrex,centrey-radius + 4 ,"N");
    painter.drawText(centrex,centrey+radius -4,"180");
    painter.drawText(centrex + radius -4,centrey,"90");

    painter.setPen(QPen(Qt::magenta,2,Qt::SolidLine,Qt::RoundCap));
    double theta = 45 * PI /180;
    painter.drawLine(centrex,centrey,centrex + radius * sin(theta),centrey - radius  *       cos(theta));

    //Track Display

    double amplitudefactor = (theObj.amp + 80)/100;
    //int trackNo = i+1;
    double pointx,pointy;
    int circleradius,innerradius = 0;

    switch(theObj.tracktype)
    {
    case 0://hostile
        circleradius = radius/3;
        pointx =centrex + circleradius * sin(theObj.theta) * amplitudefactor;
        pointy = centrey - circleradius * cos(theObj.theta)* amplitudefactor;

        painter.setPen(QPen(Qt::red,2,Qt::SolidLine,Qt::RoundCap));
        painter.drawImage(pointx,pointy,QImage("E:\\bitmaps\\rbitmap.png"));
        break;
    case 1://unknown
        circleradius = radius/3;
        innerradius = radius/3;

        pointx =centrex + innerradius * sin(theObj.theta) + circleradius *    sin(theObj.theta) * amplitudefactor;
        pointy = centrey - innerradius * cos(theObj.theta) - circleradius * cos(theObj.theta)* amplitudefactor;

        painter.setPen(QPen(Qt::yellow,2,Qt::SolidLine,Qt::RoundCap));
        painter.drawImage(pointx,pointy,QImage("E:\\bitmaps\\ybitmap.png"));
        break;
    case 2: //friendly
        circleradius = radius/3;
        innerradius = radius *2/3;

        pointx =centrex + innerradius * sin(theObj.theta) + circleradius * sin(theObj.theta) * amplitudefactor;
        pointy = centrey - innerradius * cos(theObj.theta) - circleradius * cos(theObj.theta)* amplitudefactor;

        painter.setPen(QPen(Qt::green,2,Qt::SolidLine,Qt::RoundCap));
        painter.drawImage(pointx,pointy,QImage("E:\\bitmaps\\gbitmap.png"));
        break;
    default:
        break;



    }
   // setStyleSheet("background-color: rgb(0, 0, 0);");
}

1 个答案:

答案 0 :(得分:1)

  1. 您错误地处理了readyRead()中的套接字,请检查my other answer
  2. 你正在发出信号然后连接它,而你正在做这么多次,这是一个很大的问题。
  3. 你搞砸了有关endian的东西,请查看QDataStream类(它假设默认流是大端的)。
  4. 一般来说,你有很好的意大利面条代码所以我并不感到惊讶它不起作用。