PDA

View Full Version : odd double widgets



jhearon
22nd February 2008, 18:42
Hi,
Sorry for long code post below. I'm struggling with a drop dead simple example of placing a soqtexaminerviewer in a qt window. This code is streamlined from exs. by Jon Andrews and others. There's an odd issue of the viewer's widgets doubling.

I can defeat some of that behavior by turning off decorations, or getting the buttons then reducing the number etc. but that doesn't solve the basic problem of why the widgets are doubling up in the first place.

If you comment out "this->setBaseWidget(widget);" you can clearly see a small version of the soqtexaminer viewer in the upper left hand corner ot the qt window. Strange?

I've tweaked with class signatures, layout manager, the show method etc. I can't seem to figure out what's going wrong.

Wondering if anyone else is working along same lines, and might have an idea.

thanks,




//main.cpp

# define COIN_DLL
# define SOQT_NOT_DLL
# define SIMAGE_DLL

#include <QApplication>
#include "window.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;

Window window;
window.show();
return app.exec();
}

//==============
//window.h

# define COIN_DLL
# define SOQT_NOT_DLL
# define SIMAGE_DLL

#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>
class CoinWidget;
class QHBoxLayout;


class Window : public QWidget
{
Q_OBJECT

public:
Window();

private:

QHBoxLayout *rootLayout;
CoinWidget *coinWidget;
};
#endif

//==============
//window.cpp

# define COIN_DLL
# define SOQT_NOT_DLL
# define SIMAGE_DLL

#include <QtGui>
#include "coinwidget.h"
#include "window.h"

Window::Window()
{
coinWidget = new CoinWidget;
rootLayout = new QHBoxLayout;
rootLayout->addWidget(coinWidget);
setLayout(rootLayout);
}
//==============
//coinwidget.h

# define COIN_DLL
# define SOQT_NOT_DLL
# define SIMAGE_DLL

#ifndef COINWIDGET_H
#define COINWIDGET_H

#include <QMainWindow>
#include <Inventor/Qt/SoQt.h>

//Forward declarations
class Model;

class CoinWidget : public QMainWindow
{
Q_OBJECT

public:
CoinWidget(QWidget *parent = 0);
~CoinWidget();

QSize minimumSizeHint() const;
QSize sizeHint() const;

protected:
Model *model;

};
#endif

//==============
//coinwidget.cpp

# define COIN_DLL
# define SOQT_NOT_DLL
# define SIMAGE_DLL

#include <QtGui>
#include <QtCore>
#include <Inventor/Qt/SoQt.h>
#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
#include "coinwidget.h"
#include "model.h"

CoinWidget::CoinWidget(QWidget *parent) : QMainWindow(parent)
{
SoQt::init(this);
model = new Model(this);
setCentralWidget(model->getWidget());
model->setDefaultScene();

}

CoinWidget::~CoinWidget() {
}

QSize CoinWidget::minimumSizeHint() const {
return QSize(200, 200);
}

QSize CoinWidget::sizeHint() const {
return QSize(400, 400);

}

//==============
//model.h

# define COIN_DLL
# define SOQT_NOT_DLL
# define SIMAGE_DLL

#ifndef MODEL_H
#define MODEL_H

#include <Inventor/Qt/SoQt.h>
#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoCone.h>
#include <Inventor/nodes/SoSeparator.h>

class Model : public SoQtExaminerViewer
{
public:
Model( QWidget *parent = NULL,
const char *name=NULL,
const SbBool embed=TRUE);
~Model(void);
void setDefaultScene();

private:
SoSeparator *root;
SoQtExaminerViewer * eviewer;
SoCone *cone;
SoBaseColor * col;
};
#endif

//==============
//model.cpp

# define COIN_DLL
# define SOQT_NOT_DLL
# define SIMAGE_DLL

#include <model.h>

Model::Model( QWidget *parent,
const char * name,
const SbBool embed)
: SoQtExaminerViewer( parent,
name,
embed,
SoQtFullViewer::BUILD_ALL,
SoQtViewer::BROWSER, TRUE)
{

QWidget *widget = this->buildWidget(this->getParentWidget());
this->setBaseWidget(widget); //comment this to see small 2nd viewer???
this->show();

}
// **************************
Model::~Model() {
delete this;
root->unref();
}

// **************************
void Model::setDefaultScene(void) {

root = new SoSeparator;
root->ref();
cone = new SoCone;
root->addChild(cone);
setSceneGraph(root);

}

jhearon
23rd February 2008, 18:18
I'm answering my own post.
I finally figured out the answer to this problem which is in the constructor for the model.cpp class. The SoQtExaminerViewer portion below uses a protected constructor, and delaying a superclass viewer from building it's decorations is done by passing build==FALSE as the last argument of the protected constructor. You then have to explicitly trigger the building in your own constructor.

model.cpp

Model::Model( QWidget *parent,
const char * name,
const SbBool embed)
: SoQtExaminerViewer( parent,
name,
embed,
SoQtFullViewer::BUILD_ALL,
SoQtViewer::BROWSER, FALSE)

// Explicitly triggers the construction of viewer decorations.
QWidget * widget = this->buildWidget(this->getParentWidget());
this->setBaseWidget(widget);

...therefore with "...SoQtViewer::BROWSER, TRUE)", above, the viewer decorations were getting built twice. Yikes.