PDA

View Full Version : Programming a chart... scroll problem



kea_
14th October 2011, 08:34
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

6978

UChart.h


#ifndef UCHART_H
#define UCHART_H

#include <QtGui>


class UChart : public QScrollArea
{
public:
UChart();
~UChart();

protected:

private:
int m_width;
int m_fixed;

void paintEvent(QPaintEvent *event);
void scrollContentsBy(int dx, int dy);
void resizeEvent(QResizeEvent *event);
void setScrollBars();
};

#endif // UCHART_H


UChart.cpp


#include "UChart.h"

UChart::UChart()
: QScrollArea(){

setObjectName("UChart");
setStyleSheet(" #UChart { background: white } ");
setFrameStyle(QFrame::NoFrame);

horizontalScrollBar()->setRange(0, 1);
}


UChart::~UChart(){}


void UChart::paintEvent(QPaintEvent *event){
int dx = horizontalScrollBar()->value();
int dy = verticalScrollBar()->value();
m_fixed = 100;
m_width = 300;

QPainter painter(viewport());
painter.translate(-dx, 0);

painter.setPen("#8e520c");
painter.setBrush(QBrush("#fbf2a3"));
painter.drawRect(-1, -1, m_fixed, size().height()+1);


painter.setPen("#000000");
painter.drawText(m_fixed + 5, 50, "Hello World!");

setScrollBars();
}


void UChart::scrollContentsBy(int dx, int dy){
viewport()->scroll(dx, dy, QRect(m_fixed, 0, m_width, size().height()));
}


void UChart::resizeEvent(QResizeEvent *event){
//viewport()->scroll(dx, dy, QRect(m_fixed, 0, m_width, size().height()));
}


void UChart::setScrollBars(){
if(m_width > size().width()){
horizontalScrollBar()->setRange(0, m_width-size().width());
horizontalScrollBar()->setPageStep(m_width);
}else horizontalScrollBar()->setRange(0, 0);
}

FelixB
14th October 2011, 09:38
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

kea_
14th October 2011, 11:07
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