Results 1 to 3 of 3

Thread: Programming a chart... scroll problem

  1. #1
    Join Date
    May 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Programming a chart... scroll problem

    Hello together,

    I like to write a little Chart for my program.
    This should work in this way.
    Left must be a legend that must be fixed.
    And on the right hand site there should be the scrollable area.
    I like just to scroll on the horizontal direction.

    If I start scrolling everything works fine.
    But if I resize the scrollarea it has a strange behavior and the scrollarea works in a wrong way.

    Has any body a solution for my problem.

    Thanks an I hope you can follow my bad english.
    Heiko

    chart.jpg

    UChart.h
    Qt Code:
    1. #ifndef UCHART_H
    2. #define UCHART_H
    3.  
    4. #include <QtGui>
    5.  
    6.  
    7. class UChart : public QScrollArea
    8. {
    9. public:
    10. UChart();
    11. ~UChart();
    12.  
    13. protected:
    14.  
    15. private:
    16. int m_width;
    17. int m_fixed;
    18.  
    19. void paintEvent(QPaintEvent *event);
    20. void scrollContentsBy(int dx, int dy);
    21. void resizeEvent(QResizeEvent *event);
    22. void setScrollBars();
    23. };
    24.  
    25. #endif // UCHART_H
    To copy to clipboard, switch view to plain text mode 

    UChart.cpp
    Qt Code:
    1. #include "UChart.h"
    2.  
    3. UChart::UChart()
    4.  
    5. setObjectName("UChart");
    6. setStyleSheet(" #UChart { background: white } ");
    7. setFrameStyle(QFrame::NoFrame);
    8.  
    9. horizontalScrollBar()->setRange(0, 1);
    10. }
    11.  
    12.  
    13. UChart::~UChart(){}
    14.  
    15.  
    16. void UChart::paintEvent(QPaintEvent *event){
    17. int dx = horizontalScrollBar()->value();
    18. int dy = verticalScrollBar()->value();
    19. m_fixed = 100;
    20. m_width = 300;
    21.  
    22. QPainter painter(viewport());
    23. painter.translate(-dx, 0);
    24.  
    25. painter.setPen("#8e520c");
    26. painter.setBrush(QBrush("#fbf2a3"));
    27. painter.drawRect(-1, -1, m_fixed, size().height()+1);
    28.  
    29.  
    30. painter.setPen("#000000");
    31. painter.drawText(m_fixed + 5, 50, "Hello World!");
    32.  
    33. setScrollBars();
    34. }
    35.  
    36.  
    37. void UChart::scrollContentsBy(int dx, int dy){
    38. viewport()->scroll(dx, dy, QRect(m_fixed, 0, m_width, size().height()));
    39. }
    40.  
    41.  
    42. void UChart::resizeEvent(QResizeEvent *event){
    43. //viewport()->scroll(dx, dy, QRect(m_fixed, 0, m_width, size().height()));
    44. }
    45.  
    46.  
    47. void UChart::setScrollBars(){
    48. if(m_width > size().width()){
    49. horizontalScrollBar()->setRange(0, m_width-size().width());
    50. horizontalScrollBar()->setPageStep(m_width);
    51. }else horizontalScrollBar()->setRange(0, 0);
    52. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Programming a chart... scroll problem

    do you want the legend to be always visible? Then you shouldn't derive UChart from QScrollArea. Create a horizontal layout with the legend as first element and a scrollarea as second. You could also use a Splitter if you want the legend to be resizeable...

    hth
    Felix

  3. #3
    Join Date
    May 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: Programming a chart... scroll problem

    Ok, I hoped I could resolve this problem width more elegant.
    But then this is the way.

    Thank you very much for your anwer.
    Heiko

Similar Threads

  1. QDial disabling key scroll and mouse wheel scroll
    By ldg20 in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2010, 23:05
  2. Replies: 0
    Last Post: 28th December 2009, 12:24
  3. Scroll Bar Problem
    By zgulser in forum Qt Programming
    Replies: 11
    Last Post: 3rd December 2009, 16:32
  4. Scroll Area problem
    By Lodhart in forum Qt Programming
    Replies: 0
    Last Post: 10th October 2009, 13:03
  5. Replies: 6
    Last Post: 14th April 2006, 05:39

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.