PDA

View Full Version : How to draw smootly?



azazaz
22nd June 2016, 21:33
this code redraw two rectangles, every 10ms.
its looks ugly.
11997
maybe need use some interpolation? if need, how to?

All project in attached file
11996


maybe have better solution for fast drawing?


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qaudiolevel.h"
#include <QThread>
#include <QDebug>

QList<QAudioLevel*> audioLevels;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->levelsLayout->setSpacing(0);

for(int i=0;i<2;i++)
{
QAudioLevel *level = new QAudioLevel(ui->centralWidget);
audioLevels.append(level);
ui->levelsLayout->addWidget(level);
}
trs = new Threadl();
connect(trs,SIGNAL(SetLevels()),this,SLOT(SetLevel s()));
trs->start();

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::SetLevels()
{
float val1=((0.26-0.21)*((float)rand()/RAND_MAX))+0.21;
float val2=((0.26-0.21)*((float)rand()/RAND_MAX))+0.21;
qDebug() << val1 << val2;
audioLevels.at(0)->setLevel(val1);
audioLevels.at(1)->setLevel(val2);
}

void Threadl::run()
{
while(true)
{
emit SetLevels();
QThread::msleep(10);
}
}


qpainter.cpp



#include "qaudiolevel.h"
#include <QPainter>
#include <QDebug>

QAudioLevel::QAudioLevel(QWidget *parent)
: QWidget(parent)
, m_level(0.0)
{
setMinimumHeight(15);
setMaximumHeight(180);
}

void QAudioLevel::setLevel(qreal level)
{
if (m_level != level) {
m_level = level;
update();
}
}

void QAudioLevel::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

QPainter painter(this);
qreal widthLevel = m_level * height();
painter.fillRect(0,0, width(),height(), Qt::black);
painter.fillRect(0,height()-widthLevel,50, height(), Qt::red);




}

anda_skoa
23rd June 2016, 10:10
Maybe you should describe what you want to achieve, that looks like it does what the code says it should do.

What you can do in any case is to set the Qt::WA_NoSystemBackground widget attribute. You are filling all pixels, having Qt fill the background before calling paintEvent() is just a waste of CPU time.

Btw, what do you need the thread for? Anything particular you are planning to add to it?

Cheers,
_