Graphic View et remplissage
Bonjour à tous !
Je viens de réaliser un composant Qt / C++.
Mon composant hérite de QGraphicsView :
Customwidget.hpp:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QVariant>
#include <QVector>
class CustomWidget : public QGraphicsView
{
public :
/********************************/
/* Construction and destruction */
/********************************/
CustomWidget(QWidget *parent = 0);
~CustomWidget();
virtual void resizeEvent(QResizeEvent *event);
public slots:
void SetLeftMargin(int p_margin);
void SetRightMargin(int p_margin);
int GetLeftMargin();
int GetRightMargin();
void ResetLeftMargin();
void ResetRightMargin();
void SetComponentHeight(int p_height);
int GetComponentHeight();
int GetComponentWidth();
void SetComponentWidth(int p_width);
private:
QGraphicsScene* m_pScene; ///< The graphic scene containing the custom widget
CustomBackground* m_pBackground; ///< The background (Hérite de QGraphicsObject)
/**
* \brief QList of pointer to the different part of the component.
*/
QList<CustomScale*> m_ScaleList;
CustomProfil* m_pProfilBlock;
//[...] Plusieurs CustomTruc qui sont des pointeurs vers des sous-classes héritant de QGraphicsObject |
Customwidget.cpp:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| CustomWidget::CustomWidget(QWidget* parent) : QGraphicsView(parent){
setOptimizationFlags(QGraphicsView::DontSavePainterState);
m_pScene = new QGraphicsScene();
this->setFrameRect(QRect(0,0, component_width, component_height));
m_pScene->setSceneRect(0,0, component_width, component_height);
setScene(m_pScene);
m_pBackground = new CustomBackground(0, component_height, component_width);
m_pCustomSubWidget = new CustomSubWidget(m_pBackground, [...]);
m_pScene->addItem(m_pBackground);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
/**
* \brief Destructor
*/
CustomWidget::~CustomWidget()
{
delete m_pBackground;
m_pBackground = 0;
delete m_pScene;
m_pScene = 0;
}
void CustomWidget::resizeEvent(QResizeEvent* event)
{
Q_UNUSED(event);
fitInView( m_pBackground, Qt::KeepAspectRatio );
}
[...] Getter / Setter d'un intérêt inexistant pour cette question.
} |
CustomSubWidget.hpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <QPainter>
#include "CustomSubWidget.hpp"
/******************************************************************************/
/* CLASS IMPLEMENTATION */
/******************************************************************************/
/********************************/
/* Construction and destruction */
/********************************/
/**
* \brief Constructor
*/
CustomSubWidget::CustomSubWidget(QGraphicsItem* p_pParent, int p_y_position)
: QGraphicsObject(p_pParent), m_y_position(p_y_position)
{
}
/**
* \brief Destructor
*/
CustomSubWidget::~CustomSubWidget()
{
}
/********************************/
/*inherited from QGraphicsObject*/
/********************************/
/**
* \brief Overrides QGraphicsObject
*/
void CustomSubWidget::paint(QPainter* p_pPainter,
const QStyleOptionGraphicsItem* p_pOption,
QWidget* p_pWidget)
{
Q_UNUSED(p_pOption);
Q_UNUSED(p_pWidget);
DrawOADStatus(p_pPainter);
}
/**
* \brief Returns the bounding rect of the scale
*/
QRectF CustomSubWidget::boundingRect() const
{
return QRectF(40,0,1140,450);
}
/**
* \brief Draws OADStatus
*
* \param[in] p_pPainter The painter
*/
void CustomSubWidget::DrawCustomSubWidget(QPainter* p_pPainter)
{
p_pPainter->setPen(Qt::NoPen);
p_pPainter->save();
p_pPainter->setRenderHints(QPainter::Antialiasing);
p_pPainter->setPen(Qt::SolidLine);
p_pPainter->setPen(QColor("red"));
QRectF statusRect;
QPair<int, Status> StatusPair;
foreach(StatusPair, m_Status)
{
p_pPainter->setBrush(QColor("red"));
statusRect.setRect(OADStatusPair.first, m_y_position-7, 10, 14);
p_pPainter->drawRect(statusRect);
break;
}
p_pPainter->restore();
} |
CustomSubWidget.cpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| /**
#ifndef __CUSTOMWIDGET_HPP__
#define __CUSTOMWIDGET_HPP__
/******************************************************************************/
/* INCLUDE FILES */
/******************************************************************************/
#include <QGraphicsObject>
#include <QPair>
#include "CustomSubWidget.hpp"
/******************************************************************************/
/* CLASS : CustomSubWidget */
/******************************************************************************/
#include <QGraphicsObject>
class CustomSubWidget : public QGraphicsObject
{
public:
CustomSubWidget(QGraphicsItem* p_pParent = 0, int p_y_position = 0);
~CustomSubWidget();
/********************************/
/*inherited from QGraphicsObject*/
/********************************/
QRectF boundingRect() const;
void paint(QPainter *p_pPainter, const QStyleOptionGraphicsItem *p_pOption, QWidget *p_pWidget);
private:
/********************************/
/* Private methods */
/********************************/
void DrawCustomSubWidget(QPainter* p_pPainter);
/********************************/
/* Attributes */
/********************************/
QVector< QPair<int, Status> > m_Status;
int m_y_position;
bool m_hasToBeDrawn;
};
#endif // __CUSTOMWIDGET_HPP__ |
En gros pour résumer, j'ai un QGraphicsView. Je rajoute une QGraphicsScene dans ce QGraphicsView. Je set le FrameRect et le SceneRect à la taille que je veux (paramétrable à la création).
Je fais un setScene de ma QGraphicsScene dans ma QGraphicsView.
Je rajoute un QGraphicsItem Background à cette scène.
Et j'ajoute par la suite un certain nombre de sous-composant (CustomSubWidget ci-dessus) à mon Background.
Pour finir, j'ajoute le background via :
Code:
m_pScene->addItem(m_pBackground);
à ma scène.
Mon soucis est que l'ensemble de sous-composant se peignent mais se centre en plein milieu avec une grosse marge à droite et à gauche. (Plus grand que celle que j'ai rajouté de 40 à gauche et 20 à droite.)
Ce widget est affiché dans un QGridLayout.
J'ai beau tourner dans tous les sens, je ne comprend pas pourquoi mon composant persiste à ne pas vouloir prendre tout l'espace qui lui est alloué...
Quelqu'un saurait-il m'éclairer ?
Merci d'avance,
Noobboy