Results 1 to 5 of 5

Thread: Cannot attach QScrollBar to QwtPlot

  1. #1
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Question Cannot attach QScrollBar to QwtPlot

    hello,
    Background :
    I have Plot a Histogram on QwtPlot using QwtPlotHistogram. then I have been able to magnify the plot in right direction only. Now as I magnify the plot I need A ScrollBar to view the zoomed data. for this I added a QScrollBar.
    I am trying to attach a QScrollBar to My QwtPlot .

    But I can't connect the QScrollBAr to QwtPlot.

    the code which I am trying is : (this is in MyPlot 's constructor)

    QScrollBar *hScrollBar = new QScrollBar(Qt::Horizontal,plot->canvas());
    connect(hScrollBar,SIGNAL(sliderMoved(double)),thi s->canvas(),SLOT(horizontal_scroll(double)));

    horizontal_scroll is my method as :

    void MyPlot:: horizontal_scroll(double value)
    {
    setAxisScale(2, value, value + 105.0);
    //plot->setAxisScale(0,50,20);
    replot();
    }

    I am not getting where I am going wrong....!!

    Thanx in advance...!

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cannot attach QScrollBar to QwtPlot

    You also have to write layout code ( moving + resizing the scrollbar ). If you want to have it on top of the plot canvas you have to implement an event filter catching resize and move events for the canvas.

    Again look at the ScrollZoomer from the realtime example - it does this.

    Uwe

  3. The following user says thank you to Uwe for this useful post:

    mohini (4th January 2011)

  4. #3
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Question Re: Cannot attach QScrollBar to QwtPlot

    thanx Uwe ..!
    But, ScrollZoomer implements QWtPlotZoomer whereas I am using QwtPlotMagnifier ...
    Its adding to my confusion ...!!
    Can you please elaborate so that I can get it cleared ...!!

  5. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cannot attach QScrollBar to QwtPlot

    Event filtering is a general Qt feature ( see QObject::installEventFilter() ).

    Assuming your plot widget owns your scroll bar, then you have to do something like this:

    Qt Code:
    1. class YourPlot: public QwtPlot
    2. {
    3. public:
    4. YourPlot( ... )
    5. {
    6. ....
    7. m_scrollBar = new QScrollBar( Qt::Horizontal, this );
    8. canvas()->installEventFilter(this);
    9. }
    10.  
    11. virtual bool eventFilter(QObject *object, QEvent *e)
    12. {
    13. if ( object == canvas() )
    14. {
    15. if ( e->type() == QEvent::Move )
    16. {
    17. m_scrollBar->move( ... );
    18. }
    19. if ( e->type() == QEvent::Resize )
    20. {
    21. m_scrollBar->resize( ... );
    22. }
    23. }
    24. return QwtPlot::eventFilter( object, event );
    25. }
    26. };
    To copy to clipboard, switch view to plain text mode 

    But of course you can put your scrollbar also everywhere else independant of the plot canvas geometry.

    Uwe

  6. #5
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot attach QScrollBar to QwtPlot

    thanx Uwe ..
    I could attach the QScrollBar to my Plot...
    It works fine when I first magnify and then scroll forward or backward. But, the problem is if I again magnify the plot (i.e after 1 magnification and 1 scrolling ) while scrolling back it doesn't scroll to right scale. I can recognize that the problem is due to 2 calls to setAxisScale on xBottom axis viz., one in magnifier::rescale (overloaded method) and the other call is in my horizontal_scroll method (SLOT for horizontal scrollbar...)

    These two methods are :

    void MyMagnifier::rescale(double factor)
    {

    if ( factor == 1.0 || factor == 0.0)
    return;
    hold=factor;
    if(cnt!=-1)
    {

    //mag_stack[mag_cnt]=mag_stack[mag_cnt-1]+factor;
    mag_stack[mag_cnt]=factor;
    magnify_flag=1; // indicate that we have scrolled before magnifying
    }
    else
    {
    magnify_flag=0;
    }

    bool doReplot = false;
    QwtPlot* plt = plot();

    const bool autoReplot = plt->autoReplot();
    plt->setAutoReplot(false);

    //for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) // no need to scale all axes... as zooming only along X-Axis
    //{

    int axisId=2;
    const QwtScaleDiv *scaleDiv = plt->axisScaleDiv(axisId);
    if ( isAxisEnabled(axisId) && scaleDiv->isValid() )
    {
    plt->setAxisScale(axisId, scaleDiv->lowerBound() * factor , scaleDiv->upperBound() * factor);
    doReplot = true;
    }
    // }

    plt->setAutoReplot(autoReplot);

    if ( doReplot )
    {
    plt->replot();
    }
    }


    and

    void TVPlot:: horizontal_scroll(int value)
    {
    scroll_flag=1;
    hold_scroll=value;
    static int prev_val=0;
    const QwtScaleDiv *scaleDiv = axisScaleDiv(2);

    if(value >prev_val && prev_val>=0)
    {
    cnt++;
    stack[cnt]=value;
    prev_val=value;
    setAxisScale(2, scaleDiv->lowerBound() + value , scaleDiv->upperBound() + value);

    }
    else if(prev_val>0)
    {
    int temp;
    if(magnify_flag==1)
    {
    int i =mag_cnt;
    if(i >-1) // trying to correlate magnification factor with back scrolling value // NOT working
    {
    stack[cnt]=(int)(stack[cnt]-mag_stack[i]);
    i--;
    setAxisScale(2, scaleDiv->lowerBound() - stack[cnt] , scaleDiv->upperBound() - stack[cnt]);
    cnt--;
    }

    }
    else
    {
    setAxisScale(2, scaleDiv->lowerBound() - stack[cnt] , scaleDiv->upperBound() - stack[cnt]);
    cnt--;
    }
    prev_val=value;
    }

    replot();
    }

    But , I am not getting how should I correlate the magnify axis scale to scroll axis scale ...
    stack[] contains the scroll factor each time I scroll the plot.. I have done this so that I can scroll back by same factor... because while scrolling the scroll factor is 1,2,3,4.... (an A.P.)

    Please suggest how to correlate these factors to magnification factor ..

    I am not using the virtual bool eventFilter(QObject *object, QEvent *e) function ...!

Similar Threads

  1. attach more widgets to a QScrollArea
    By franco.amato in forum Qt Programming
    Replies: 39
    Last Post: 19th January 2010, 16:47
  2. QSharedMemory won't attach
    By MattPhillips in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2009, 15:45
  3. Replies: 3
    Last Post: 16th May 2009, 10:16
  4. Replies: 6
    Last Post: 14th May 2009, 12:02

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
  •  
Qt is a trademark of The Qt Company.