[Kde-java] KListViewItem problems

Maik Schulz list at maikschulz.de
Mon Jun 7 02:16:58 CEST 2004


On Sunday 06 June 2004 20:28, Richard Dale wrote:
>
> Please can you post full example code with suitable test cases so this can
> be run and tried out? The example above is much too complex to just read
> the source and 'think about it'.
>
> But I would be surprised if the java bindings managed to change the
> semantics of KListViewItem insertion into a KListView.
>
> -- Richard
> _______________________________________________
> Kde-java mailing list
> Kde-java at kde.org
> https://mail.kde.org/mailman/listinfo/kde-java

Hi Richard,

took me a while, but here goes the code. The comments in the code explain what 
works and what doesn't.

Thanks,
-Maik

-- 
Emails accepted only from list. If you need to contact me
off list please exchange list with post in my email address.

import org.kde.koala.*;
import org.kde.qt.*;

public class KListViewTest extends KMainWindow {

	KListView kListView = null;
	int itemCounter = 0;
	
	private KListViewTest() {
		super(null, "KListView Test");
		
		QVBoxLayout topLayout = new QVBoxLayout(this);
		topLayout.setAutoAdd(true);
						
		kListView = new KListView(this, "");
		kListView.addColumn("Content");
		kListView.setFullWidth(true);
		
//root element, works as expected	;-)
		KListViewItem item0 = new KListViewItem(kListView);
		item0.setText(0, "Root");
		item0.setOpen(true);
		
//third sub element. ONLY works if parent is passed as kListView.firstChild()
//DOES NOT WORK if parent is passed as item0 which is expected to be the same.
//Instead, the parent is overwritten as well as the window title. This appears
//to be a MEMORY OVERFLOW problem...
		KListViewItem item3 = new KListViewItem(kListView.firstChild());
		item3.setText(0, "Level 1, Third Item");
		
		KListViewItem item1 = new KListViewItem(kListView.firstChild());
		item1.setText(0, "Level 1, First Item");
		
//using item0 instead of kListView.firstChild() DOES WORK here. No idea
//why this should be different
		KListViewItem item2 = new KListViewItem(item0, item1);
		item2.setText(0, "Level 1, Second Item");
		
		QPushButton button = new QPushButton(this);
		button.setText(tr("add item below selected"));
		
		connect(button, SIGNAL("clicked()"), this, SLOT("buttonClicked()"));
	}
	
	public void buttonClicked() {
//I have to use QListViewItem because casting to KListViewItem throws
//a CLASSCASTEXCEPTION. Why is that? I only used KListViewItems...
		QListViewItem selectedItem = kListView.selectedItem();
		if (selectedItem != null) {
//a new item should appear below the selected one, on the same level.
//This DOES NOT WORK, it appears as the last item on the same level.
//Why is this different from adding item2 above??
			KListViewItem newItem = new KListViewItem(
				selectedItem.parent(), selectedItem
			);
			newItem.setText(
				0, "new Item #" + ++itemCounter + ", after " + selectedItem.text(0)
			);
		}
	}
	
	public static void main(String[] args) {
		KCmdLineArgs.init(
			args, new KAboutData("klistviewtest", "KListView Test", "1.0")
		);
		KApplication kApp = new KApplication();
		KListViewTest app = new KListViewTest();
		kApp.setMainWidget(app);
		app.show();
		kApp.exec();
	}

	static {
		qtjava.initialize();
		kdejava.initialize();
	}
}


More information about the Kde-java mailing list