Hi,

On the weekend I tried to write some custom layouts to match layouts I
already know from SWT but I'm somehow failing miserably.

Problem 1: The time of setting the layout to a QWidget
======================================================

----------8<----------
QWidget center = new QWidget();
setCentralWidget(center);

QTFillLayout layout = new QTFillLayout();
center.setLayout(layout);

QWidget widget = new QWidget();
layout.addWidget(widget,"");

QTFillLayout layout2 = new QTFillLayout();
widget.setLayout(layout2);

QLabel label = new QLabel();
label.setText("BlaBla");
layout2.addWidget(label, "");

QPushButton button = new QPushButton();
button.setText("B1");
layout2.addWidget(button,"");
----------8<----------

Renderes an empty window but when I rearrange the setting of the layout
like this:

----------8<----------
QWidget center = new QWidget();
setCentralWidget(center);

QTFillLayout layout = new QTFillLayout();
                
QWidget widget = new QWidget();
layout.addWidget(widget,"");

QTFillLayout layout2 = new QTFillLayout();
widget.setLayout(layout2);

QLabel label = new QLabel();
label.setText("BlaBla");
layout2.addWidget(label, "");

QPushButton button = new QPushButton();
button.setText("B1");
layout2.addWidget(button,"");

layout.addWidget(widget,"");
center.setLayout(layout);
----------8<----------

The layout works.

Problem 2: Using layout#addWidget(QWidget)
==========================================

Fragment 1 from above gives:

"Invalid memory access of location 00000000 eip=08d15e36"

Fragment 2 from above gives:

> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.sizeHint(QLayoutItem.java:70)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.sizeHint(QTFillLayout.java:78)
>       at com.trolltech.qt.gui.QWidget.__qt_show(Native Method)
>       at com.trolltech.qt.gui.QWidget.show(QWidget.java:2134)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:39)
> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.setGeometry(QLayoutItem.java:62)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.setGeometry(QTFillLayout.java:68)
>       at com.trolltech.qt.gui.QWidget.__qt_show(Native Method)
>       at com.trolltech.qt.gui.QWidget.show(QWidget.java:2134)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:39)
> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.setGeometry(QLayoutItem.java:62)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.setGeometry(QTFillLayout.java:68)
>       at com.trolltech.qt.gui.QWidget.__qt_show(Native Method)
>       at com.trolltech.qt.gui.QWidget.show(QWidget.java:2134)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:39)
> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.setGeometry(QLayoutItem.java:62)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.setGeometry(QTFillLayout.java:68)
>       at com.trolltech.qt.gui.QWidget.__qt_show(Native Method)
>       at com.trolltech.qt.gui.QWidget.show(QWidget.java:2134)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:39)
> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.setGeometry(QLayoutItem.java:62)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.setGeometry(QTFillLayout.java:68)
>       at com.trolltech.qt.gui.QWidget.__qt_show(Native Method)
>       at com.trolltech.qt.gui.QWidget.show(QWidget.java:2134)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:39)
> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.sizeHint(QLayoutItem.java:70)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.sizeHint(QTFillLayout.java:78)
>       at com.trolltech.qt.gui.QApplication.exec(Native Method)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:40)
> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.setGeometry(QLayoutItem.java:62)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.setGeometry(QTFillLayout.java:68)
>       at com.trolltech.qt.gui.QApplication.exec(Native Method)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:40)
> QtJambi: Exception pending in native code
> Exception in thread "main" com.trolltech.qt.QNoNativeResourcesException: 
> Function call on incomplete object of type: 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper
>       at 
> com.trolltech.qt.gui.QLayoutItem$ConcreteWrapper.sizeHint(QLayoutItem.java:70)
>       at 
> org.ufacekit.ui.qt.core.layouts.QTFillLayout.sizeHint(QTFillLayout.java:78)
>       at com.trolltech.qt.gui.QApplication.exec(Native Method)
>       at 
> org.ufacekit.ui.qt.example.TestFillLayout.main(TestFillLayout.java:40)

The example i used is
http://doc.trolltech.com/4.4/layouts-borderlayout.html, I don't know C++
but I think I translated this appropriately into Java.

Tom

/*******************************************************************************
 * Copyright (c) 2008, Original authors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl <[EMAIL PROTECTED]>
 
*******************************************************************************/
package org.ufacekit.ui.qt.core.layouts;

import java.util.ArrayList;
import java.util.List;

import com.trolltech.qt.core.QRect;
import com.trolltech.qt.core.QSize;
import com.trolltech.qt.gui.QLayout;
import com.trolltech.qt.gui.QLayoutItemInterface;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QWidgetItem;

public class QTFillLayout extends QLayout {
        private List<ItemWrapper> items = new ArrayList<ItemWrapper>();

        private class ItemWrapper {
                private QLayoutItemInterface item;
                public ItemWrapper(QLayoutItemInterface item) {
                        this.item = item;
                }
        }

        @Override
        public void addItem(QLayoutItemInterface arg0) {
                items.add(new ItemWrapper(arg0));
        }

        public void addWidget(QWidget widget, String a) {
                items.add(new ItemWrapper(new QWidgetItem(widget)));
        }

        @Override
        public int count() {
                return items.size();
        }

        @Override
        public QLayoutItemInterface itemAt(int arg0) {
                if( arg0 < items.size() ) {
                        return items.get(arg0).item;
                }
                return null;
        }

        @Override
        public void setGeometry(QRect geometry) {
                super.setGeometry(geometry);

                if( items.size() == 0 ) {
                        return;
                }

                int width = geometry.width() / items.size();
                int height = geometry.height();

                int x = 0;
                for (ItemWrapper i: items) {
                        i.item.setGeometry(new QRect(x, 0, width, height));
                        x += width;
                }
        }

        @Override
        public QSize sizeHint() {
                QSize size = new QSize();

                for( ItemWrapper i: items ) {
                        size.setWidth(size.width()+ i.item.sizeHint().width());
                        int height = i.item.sizeHint().height();
                        if (size.height() < height) {
                                size.setHeight(height);
                        }
                }

                return size;
        }

        @Override
        public QLayoutItemInterface takeAt(int arg0) {
                return items.remove(arg0).item;
        }
}
package org.ufacekit.ui.qt.example;

import org.ufacekit.ui.qt.core.layouts.QTFillLayout;
import org.ufacekit.ui.qt.core.layouts.QTGridData;
import org.ufacekit.ui.qt.core.layouts.QTGridLayout;

import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QWidget;

public class TestFillLayout extends QMainWindow {
        public TestFillLayout() {
                QWidget center = new QWidget();
                setCentralWidget(center);

                QTFillLayout layout = new QTFillLayout();
                center.setLayout(layout);

                QWidget widget = new QWidget();
                layout.addWidget(widget);

                QTFillLayout layout2 = new QTFillLayout();
                widget.setLayout(layout2);

                QLabel label = new QLabel();
                label.setText("BlaBla");
                layout2.addWidget(label);

                QPushButton button = new QPushButton();
                button.setText("B1");
                layout2.addWidget(button);
        }

        public static void main(final String[] args) {
                try {
                        QApplication.initialize(args);
                        new TestFillLayout().show();
                        QApplication.exec();
                } catch(Exception e) {
                        e.printStackTrace();
                }

        }
}
_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to