this code redraw two rectangles, every 10ms.
its looks ugly.
ezgif-2422137665.gif
maybe need use some interpolation? if need, how to?
All project in attached file
TestQPainterSlowdrawing.zip
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
) : 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(SetLevels()));
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();
}
}
#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(SetLevels()));
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);
}
}
To copy to clipboard, switch view to plain text mode
qpainter.cpp
#include "qaudiolevel.h"
#include <QPainter>
#include <QDebug>
QAudioLevel
::QAudioLevel(QWidget *parent
) , m_level(0.0)
{
setMinimumHeight(15);
setMaximumHeight(180);
}
void QAudioLevel::setLevel(qreal level)
{
if (m_level != level) {
m_level = level;
update();
}
}
{
Q_UNUSED(event);
qreal widthLevel = m_level * height();
painter.fillRect(0,0, width(),height(), Qt::black);
painter.fillRect(0,height()-widthLevel,50, height(), Qt::red);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks