Passing 2-dimensional QVariantList from C++ to QML - c++

I am really confused on how to pass a 2-dimensional QVariantList from C++ to QML, I basically want to pass a value from C++ which will do the same as assigning it in QML like this:
property var twoDim: [["1-1", "1-2"],["2-1", "2-2"]]
So that I can use the array as a model in a Repeater element by doing: modelData[0] which will return 1st array of values, and modelData[1] which will return 2nd array of values. Names and surnames for example...
Please help

Firstly you can have a QVariantList of QVariantLists:
// main.cpp
int main( int argc, char* argv[] )
{
QGuiApplication app( argc, argv );
auto myList = QVariantList{};
for ( auto i = 0; i < 2; ++i ) {
myList << QVariant::fromValue(
QVariantList{ QString::number( i + 1 ) + "-1",
QString::number( i + 1 ) + "-2" } );
}
auto view = QQuickView{};
view.rootContext()->setContextProperty( "myList", myList );
view.setSource( QUrl{ QStringLiteral{ "qrc:/QmlCppTest.qml" } } );
view.show();
return app.exec();
}
// QmlCppTest.qml
import QtQuick 2.3
Item {
property var listOfLists: myList
Component.onCompleted: {
for ( var i = 0; i < listOfLists.length; ++i ) {
for ( var j = 0; j < listOfLists[i].length; ++j ) {
print( i, j, listOfLists[i][j] );
}
}
}
}
Results in:
qml: 0 0 1-1
qml: 0 1 1-2
qml: 1 0 2-1
qml: 1 1 2-2
But like I said in my comment, if your first dimension represents an entity, and the second dimension represents properties of that entity, the superior approach for performance and maintenance reasons is to use QAbstractItemModel (or one of it's more specific derived classes).
Qt's documentation has lots of stuff on MVC programming, you should take some time to learn the subject as it underpins much of Qt.

Related

A function to display contents of 1 or 2 dimensional array of any type

I needed to be able to display the contents of my various arrays (for debugging purposes at this point), and decided to write a function to help me with that. This is what I came up with.
The goal is to be able to display any type of incoming array (int, double, etc).
Because I never had any official programming training, I am wondering if what I have is too "inelegant" and could be improved by doing something obvious to a good computer science person, but not so to a layperson.
int
DisplayArrayInDebugWindow(
void** incoming_array,
char* array_type_str,
int array_last_index_dim_size,
int array_terminator,
HWND handle_to_display_window,
wchar_t* optional_array_name )
{
wchar_t message_bufferw[1000];
message_bufferw[0] = L'\0';
wchar_t temp_buffer[400];
if ( array_last_index_dim_size == 0 ) { array_last_index_dim_size = 1; }
// ----------------------------------------------------------------------------
// Processing for "int" type array
// ----------------------------------------------------------------------------
if ( 0 == (strcmp( array_type_str, "int" )) )
{
int j = 0;
swprintf( temp_buffer, L"%s\r\n", optional_array_name );
wcscat( message_bufferw, temp_buffer );
for ( int i = 0; ((int)(*((int*)( (int)incoming_array + i * (int)sizeof(int) * array_last_index_dim_size + j * (int)sizeof(int))))) != array_terminator; i++ )
{
swprintf( temp_buffer, L"%02i:\t", i );
wcscat( message_bufferw, temp_buffer );
for ( j; j < last_array_dim_size; j++ )
{
swprintf( temp_buffer, L"%i\t", ((int)(*((int*)( (int)incoming_array + i * (int)sizeof(int) * array_last_index_dim_size + j * (int)sizeof(int) )))) ); //
wcscat( message_bufferw, temp_buffer );
}
wcscat( message_bufferw, L"\r\n" );
// --------------------------------------------------------------------
// reset j to 0 each time
// --------------------------------------------------------------------
j = 0;
}
swprintf( temp_buffer, L"\nEnd of Array\n" );
wcscat( message_bufferw, temp_buffer );
SetWindowText( handle_to_display_window, message_bufferw );
}
return 0;
}
NB: When I pass in "incoming array", I type cast it as (void**) obviously.
When the data type changes but the algorithm doesn't, it's time to consider using templates.
template<class Element_Type>
print_array(Element_Type const * p_begin,
Element_Type const * p_end)
{
while (p_begin != p_end)
{
cout << *p_begin;
++p_begin;
}
}
The conversion from single dimension to multiple dimension is left as an exercise to the OP and readers.
Edit 1: Another alternative
At some point, the output function will need information about how to print the information you gave it.
One option is for you to write your own printf function that has format specifiers for the data you send it.
While another option is to pass a pointer to a function that prints the data.
The fundamental issue is that the output function needs to know how to print the data.
For C++, I suggest overriding operator<< in the class / structure. Since the class/structure knows the data, it can easily know how to print the data.

How do I cast a void pointer to a int[3]?

I need to call a 3rd party library and pass in an int[3] as a void * like this [works]:
int pattern[3] = {2,4,10};
if ( OSTaskCreate( BlinkLED,
( void * ) pattern,
( void * ) &BlinkTaskStack[USER_TASK_STK_SIZE],
( void * ) BlinkTaskStack,
MAIN_PRIO - 1 ) != OS_NO_ERR )
{
iprintf( "*** Error creating blink task\r\n" );
}
But now I need to parse a string to get the pattern array and I can't seem to get it right.
First I pass the string into the parser and get back the array:
int (&ParseBlinkOnCommand(char rxbuffer[3]))[3]
{
// Code parses rxbuffer and creates the 3 ints needed
int pattern[3] = {repeats, onTicks, offTicks};
return pattern;
}
Then I try to pass it to the OSTaskCreate just like I did before:
int pattern2[3] = ParseBlinkOnCommand(rxbuffer);
if ( OSTaskCreate( BlinkLED,
( void * ) pattern2,
( void * ) &BlinkTaskStack[USER_TASK_STK_SIZE],
( void * ) BlinkTaskStack,
MAIN_PRIO - 1 ) != OS_NO_ERR )
{
iprintf( "*** Error creating remote blink task\r\n" );
}
but I get the error 'array must be initialized with a brace-enclosed initializer'.
What is the right way to do this?
First, ParseBlinkOnCommand returns reference to local object and so return dangling reference.
Second C-array are not copyable, so int pattern2[3] = ParseBlinkOnCommand(rxbuffer); should be int (&pattern2)[3] = ParseBlinkOnCommand(rxbuffer);.
but why not using std::vector or std::array (or custom structure) ?
std::vector<int> ParseBlinkOnCommand(const char (&rxbuffer)[3])
{
// Code parses rxbuffer and creates the 3 ints needed
return {repeats, onTicks, offTicks};
}
And then
auto pattern2 = ParseBlinkOnCommand(rxbuffer);
if ( OSTaskCreate( BlinkLED,
pattern2.data(),
&BlinkTaskStack[USER_TASK_STK_SIZE],
BlinkTaskStack,
MAIN_PRIO - 1 ) != OS_NO_ERR )
{
iprintf( "*** Error creating remote blink task\r\n" );
}

How to pass QString or QStringList to QTableWidgetItem

I am trying to create a table using QTableView and QTableWidgetItem.
Basically I am trying to create a few rows with some name under first Header.
But the problem is I am passing each row with some name.
Same thing I want to achieve using QString, QStringList array.
Currently I am not passing any value under second header.
Please suggest how to do that. Please find the output window.
Below is my code snippet:
#include <QApplication>
#include <QtGui>
#include <QMainWindow>
#include <QTableWidget>
#include <QString>
#define ROW_NUMBER 20
#define COL_NUMBER 2
int main(int argc , char **argv)
{
QApplication app(argc,argv);
QMainWindow *window = new QMainWindow();
window->setWindowTitle(QString::fromUtf8("TableWidget Resize column width"));
window->resize(200,250);
QTableWidget *table = new QTableWidget();
table->setRowCount(ROW_NUMBER);
table->setColumnCount(COL_NUMBER);
table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
table->setHorizontalHeaderLabels(QString("Field;Value").split(";"));
table->setItem(0,0,new QTableWidgetItem("Sequence Number"));
table->setItem(1,0,new QTableWidgetItem("Date"));
table->setItem(2,0,new QTableWidgetItem("Seconds"));
table->setItem(3,0,new QTableWidgetItem("Source Date"));
table->setItem(4,0,new QTableWidgetItem("Source Time"));
table->setItem(5,0,new QTableWidgetItem("ServiceType"));
table->setItem(6,0,new QTableWidgetItem("Transtype"));
table->setItem(7,0,new QTableWidgetItem("UserId"));
table->setItem(8,0,new QTableWidgetItem("UserIMSI"));
table->setItem(9,0,new QTableWidgetItem("CorrelationId"));
table->setItem(10,0,new QTableWidgetItem("MajorNumber"));
table->setItem(11,0,new QTableWidgetItem("MinorNumber"));
table->setItem(12,0,new QTableWidgetItem("Source Type"));
table->setItem(13,0,new QTableWidgetItem("Total Consume"));
table->setItem(14,0,new QTableWidgetItem("Source Info"));
table->setItem(15,0,new QTableWidgetItem("Event"));
table->setItem(16,0,new QTableWidgetItem("GroupID"));
table->setItem(17,0,new QTableWidgetItem("ServiceID"));
table->setItem(18,0,new QTableWidgetItem("OperatorId"));
table->setItem(19,0,new QTableWidgetItem("Options Array"));
table->resizeColumnsToContents();
window->setCentralWidget(table);
window->show();
return app.exec();
}
QString test;
test = "a";
// First Column
table->setItem(0,0,new QTableWidgetItem(a));
// Second Column
table->setItem(0,1,new QTableWidgetItem(a));
just like that or:
// 100% working example
QVector< QString > string;
QVector< QString > value;
string.append( "Nr1" );
string.append( "Nr2" );
string.append( "Nr3" );
string.append( "Nr4" );
value.append( "1" );
value.append( "2" );
value.append( "3" );
value.append( "4" );
ui->twTable->setSortingEnabled( false );
for( int i = 0 ; i < string.size( ) && i < value.size( ) ; ++i )
{
ui->twTable->insertRow( 0 );
// First Column
ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( string[i] ) );
// Second Column
ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( value[i] ) );
}
ui->twTable->setSortingEnabled( true );
array , QStringList work the same way just without the append.
array:
QString str[5] = { "a" , "b" , "c" , "d" , "e" };
for( int i = 0 ; i < 5 ; ++i )
{
ui->twTable->insertRow( 0 );
// First Column
ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( str[i] ) );
// Second Column
ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( str[i] + QString::number( i ) ) );
}
QStringList:
QStringList str = { "a" , "b" , "c" , "d" , "e" };
for( int i = 0 ; i < 5 ; ++i )
{
ui->twTable->insertRow( 0 );
// First Column
ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( str[i] ) );
// Second Column
ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( str[i] + QString::number( i ) ) );
}
And in reverse order:
// Probably the output you want:
QString str[5] = { "a" , "b" , "c" , "d" , "e" };
for( int i = 4 ; i >= 0 ; --i )
{
ui->twTable->insertRow( 0 );
// First Column
ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( str[i] ) );
// Second Column
ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( str[i] + QString::number( i ) ) );
}
Remove the ui->twTable->insertRow( 0 ) when you're using a fixed amount of rows like you do in your code.
Side note: I tend to disable sorting while inserting items and enabling it after so the rows don't get auto sorted to a different position when you input them.

Maya API - Move CV with Kinect

I’m trying to create a 64-bit plug-in for Autodesk Maya 2013. The aim of this plug-in is to be able to model objects through the Kinect.
In context, with the plug-in you can select the CV of a created object. To specify you want to move the selected points, the position of selected CV is updated constantly.
Here is my problem, when I try to update the position of a selected CV of an object, the result is not displayed on the screen.
I have tried many ways without getting any result, as an example:
1) when I do the setPosition the point doesn’t update.
for ( ; !iter.isDone(); iter.next() ) {
iter.getDagPath( mdagPath, mComponent );
MItSurfaceCV sCvFn( mdagPath, mComponent, true, &stat );
if ( MS::kSuccess == stat ) {
for ( ; !sCvFn.isDone(); sCvFn.nextRow() ) {
for ( ; !sCvFn.isRowDone(); sCvFn.next() ) {
MPoint pto(pHand.posX, pHand.posY, pHand.posZ);
sCvFn.setPosition(pto, MSpace::kWorld);
}
}
}
sCvFn.updateSurface();
}
2) in this case, if you iterate over the array newCVS before doing the setCVs you can see the updated points.
But then, when doing setCVs and looking for the position in the CV array of the object (doing a getCVs) the position of the modified CV doesn’t change.
for ( ; !iter.isDone(); iter.next() ) {
stat = iter.getDagPath( mdagPath, mComponent );
if(stat == MS::kSuccess) {
MFnNurbsSurface surf(mdagPath);
MItSurfaceCV sCvFn( mdagPath, mComponent, true, &stat );
MPointArray currentCV;
surf.getCVs(currentCV, MSpace::kWorld);
MPointArray newCVS(currentCV.length(), MPoint::origin);
newCVS.setLength(currentCV.length());
for(unsigned i = 0; i < currentCV.length(); i++){
newCVS[i] = newCVS[i] + currentCV[i];
}
int index;
if ( MS::kSuccess == stat ) {
for ( ; !sCvFn.isDone(); sCvFn.nextRow() ) {
for ( ; !sCvFn.isRowDone(); sCvFn.next() ) {
sCvFn.getIndex(indexU, indexV);
int posArray = indexU * surf.numCVsInV() + indexV;
index = posArray;
MVector diff = MPoint(pHand.posX, pHand.posY, pHand.posZ) - currentCV[posArray];
newCVS[posArray] = newCVS[posArray] + diff;
}
}
surf.setCVs(newCVS, MSpace::kWorld);
surf.updateSurface();
}
}
3) When having CV selected in Maya, I tried to move this points with MEL commands. For this, I use the “MGlobal::executeCommand” function, but still nothing happens.
Finally, I changed executeCommand to executeCommandOnIdle. In this case, the object change correctly but the procedure crashes, perhaps, because this procedure is executed many times per second.
I would appreciate if anyone can help me with this problem or knows something about this.

Flex 3 - how to select an item in the list as default?

how to select an item in the list as default
default selected item say of index 0
I tried stuff like this --
listid.selectedIndex = somevalueinmyprogram - 1; // 0
but when i debug this i get
_selectedIndex = 0
selectedIndex = -1
and default value is not selected why so?
[i have already checked for the obvious that somevaluefrommyprogram is not equal to 0]
Help!
I have found that if you set the selectedItems by defining an array of selected items it works better than the selectedIndex.
function setSelectedCategories():void{
var selectedItems :Array = new Array();
for each (var selectedCategory:Category in entry.categories) {
for each (var category:Category in categories) {
if (selectedCategory.categoryID == category.categoryID){
selectedItems .push(category);
break;
}
}
}
categoriesList.selectedItems = selectedItems ;
}
OR using the selectedIndices works if you want to use an array that contains the indexes that you want to be selected.
for ( var i:int=0; i < userIpods.length; i++ ) {
//j will represent the list item's index value
for ( var j:int = 0; j < iPodAry.length; j++) {
if ( userIpods[i] == iPodAry[j].id ) {
selectedIpodIndices.push( j );
break;
} //end if
} //end for ( var iPodObj:Object in iPodAry) {
} //end for ( var i:int in userIpods )
/*mark as selected those index values in the
selectedIpodIndices array*/
iPodList.selectedIndices = selectedIpodIndices ;