Comportement bizarre d'une variable dynamique
salut a tous :)
j'utilise la classe "BorderLayout" du Qt dans mon projet et j'ai rencontré comportement bizarre.
Dans la classe j'utilise un variable booléenne "largeur" que j'avais besoin dans mon fenêtre principale:
borderlayout.h
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
|
#ifndef BORDERLAYOUT_H
#define BORDERLAYOUT_H
#include <QLayout>
#include <QRect>
#include <QWidgetItem>
class BorderLayout : public QLayout
{
public:
bool largeur;
enum Position { West, North, South, East, Center };
BorderLayout(QWidget *parent, int margin = 0, int spacing = -1);
BorderLayout(int spacing = -1);
~BorderLayout();
void addItem(QLayoutItem *item);
void addWidget(QWidget *widget, Position position);
Qt::Orientations expandingDirections() const;
bool hasHeightForWidth() const;
int count() const;
QLayoutItem *itemAt(int index) const;
QSize minimumSize() const;
void setGeometry(const QRect &rect);
QSize sizeHint() const;
QLayoutItem *takeAt(int index);
void add(QLayoutItem *item, Position position);
private:
struct ItemWrapper
{
ItemWrapper(QLayoutItem *i, Position p) {
item = i;
position = p;
}
QLayoutItem *item;
Position position;
};
enum SizeType { MinimumSize, SizeHint };
QSize calculateSize(SizeType sizeType) const;
QList<ItemWrapper *> list;
};
#endif |
et layoutborder.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 55 56 57 58 59 60 61
|
...
void BorderLayout::setGeometry(const QRect &rect)
{
largeur=false;
ItemWrapper *center = 0;
int westWidth = 0;
int northHeight = 0;
int centerWidth = 0;
int i;
QLayout::setGeometry(rect);
for (i = 0; i < list.size(); ++i) {
ItemWrapper *wrapper = list.at(i);
QLayoutItem *item = wrapper->item;
Position position = wrapper->position;
if (position==West){
item->setGeometry(QRect(westWidth,0,item->sizeHint().width(),rect.height()));
westWidth+=item->geometry().width()+spacing();
}else if(position == Center) {
center = wrapper;
}
}
centerWidth= rect.width() - westWidth;
for (i = 0; i < list.size(); ++i) {
ItemWrapper *wrapper = list.at(i);
QLayoutItem *item = wrapper->item;
Position position = wrapper->position;
if (position==North) {
item->setGeometry(QRect(westWidth,0,centerWidth,item->sizeHint().height()));
northHeight+=item->geometry().height()+spacing();
}
}
if (center)
center->item->setGeometry(QRect(westWidth, northHeight,
centerWidth,
rect.height()-northHeight));
for (i = 0; i < list.size(); ++i) {
ItemWrapper *wrapper = list.at(i);
QLayoutItem *item = wrapper->item;
Position position = wrapper->position;
if(position==West){
item->setGeometry(QRect(0,northHeight,item->sizeHint().width(),rect.height()-northHeight));
largeur=true;
std::cout << "Largeur: " << largeur << std::endl ;
}
}
}
... |
et dans ma fenetre.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
Window::Window()
{
QTextBrowser *centralWidget = new QTextBrowser;
centralWidget->setPlainText(tr("Central widget"));
QHBoxLayout *hbox= new QHBoxLayout;
QWidget *widget = new QWidget;
BorderLayout *layout = new BorderLayout;
layout->addWidget(createLabel("North"), BorderLayout::North);
layout->addWidget(centralWidget, BorderLayout::Center);
layout->addWidget(createLabel("West"), BorderLayout::West);
setLayout(layout);
std::cout << "Largeur:------" << layout->largeur << std::endl ; |
:
mon problème est la variable "largeur" prend la bonne valeur a l'interieur de la classe "borderlayout" et lorsque je fais un appel de cette variable dans la classe "window" elle change en une valeur bizarre:voici l'affichage test que j'ai:
Code:
1 2 3 4
|
Largeur:--------------7471206
Largeur: 1
Largeur: 1 |
j'ai besoin d'aide ici et merci d'avance :)