Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

UI doesnt refresh correctly



  • Im reading some datas on serial port. My user ınterface doesnt refrest the datas correctly. I want to see the last data on my gui, which is coming from serial . How can i do it with a timer

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        count =0;
        serial = new QSerialPort();
        open_Serial();
        m_timer = new QTimer();
        connect(m_timer,SIGNAL(timeout()),this, SLOT(on_joystick()));
        m_timer->setInterval(20);
        m_timer->start();
    
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::open_Serial()
    {
    
        serial->setPortName("/dev/ttyUSB0");
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
    
        if (serial->open(QIODevice::ReadWrite))
        {
              qDebug() << "ttyUSB0" << "is Open ";
              serial->clear();
              write_Json();
              connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);
            //connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_joystick);
        }
    
        if(serial->isOpen())
        {
            ui->Open->setChecked(true);
        }
    
    //    if(serial->disconnect())
    //    {
    //        ui->Close->setChecked(true);
    //    }
    
    }
    void MainWindow::on_Serial_Read()
    {
        QByteArray data = serial->readAll();
    
        s= data.size();
        if (s>14)
            s=14;
    
        memcpy(dizi, data.constData(), 14);
    
        if(dizi[1] < -500 || dizi[1] > 500 ||
                dizi[2] < -500 || dizi[2] > 500 ||
                dizi[3] < -500 || dizi[3] > 500 ||
                dizi[4] < -500 || dizi[4] > 500 ) {
           count ++;
            ui->lcdNumberError->display(count);
            qDebug() << "-->"<< dizi[1] << dizi[2]<< dizi[3]<< dizi[4]<< dizi[5]<< dizi[6];
        }
    
    }
    void MainWindow::on_joystick()
    {
    
        if(dizi[1] > 0 && dizi[1] < 600)
        {
            ui->progressBarAx->setValue(dizi[1]);
            ui->progressBarAxEksi->setValue(0);
        }
    
    
        if(dizi[2] > 0 && dizi[2] < 600)
        {
            ui->progressBarAy->setValue(dizi[2]);
            ui->progressBarAy->setValue(0);
        }
    
        if(dizi[3] > 0 && dizi[3] < 600 )
        {
            ui->progressBarBx->setValue(dizi[3]);
            ui->progressBarBxEksi->setValue(0);
        }
    
        if(dizi[4] > 0 && dizi[4] < 600 )
        {
            ui->progressBarBy->setValue(dizi[4]);
            ui->progressBarByEksi->setValue(0);
        }
    
    
        if(dizi[1] <0  )
        {
            //ui->progressBarAxEksi->setValue(dizi[1]*-1);
            ui->progressBarAxEksi->setValue(qAbs(dizi[1]));
        }
    
        if(dizi[2] <0 )
        {
            ui->progressBarAyEksi->setValue(qAbs(dizi[2]));
           // ui->progressBarAyEksi->setValue(dizi[2]*-1);
        }
    
        if(dizi[3] <0 )
        {
            //ui->progressBarBxEksi->setValue(dizi[3]*-1);
            ui->progressBarBxEksi->setValue(qAbs(dizi[3]));
        }
    
        if(dizi[4] <0 )
        {
          //ui->progressBarByEksi_2->setValue(dizi[4]*-1);
            ui->progressBarByEksi->setValue(qAbs(dizi[4]));
        }
    
    
            ui->lcdNumberAx->display(dizi[1]);
            ui->lcdNumberAy->display(dizi[2]);
            ui->lcdNumberBx->display(dizi[3]);
            ui->lcdNumberBy->display(dizi[4]);
    
            if(dizi[5] == 0)
            {
            //    ui->SwitchCSol->display(dizi[5]);
            //    ui->SwitchDSa->display(dizi[6]);
            ui->radioButtonSolO->setChecked(true);
            ui->radioButtonSagO->setChecked(false);
    
            }
            else if(dizi[5] == 1)
            {
            //    ui->SwitchCSol->display(dizi[5]);
            //    ui->SwitchDSa->display(dizi[6]);
            ui->radioButtonSolY->setChecked(true);
            ui->radioButtonSagY->setChecked(false);
    
            }
    
            else
            {
    
            ui->radioButtonSolA->setChecked(true);
            ui->radioButtonSagA->setChecked(false);
    
            }
    
            if(dizi[6] == 0)
            {
    
            ui->radioButtonSagO->setChecked(true);
            ui->radioButtonSolO->setChecked(false);
    
            }
            else if(dizi[6] == 1)
            {
    
            ui->radioButtonSagY->setChecked(true);
            ui->radioButtonSolY->setChecked(false);
    
            }
    
            else
            {
    
            ui->radioButtonSagA->setChecked(true);
            ui->radioButtonSolA->setChecked(false);
            }
    }
    

  • Lifetime Qt Champion

    Hi
    Im not sure what you want to use a timer for.
    You already have
    connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);

    so when data comes, Qt calls your on_Serial_Read

    Calling the on_joystick from a timer will not work good as
    it will be very out of sync with on_Serial_Read and data in "dizi"
    might not been read in yet.

    So if you see odd data , it would be that timer seems very fast and at
    the first calls, the "dizi" will not have valid values as on_Serial_Read didnt trigger yet.



  • @mrjj my progress bar was stuck on some point. It doesnt see the correct data. I doesnt refresh yourselft when new data comes


  • Lifetime Qt Champion

    @suslucoder
    Did you check the incoming data ?
    Its more likely some unexpected with the data than any refresh problem.

    In on_Serial_Read when dont you just call on_joystick ? ( i assume on_joystick is where you set the data to the GUI)



  • @mrjj on serial read, im reading from serial and on_joystick i put the datas on my gui. Where should i call joystick

    connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);
            //connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_joystick);
    

  • Lifetime Qt Champion

    @suslucoder
    hi
    at the very end of :on_Serial_Read()

    so get signal, read data and call on_joystick to display the 14 data you just copied to dizi



  • @mrjj I did it like that. But i dont know why in some data, progress bar doesnt refresh.


  • Lifetime Qt Champion

    @suslucoder

    well if dont move the bar, the most logical conclusion is that it gets the same value as before.

    So put in
    qDebug() << "-->"<< dizi[1] << dizi[2]<< dizi[3]<< dizi[4]<< dizi[5]<< dizi[6];
    in on_joystick (in top)
    and see when it dont move what data, if it still has the same value as before.



  • @mrjj said in UI doesnt refresh correctly:

    qDebug() << "-->"<< dizi[1] << dizi[2]<< dizi[3]<< dizi[4]<< dizi[5]<< dizi[6];

    I've checked, coming datas are true. Is there anything about threads? I have serial port and i have a gui. There may be something different about it


  • Lifetime Qt Champion

    @suslucoder
    Hi
    Only if you added threads yourself. else it cant be the issue.

    So that data is right in top of on_joystick ?
    that you are very sure about ?

    Since we dont clear dizi between readyRead
    it will have old values.

    So waht excactly do you see ?
    first it work, then it stops. or it never works ?



  • This post is deleted!

  • Lifetime Qt Champion

    @suslucoder
    Well it cant really be stuck as such.
    It means its current value is 15.

    You dont have any loops or anything that would prevent it from redrawing ?

    I think you will have to use the debugger and set breakpoint on the

    ui->progressBarXXXX

    and then see when you set data what it is.

    Its very unlikely the Widget simply don't redraw as that normally makes them white/odd if
    you have strangulated the event loop.



  • @mrjj I didnt understand what you mean. Am i doing something wrong?


  • Lifetime Qt Champion

    @suslucoder
    Hi
    Nope code looks fine but you seem think the widget would suddenly stop to draw and
    they rarely do and if they do, then it's very clear as it then looked messed up.

    So Im trying to guess at what could be wrong/go wrong.
    I think its just something with the data but you seems sure the values are newer and higher than 15 so
    I would like you to set a break point on the line where you set the progressbar value
    and then run & stop and inspect value in the dubugger to be truely sure that a value higher than 15 comes.

    other note, this line
    memcpy(dizi, data.constData(), 14);

    If less than 14 data comes, then old values are left in dizi.
    and what happens if data.constData() is less than 14 ?

    I think its something like that.



  • @mrjj data.constData() cant be less than 14. I set it. Its too weird


  • Lifetime Qt Champion

    @suslucoder
    Hi
    What you mean ?
    You dont set it at all

    QByteArray data = serial->readAll(); <<< this can read all from 1 to xxx bytes

    s= data.size(); << this is the size of data we read
    if (s>14)  << is is just some variable not related to data
        s=14;
    

    then we copy 14 from data
    memcpy(dizi, data.constData(), 14);

    so it dont matter how much data we actually got. we always copy 14



  • @mrjj said in UI doesnt refresh correctly:

    @suslucoder
    Hi
    What you mean ?

    I have

    int16_t dizi[7];
    

    Since I have an array which size is 7, i wrote something

    s= data.size(); 
    if (s>14)  
        s=14;
    

  • Lifetime Qt Champion

    @suslucoder

    But what do you then do with s to make it do anything ?



  • @mrjj Im just try to convert my binary data do int with that code script


  • Lifetime Qt Champion

    @suslucoder

    ok im not sure what s does.

    Anyway, could you try

    (in on_Serial_Read)
    memset(dizi, 0, 14);
    memcpy(dizi, data.constData(), data.size());

    in place ot the line
    memcpy(dizi, data.constData(), 14);

    and tell if it still stops at 15 ?