Results 1 to 2 of 2

Thread: QRubberband and QcustomPlot

  1. #1
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QRubberband and QcustomPlot

    Hi all,

    i was wondering if anyone had used a QRubberBand in conjunction with the QCustomPlot class. I really like the class since I don't have to install anything to get it working but it doesn't have a picker that comes with it like Qwt.

    I have tried to get a QRubberband to work but due my serious lack of programming knowledge I have not been successful. My thinking is that it is hopefully a trivial exercise for most Qt programmers (I am still really new to C++) and I was hoping someone may have already been down this path and be able to give me some tips/tricks or help. My main problem seems to be when I try and set the geometry of the QRubberBand (it just seg faults on me), which in turn makes me think that I am not setting things correctly (I did go off of the examples given in the documentation where it uses mouse events to draw the rubberband).

    Cheers
    Oz

  2. The following user says thank you to OzQTNoob for this useful post:


  3. #2
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QRubberband and QcustomPlot

    Hi everyone,

    Okay so I managed to solve this one.

    firstly, a real cut down version of my code. I am not saying this compiles (as is since it is not the complete app) I am only showing the combination of code I needed to get a QRubberband happening on the QCustomPlot.
    The *.h file
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "qcustomplot.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. public slots:
    20.  
    21. private slots:
    22.  
    23. void mousePress(QMouseEvent* mevent);
    24.  
    25. void mouseMove(QMouseEvent *mevent);
    26.  
    27. void mouseRelease(QMouseEvent *mevent);
    28.  
    29. private:
    30. Ui::MainWindow *ui;
    31.  
    32. QRubberBand *rubberBand;
    33. QPoint origin;
    34.  
    35. };
    36.  
    37. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Okay so the big thing was I hunted around the QtSDK examples for any usage of QRubberband and found that they inserted a
    Qt Code:
    1. rubberBand = 0;
    To copy to clipboard, switch view to plain text mode 
    in the mainwindow constructor. So I did the same and YEAH BABY its ON!
    It would have been nice to see this in the QRubberband example since newbies like myself tend to copy and paste which may not be the best thing to do but its how we learn sometimes (or at least how I do : at 43 years of age I need to get results).

    Obviously I haven't shown the ui here, but suffice to say i added a widget and promoted it to a QCustomPlot and renamed it to customPlot. Being the newb that I am I was hoping someone could tell me what the
    Qt Code:
    1. rubberBand = 0;
    To copy to clipboard, switch view to plain text mode 
    does. I assume it initializes the pointer. I thought the
    Qt Code:
    1. if(!rubberBand)
    To copy to clipboard, switch view to plain text mode 
    in the mousePress slot was saying if it doesn't exist then lets create it but obviously not. I should also say that you will see that I check to see if the right mouse has been clicked and dragged since I want all the other cool zoom and axis drag functionality to remain in the customPlot (left mouse click/clickdrag). Lastly, I assume that until I destroy the widget (e.g. close the app) the actual rubberBand object will stay in memory and no new rubberBand will be created since if(!rubberBand) will always be false once the initial one was created. Anyhow, I hope this helps others who may use QCustomPlot as well.

    The *.cpp file.
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. void MainWindow::mousePress(QMouseEvent* mevent)
    5. {
    6. // if an axis is selected, only allow the direction of that axis to be dragged
    7. // if no axis is selected, both directions may be dragged
    8.  
    9. if(mevent->button() == Qt::RightButton){
    10. origin = mevent->pos();
    11. if (!rubberBand){
    12. rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->customPlot);
    13. }
    14. rubberBand->setGeometry(QRect(origin, QSize()));
    15. rubberBand->show();
    16. }else{
    17. /*This below is striaght from the QCustomPlot example and can be ignored in the context of the QRubberBand solution*/
    18. if (ui->customPlot->xAxis->selected().testFlag(QCPAxis::spAxis))
    19. ui->customPlot->setRangeDrag(ui->customPlot->xAxis->orientation());
    20. else if (ui->customPlot->yAxis->selected().testFlag(QCPAxis::spAxis))
    21. ui->customPlot->setRangeDrag(ui->customPlot->yAxis->orientation());
    22. else
    23. ui->customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
    24. }
    25. }
    26.  
    27. void MainWindow::mouseMove(QMouseEvent *mevent)
    28. {
    29. if(rubberBand)
    30. rubberBand->setGeometry(QRect(origin, mevent->pos()).normalized());
    31. }
    32.  
    33. void MainWindow::mouseRelease(QMouseEvent *mevent)
    34. {
    35. if(rubberBand)
    36. rubberBand->hide();
    37. }
    38.  
    39. MainWindow::MainWindow(QWidget *parent) :
    40. QMainWindow(parent),
    41. ui(new Ui::MainWindow)
    42. {
    43.  
    44. ui->setupUi(this);
    45. rubberBand = 0;/*<-THIS WAS THE KEY! Without it the setGeometry call in the mousePress slot always stuffed up*/
    46.  
    47. /*set up a custom plot: This is pulled straight from one of the QCustomPlot examples*/
    48. ui->customPlot->setInteractions(QCustomPlot::iRangeDrag | QCustomPlot::iRangeZoom | QCustomPlot::iSelectAxes |
    49. QCustomPlot::iSelectLegend | QCustomPlot::iSelectPlottables | QCustomPlot::iSelectTitle);
    50. ui->customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
    51. ui->customPlot->setRangeZoom(Qt::Horizontal|Qt::Vertical);
    52. ui->customPlot->xAxis->setRange(-20, 20);
    53. ui->customPlot->yAxis->setRange(-20, 20);
    54. ui->customPlot->setupFullAxesBox();
    55. ui->customPlot->setTitle("Interaction Example");
    56. ui->customPlot->xAxis->setLabel("x Axis");
    57. ui->customPlot->yAxis->setLabel("y Axis");
    58. ui->customPlot->legend->setVisible(true);
    59. QFont legendFont = font();
    60. legendFont.setPointSize(10);
    61. ui->customPlot->legend->setFont(legendFont);
    62. ui->customPlot->legend->setSelectedFont(legendFont);
    63. ui->customPlot->legend->setSelectable(QCPLegend::spItems); // legend box shall not be selectable, only legend items
    64.  
    65. // connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
    66. connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));
    67. connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
    68. connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));
    69. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Changing the border of a QRubberBand
    By Sergex in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2012, 04:17
  2. QRubberBand problem
    By nightroad in forum Newbie
    Replies: 15
    Last Post: 29th March 2011, 16:11
  3. Select texts using the QRubberband
    By charlesprime in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2011, 07:14
  4. Use of QRubberBand
    By lni in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2009, 13:59
  5. Using QRubberBand class?!?!?
    By nupul in forum Newbie
    Replies: 3
    Last Post: 1st April 2006, 18:19

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.