Results 1 to 2 of 2

Thread: How to draw smootly?

  1. #1
    Join Date
    Feb 2014
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to draw smootly?

    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

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qaudiolevel.h"
    4. #include <QThread>
    5. #include <QDebug>
    6.  
    7. QList<QAudioLevel*> audioLevels;
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. ui->levelsLayout->setSpacing(0);
    15.  
    16. for(int i=0;i<2;i++)
    17. {
    18. QAudioLevel *level = new QAudioLevel(ui->centralWidget);
    19. audioLevels.append(level);
    20. ui->levelsLayout->addWidget(level);
    21. }
    22. trs = new Threadl();
    23. connect(trs,SIGNAL(SetLevels()),this,SLOT(SetLevels()));
    24. trs->start();
    25.  
    26. }
    27.  
    28. MainWindow::~MainWindow()
    29. {
    30. delete ui;
    31. }
    32.  
    33. void MainWindow::SetLevels()
    34. {
    35. float val1=((0.26-0.21)*((float)rand()/RAND_MAX))+0.21;
    36. float val2=((0.26-0.21)*((float)rand()/RAND_MAX))+0.21;
    37. qDebug() << val1 << val2;
    38. audioLevels.at(0)->setLevel(val1);
    39. audioLevels.at(1)->setLevel(val2);
    40. }
    41.  
    42. void Threadl::run()
    43. {
    44. while(true)
    45. {
    46. emit SetLevels();
    47. QThread::msleep(10);
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 


    qpainter.cpp
    Qt Code:
    1. #include "qaudiolevel.h"
    2. #include <QPainter>
    3. #include <QDebug>
    4.  
    5. QAudioLevel::QAudioLevel(QWidget *parent)
    6. : QWidget(parent)
    7. , m_level(0.0)
    8. {
    9. setMinimumHeight(15);
    10. setMaximumHeight(180);
    11. }
    12.  
    13. void QAudioLevel::setLevel(qreal level)
    14. {
    15. if (m_level != level) {
    16. m_level = level;
    17. update();
    18. }
    19. }
    20.  
    21. void QAudioLevel::paintEvent(QPaintEvent *event)
    22. {
    23. Q_UNUSED(event);
    24.  
    25. QPainter painter(this);
    26. qreal widthLevel = m_level * height();
    27. painter.fillRect(0,0, width(),height(), Qt::black);
    28. painter.fillRect(0,height()-widthLevel,50, height(), Qt::red);
    29.  
    30.  
    31.  
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw smootly?

    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,
    _

Similar Threads

  1. Replies: 10
    Last Post: 11th February 2011, 00:31
  2. How to draw a arc
    By ProTonS in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2009, 16:41
  3. Draw Line
    By aloysiusjegan in forum Qwt
    Replies: 4
    Last Post: 12th February 2009, 12:02
  4. Draw on a Progress Bar
    By fruzzo in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2007, 16:31
  5. From calculator: how to draw
    By mattia in forum Newbie
    Replies: 1
    Last Post: 26th October 2007, 12:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.