momesana
4th January 2008, 14:07
I went through the ordeal of downloading a recent Qt-4.4.0 snapshot (it's about two days old) over a 56kbit modem line in order to test the new QGraphicsView capabilities. I was most interested in using the Widget on canvas feature and the new QGraphics-layout-managers. None of them work for me. I will focus on the issue with the linearLayout in this post.
I have pasted a little application below that highlights my problems. The layout works in so far that the QGraphicsWidgets are placed into the layout and handled properly. But the setSpacing member doesn't work as expected. If I set a spacing of 0, then all the items are painted on top of each other so that the result looks like a single item while the default spacing of 4 pixels creates a condensed heap of items. I have to resort to a spacing greater then the size of the items themselves in order to make them appear side by side. What am I doing wrong?
Thanx in advance
#include <QtGui>
class Panel : public QGraphicsWidget
{
Q_OBJECT
public:
Panel(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0) :
QGraphicsWidget(parent, wFlags) {
rect = QRectF(0, 0, 40, 40);
setGeometry(rect);
}
void paint(QPainter* painter,
const QStyleOptionGraphicsItem*, QWidget* widget = 0) {
Q_UNUSED(widget);
painter->setRenderHints(QPainter::Antialiasing);
QPen pen(Qt::black);
pen.setWidth(1);
painter->setPen(pen);
painter->setBrush(Qt::BDiagPattern);
painter->drawEllipse(rect);
}
private:
QRectF rect;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent=0) : QMainWindow(parent) {
resize(800, 600);
QGraphicsScene* scene = new QGraphicsScene;
QGraphicsWidget* container = new QGraphicsWidget;
m_lo = new QGraphicsLinearLayout;
m_lo->setOrientation(Qt::Horizontal);
m_lo->setSpacing(30);
container->setLayout(m_lo);
scene->addItem(container);
for (int i = 0; i < 10; i++) {
QGraphicsLinearLayout* row = new QGraphicsLinearLayout;
row->setOrientation(Qt::Vertical);
row->setSpacing(30);
m_lo->addItem(row);
for (int j = 0; j < 10; j++) {
Panel* p = new Panel;
row->addItem(p);
}
}
QGraphicsView* view = new QGraphicsView(scene);
setCentralWidget(view);
}
private:
QGraphicsLinearLayout* m_lo;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}
#include "main.moc"
I have pasted a little application below that highlights my problems. The layout works in so far that the QGraphicsWidgets are placed into the layout and handled properly. But the setSpacing member doesn't work as expected. If I set a spacing of 0, then all the items are painted on top of each other so that the result looks like a single item while the default spacing of 4 pixels creates a condensed heap of items. I have to resort to a spacing greater then the size of the items themselves in order to make them appear side by side. What am I doing wrong?
Thanx in advance
#include <QtGui>
class Panel : public QGraphicsWidget
{
Q_OBJECT
public:
Panel(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0) :
QGraphicsWidget(parent, wFlags) {
rect = QRectF(0, 0, 40, 40);
setGeometry(rect);
}
void paint(QPainter* painter,
const QStyleOptionGraphicsItem*, QWidget* widget = 0) {
Q_UNUSED(widget);
painter->setRenderHints(QPainter::Antialiasing);
QPen pen(Qt::black);
pen.setWidth(1);
painter->setPen(pen);
painter->setBrush(Qt::BDiagPattern);
painter->drawEllipse(rect);
}
private:
QRectF rect;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent=0) : QMainWindow(parent) {
resize(800, 600);
QGraphicsScene* scene = new QGraphicsScene;
QGraphicsWidget* container = new QGraphicsWidget;
m_lo = new QGraphicsLinearLayout;
m_lo->setOrientation(Qt::Horizontal);
m_lo->setSpacing(30);
container->setLayout(m_lo);
scene->addItem(container);
for (int i = 0; i < 10; i++) {
QGraphicsLinearLayout* row = new QGraphicsLinearLayout;
row->setOrientation(Qt::Vertical);
row->setSpacing(30);
m_lo->addItem(row);
for (int j = 0; j < 10; j++) {
Panel* p = new Panel;
row->addItem(p);
}
}
QGraphicsView* view = new QGraphicsView(scene);
setCentralWidget(view);
}
private:
QGraphicsLinearLayout* m_lo;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}
#include "main.moc"