PDA

View Full Version : scroll bars, qgraphicsview, qgraphicsscene, ...



tonio
9th June 2009, 14:11
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



#ifndef VIEWER_H_
#define VIEWER_H_

#include <QtGui>

class Scheme;

class Viewer : public QGraphicsView {

public:
Viewer(Scheme *scheme);
~Viewer();

protected:
virtual void mouseMoveEvent(QMouseEvent *event);
virtual void dragEnterEvent(QDragEnterEvent *event);
virtual void dropEvent(QDropEvent *event);
virtual void resizeEvent ( QResizeEvent * );
};

#endif /* VIEWER_H_ */



Viewer.cpp



#include "Viewer.h"
#include "Scheme.h"


Viewer::Viewer(Scheme *scheme) : QGraphicsView(scheme){
setRenderHint(QPainter::Antialiasing);
setAcceptDrops(true);
setDragMode(QGraphicsView::RubberBandDrag);


}

Viewer::~Viewer(){ }

void Viewer::mouseMoveEvent(QMouseEvent *event){
QGraphicsView::mouseMoveEvent(event);
}

void Viewer::dragEnterEvent(QDragEnterEvent *event){
QGraphicsView::dragEnterEvent(event);
}


void Viewer::dropEvent(QDropEvent *event){
QGraphicsView::dropEvent(event);
}


void Viewer::resizeEvent ( QResizeEvent * ){

scene()->setSceneRect(0,0,width(), height());

}



Scheme.h




#ifndef SCHEME_H_
#define SCHEME_H_


#include <QGraphicsScene>

#include "Component.h"


class Scheme : public QGraphicsScene {

Q_OBJECT

public:

Scheme(QMenu *compMenu, QObject *parent = 0);
~Scheme();

protected:

void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);

virtual void dragEnterEvent(QGraphicsSceneDragDropEvent * event );
virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
virtual void dropEvent (QGraphicsSceneDragDropEvent * event );

};



Scheme.cpp




#include <QtGui>
#include <QLabel>
#include "Scheme.h"



Scheme::Scheme(QMenu *compMenu, QObject *parent) : QGraphicsScene(parent) {

line = 0;
mode = MoveComponent;
componentType = Component::EventIO;
this->compMenu = compMenu;
compColor = Qt::black;
}

Scheme::~Scheme(){

void Scheme::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent){

QGraphicsScene::mousePressEvent(mouseEvent);
}

void Scheme::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent){

QGraphicsScene::mouseMoveEvent(mouseEvent);

}

void Scheme::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent){

QGraphicsScene::mouseReleaseEvent(mouseEvent);
}


void Scheme::dragEnterEvent(QGraphicsSceneDragDropEvent * event ){

QGraphicsScene::dragEnterEvent(event);
}

void Scheme::dropEvent (QGraphicsSceneDragDropEvent * event ){

QGraphicsScene::dropEvent(event);

}

void Scheme::dragMoveEvent(QGraphicsSceneDragDropEvent *event){

qDebug("Drag move event");
}




MainWindow.h




#ifndef MAINWINDOW_H_
#define MAINWINDOW_H_



#include <QtGui>

class Scheme;
class Viewer;
class DragWidget;

class MainWindow : public QMainWindow {

Q_OBJECT

public:
MainWindow();
~MainWindow();

private:
QWidget *viewWidget;
QHBoxLayout *viewLayout;
Scheme *scheme; //list of all drawings that are participation in the scheme
Viewer *view; //view port

};

#endif /* MAINWINDOW_H_ */




MainWindow.cpp




#include <QtGui>
#include <QLabel>
#include <QPainterPath>
#include <QGraphicsRectItem>
#include <QHBoxLayout>
#include <QRect>
#include "MainWindow.h"
#include "Scheme.h"
#include "Viewer.h"

MainWindow::MainWindow() : QMainWindow() {

scheme = new Scheme(itemMenu);
scheme->setMode(Scheme::MoveComponent);
scheme->setSceneRect(QRectF(0, 0, 1000, 1000));

view = new Viewer(scheme);
view->setRenderHint(QPainter::Antialiasing);
view->centerOn(0,0);

viewLayout = new QHBoxLayout();
viewLayout->addWidget(view);

viewWidget = new QWidget;
viewWidget->setLayout(viewLayout);

setCentralWidget(viewWidget);
}


MainWindow::~MainWindow(){

}



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():



#include <QtGui>
#include <QCoreApplication>

#include "MainWindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);


MainWindow mainWindow;

mainWindow.setGeometry(0, 0, 1000, 1000);

mainWindow.show();

return a.exec();
}

wagmare
11th June 2009, 05:49
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);