DzListBox

Victor_BVictor_B Posts: 391

Hello.

I need widget like DzListBox, but with multiselection (in DzListBox I can select only 1 item).

With peace of code how to create and add items to it, please.

Or

I can use bunch of checkboxes instead, but I don't know how to wrap them into area with scrollbar (I'm limited with space). With code example, please.

Thanks.

p.s.: Daz widgets are killing me... I'm almoust dead... :)

Post edited by Victor_B on

Comments

  • Victor_BVictor_B Posts: 391
    edited December 2020

    Restart docs.daz3d.com please. It's not available (Gateway Timeout) for several hours yet. Thank you.

    Post edited by Victor_B on
  • ChoholeChohole Posts: 33,604

    Victor_B said:

    Restart docs.daz3d.com please. It's not available (Gateway Timeout) for several hours yet. Thank you.

    Do you mean   http://docs.daz3d.com/doku.php     

  • Victor_BVictor_B Posts: 391
    edited December 2020

    Yes. It doesn't works again, btw

    But I made DzListBox multi selectable.

    And died... :))

    And rose again to add a code to this post (maybe somebody need it)

    listWidget.selectionMode = 1; // 1 - Multi, 2 - Extended
    Post edited by Victor_B on
  • Using the name would be more robust:

    listWidget.selectionMode = DzListBox.Multi;

  • Victor_BVictor_B Posts: 391
    edited December 2020

    Richard Haseltine said:

    Using the name would be more robust:

    listWidget.selectionMode = DzListBox.Multi;

    Were you was yesterday with this code?.... Thanks anyway. If it robust, I will use it ;)

    Post edited by Victor_B on
  • Victor_BVictor_B Posts: 391
    edited December 2020

    Today I back to my dzListBox to work a little with selected Items. So, I printed out all properties and triyed to find among them something like "getItems" or "getSelectedItems" or maybe "Items" or "Selected" or...noup, nothing. There is a "selected", but it always "-1" with multiselection.

    So, the question is: how to work with DzListBox? I need a loop with all items, selected items and I need to set up index to list item (idetificator), not just text label. Why, oh Heavens, insertItem(QString) has only one parameter for text label?

    Ok, here is a dialog box with dzListBox. Please, add to it some code I need.

    Thanks.

    // DAZ Studio version 4.12.0.86 filetype DAZ Scriptvar Dialog = new DzDialog();Dialog.width = 250;Dialog.height = 250;Dialog.caption = "";var listBox = new DzListBox( Dialog );listBox.setGeometry( 0, 0, 250, 200 );listBox.selectionMode = DzListBox.Multi;var aItems = [['Madagascar', 2010],['Malawi', 2472],['Malaysia', 2709],['Maldives', 2464],['Madagascar', 2410],['Mali', 2482],['Malta', 2750],['Marshall Islands', 8404],['Mauritius', 2700],];for (var i = 0; i < aItems.length; i++) {	listBox.insertItem(aItems[i][0]);}var btnClose = new DzPushButton( Dialog );btnClose.text = 'Close';btnClose.objectName = 'btnClose';btnClose.y = 200;btnClose.minWidth = 75;btnClose.minHeight = 23;Dialog.setRejectButton(btnClose);var btnOK = new DzPushButton( Dialog );btnOK.text = 'OK';btnOK.objectName = 'btnOK';btnOK.y = 200;btnOK.x = 80;btnOK.minWidth = 75;btnOK.minHeight = 23;Dialog.setAcceptButton(btnOK);if( Dialog.exec() ) {	// loop with List items		// selected items		} else {	// dialog box was closed}
    Post edited by Victor_B on
  • Victor_BVictor_B Posts: 391
    edited December 2020

    Ok, looks like I found a quick solution by adding

    connect(listBox, "clicked(int)", getSelected);

    and creating array with selected items with every click, but I don't like it. So, if you know an elegant soution - you're welcome.

    Post edited by Victor_B on
  • Victor_BVictor_B Posts: 391
    edited December 2020

    I know about .isSelected()

    I don't know how to get members of the list box. Selected or all. That was the main question.

    Post edited by Victor_B on
  • oMyListBox.count gives the number of items, so you just need to loop from 0 to count - 1 checking each for its selection state.

  • Rob offers a little example, including a neat way to cull the empty entries from an array in the last line:

    var nItems = listBox.count;var aSelected = new Array( nItems );for( var i = 0; i < nItems; i += 1 ){	if( listBox.isSelected( i ) ){		aSelected[i] = listBox.text( i );	}}aSelected = aSelected.filter( Boolean );

     

  • TheMysteryIsThePointTheMysteryIsThePoint Posts: 2,923
    edited December 2020

    TheMysteryIsThePoint said:

    Richard Haseltine said:

    Rob offers a little example, including a neat way to cull the empty entries from an array in the last line:

    var nItems = listBox.count;var aSelected = new Array( nItems );for( var i = 0; i < nItems; i += 1 ){	if( listBox.isSelected( i ) ){		aSelected[i] = listBox.text( i );	}}aSelected = aSelected.filter( Boolean );

    Shouldn't it be:

    aSelected.push( listBox.text(i) );

    ? Why insert into a sparse array, just to filter it out later?

    Edited to fix quote

    Post edited by Richard Haseltine on
  • Victor_BVictor_B Posts: 391
    edited December 2020

    Wow, discussion... Yep, I used .count in the last version and now I see that it is the one legal method. Thanks to all. :)

    About filtering... yes,

    aSelected.push( listBox.text(i) );

    or

    aSelected[aSelected.length] = listBox.text(i);

    are better imho :)

     

    Post edited by Victor_B on
  • I'm just going to post rob's explanantion verbatim:

    The example was initially written to hold the selected state of each item (as opposed to the text of selected items), which is faster (in the Qt4 engine) if the array is pre-sized and values are assigned by index, instead of dynamically resizing the array with push. The example was then modified (from its initial form) to use the selected state to conditionally populate the array with the text of the item. The filtering statement was just a quick one-liner to purge falsy values. Performance tests do indicate that initializing an empty array and dynamically resizing by pushing the text of selected items is (usually) msecs faster (varies per execution) due to the time associated with the filter operation, so the next optimization would be...

    var aSelected = [];for( var i = 0, n = listBox.count; i &lt; n; i += 1 ){	if( listBox.isSelected( i ) ){		aSelected.push( listBox.text( i ) );	}}

     

    He also offers this to show the difference in timing:

    (function(){		var nItems = 1000000;		var nStart = Date.now();	var a2 = [];	for( var i = 0; i &lt; nItems; i += 1 ){		a2.push( true );	}	var nEnd = Date.now();	print( "resize:", nEnd - nStart );		nStart = Date.now();	a2 = new Array( nItems );	for( var i = 0; i &lt; nItems; i += 1 ){		a2[i] = true;	}	nEnd = Date.now();	print( "pre-size:", nEnd - nStart );	})();

     

  • A more robust performance test:

    (function(){		var nItems = 1000000;		var nStart;	var aArray;	var nInit;	var nFilter;	var nEnd;	var i;		nStart = Date.now();	aArray = [];	nInit = Date.now();	for( i = 0; i &lt; nItems; i += 1 ){		if( i &gt; 0 ){			aArray.push( true );		} else {			aArray.push( false );		}	}	nEnd = Date.now();		print( "resize:",		"\n\tinit:", nInit - nStart,		"\n\tfill:", nEnd - nStart );		nStart = Date.now();	aArray = new Array( nItems );	nInit = Date.now();	for( i = 0; i &lt; nItems; i += 1 ){		if( i &gt; 0 ){			aArray[i] = true;		} else {			aArray[i] = false;		}	}	nFilter = Date.now();	aArray = aArray.filter( Boolean );	nEnd = Date.now();		print( "pre-size:",		"\n\tinit:", nInit - nStart,		"\n\tfill:", nFilter - nInit,		"\n\tfltr:", nEnd - nFilter,		"\n\ttotl:", nEnd - nStart );	})();

     

Sign In or Register to comment.