Results 1 to 2 of 2

Thread: scroll bars, qgraphicsview, qgraphicsscene, ...

  1. #1
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default scroll bars, qgraphicsview, qgraphicsscene, ...

    Hello,

    I have a program that uses the Qt Graphics Framework to draw different graphic items. For that I'm using the QGraphicsItem, QGraphicsScene and the QGraphicsView subclasses.
    I put the QGraphicsView subclass in a QMainWindow (a subclass too).
    When I'm starting my program I have 2 scroll bars one horizontal and one vertical. When I'm resizing the QMainWindow these scroll bars are remaining always with the same size ,or may be position, I don't know how to call it but they don't have the normal behavior...
    When I put an item on the scene but in the border between the visible and the not visible part of the scene i.e. the item appears not entirely or when I put the item entirely in the not visible, by the view, zone the scroll bars are remaining too at the same state ...
    The thing I want to have is that these scroll bars to be adaptable when I resize the main window or when I put some item outside the visible zone.
    I hope that my explanation is clearly enough

    Here is some source code of how I create the main windows with the view and the scene:

    Viewer.h
    Qt Code:
    1. #ifndef VIEWER_H_
    2. #define VIEWER_H_
    3.  
    4. #include <QtGui>
    5.  
    6. class Scheme;
    7.  
    8. class Viewer : public QGraphicsView {
    9.  
    10. public:
    11. Viewer(Scheme *scheme);
    12. ~Viewer();
    13.  
    14. protected:
    15. virtual void mouseMoveEvent(QMouseEvent *event);
    16. virtual void dragEnterEvent(QDragEnterEvent *event);
    17. virtual void dropEvent(QDropEvent *event);
    18. virtual void resizeEvent ( QResizeEvent * );
    19. };
    20.  
    21. #endif /* VIEWER_H_ */
    To copy to clipboard, switch view to plain text mode 

    Viewer.cpp
    Qt Code:
    1. #include "Viewer.h"
    2. #include "Scheme.h"
    3.  
    4.  
    5. Viewer::Viewer(Scheme *scheme) : QGraphicsView(scheme){
    6. setRenderHint(QPainter::Antialiasing);
    7. setAcceptDrops(true);
    8. setDragMode(QGraphicsView::RubberBandDrag);
    9.  
    10.  
    11. }
    12.  
    13. Viewer::~Viewer(){ }
    14.  
    15. void Viewer::mouseMoveEvent(QMouseEvent *event){
    16. QGraphicsView::mouseMoveEvent(event);
    17. }
    18.  
    19. void Viewer::dragEnterEvent(QDragEnterEvent *event){
    20. QGraphicsView::dragEnterEvent(event);
    21. }
    22.  
    23.  
    24. void Viewer::dropEvent(QDropEvent *event){
    25. QGraphicsView::dropEvent(event);
    26. }
    27.  
    28.  
    29. void Viewer::resizeEvent ( QResizeEvent * ){
    30.  
    31. scene()->setSceneRect(0,0,width(), height());
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    Scheme.h

    Qt Code:
    1. #ifndef SCHEME_H_
    2. #define SCHEME_H_
    3.  
    4.  
    5. #include <QGraphicsScene>
    6.  
    7. #include "Component.h"
    8.  
    9.  
    10. class Scheme : public QGraphicsScene {
    11.  
    12. Q_OBJECT
    13.  
    14. public:
    15.  
    16. Scheme(QMenu *compMenu, QObject *parent = 0);
    17. ~Scheme();
    18.  
    19. protected:
    20.  
    21. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    22. void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    23. void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    24.  
    25. virtual void dragEnterEvent(QGraphicsSceneDragDropEvent * event );
    26. virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
    27. virtual void dropEvent (QGraphicsSceneDragDropEvent * event );
    28.  
    29. };
    To copy to clipboard, switch view to plain text mode 

    Scheme.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include <QLabel>
    3. #include "Scheme.h"
    4.  
    5.  
    6.  
    7. Scheme::Scheme(QMenu *compMenu, QObject *parent) : QGraphicsScene(parent) {
    8.  
    9. line = 0;
    10. mode = MoveComponent;
    11. componentType = Component::EventIO;
    12. this->compMenu = compMenu;
    13. compColor = Qt::black;
    14. }
    15.  
    16. Scheme::~Scheme(){
    17.  
    18. void Scheme::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent){
    19.  
    20. QGraphicsScene::mousePressEvent(mouseEvent);
    21. }
    22.  
    23. void Scheme::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent){
    24.  
    25. QGraphicsScene::mouseMoveEvent(mouseEvent);
    26.  
    27. }
    28.  
    29. void Scheme::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent){
    30.  
    31. QGraphicsScene::mouseReleaseEvent(mouseEvent);
    32. }
    33.  
    34.  
    35. void Scheme::dragEnterEvent(QGraphicsSceneDragDropEvent * event ){
    36.  
    37. QGraphicsScene::dragEnterEvent(event);
    38. }
    39.  
    40. void Scheme::dropEvent (QGraphicsSceneDragDropEvent * event ){
    41.  
    42. QGraphicsScene::dropEvent(event);
    43.  
    44. }
    45.  
    46. void Scheme::dragMoveEvent(QGraphicsSceneDragDropEvent *event){
    47.  
    48. qDebug("Drag move event");
    49. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H_
    2. #define MAINWINDOW_H_
    3.  
    4.  
    5.  
    6. #include <QtGui>
    7.  
    8. class Scheme;
    9. class Viewer;
    10. class DragWidget;
    11.  
    12. class MainWindow : public QMainWindow {
    13.  
    14. Q_OBJECT
    15.  
    16. public:
    17. MainWindow();
    18. ~MainWindow();
    19.  
    20. private:
    21. QWidget *viewWidget;
    22. QHBoxLayout *viewLayout;
    23. Scheme *scheme; //list of all drawings that are participation in the scheme
    24. Viewer *view; //view port
    25.  
    26. };
    27.  
    28. #endif /* MAINWINDOW_H_ */
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include <QLabel>
    3. #include <QPainterPath>
    4. #include <QGraphicsRectItem>
    5. #include <QHBoxLayout>
    6. #include <QRect>
    7. #include "MainWindow.h"
    8. #include "Scheme.h"
    9. #include "Viewer.h"
    10.  
    11. MainWindow::MainWindow() : QMainWindow() {
    12.  
    13. scheme = new Scheme(itemMenu);
    14. scheme->setMode(Scheme::MoveComponent);
    15. scheme->setSceneRect(QRectF(0, 0, 1000, 1000));
    16.  
    17. view = new Viewer(scheme);
    18. view->setRenderHint(QPainter::Antialiasing);
    19. view->centerOn(0,0);
    20.  
    21. viewLayout = new QHBoxLayout();
    22. viewLayout->addWidget(view);
    23.  
    24. viewWidget = new QWidget;
    25. viewWidget->setLayout(viewLayout);
    26.  
    27. setCentralWidget(viewWidget);
    28. }
    29.  
    30.  
    31. MainWindow::~MainWindow(){
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    I removed some part of the code and I left just the constructors because I think this is the most important and I suppose that I should add something to the Viewer constructor in way that its scroll bars to become "adjustable"
    If you have any ideas ....

    Best regards,

    Anton

    PS:
    this is the main():

    Qt Code:
    1. #include <QtGui>
    2. #include <QCoreApplication>
    3.  
    4. #include "MainWindow.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10.  
    11. MainWindow mainWindow;
    12.  
    13. mainWindow.setGeometry(0, 0, 1000, 1000);
    14.  
    15. mainWindow.show();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: scroll bars, qgraphicsview, qgraphicsscene, ...

    when I'm starting my program I have 2 scroll bars one horizontal and one vertical. When I'm resizing the QMainWindow these scroll bars are remaining always with the same size ,or may be position
    check the reimplementation of resize event ....
    fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. QGraphicsScene & QGraphicsView help please
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 13th February 2017, 12:30
  2. Replies: 0
    Last Post: 5th March 2009, 06:54
  3. QGraphicsView and QGraphicsScene
    By alisami in forum Qt Programming
    Replies: 8
    Last Post: 4th December 2008, 10:10
  4. QGraphicsScene and QGraphicsView
    By rossd in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2007, 14:43
  5. QScrollArea's Scroll Bars
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 19th September 2006, 13:27

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.