Hi everybody and thanks for your time,
I read filenames in a QList. This is what I've done:
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFiles);
dialog.setViewMode(QFileDialog::Detail);
if(dialog.exec()) {
files.append(dialog.selectedFiles());
}
foreach(QString file, files) {
// add files to a table
}
I have all files in the QList files. Now I want the user to add information to this list. In PHP (or javascript) I would do something like this:
$fileinformation = array();
foreach($files AS $file) {
array_push($fileinformation, array (
'filename' => $file,
'doAction1' => false,
'doAction2' => true,
'parameter2' => array (
'value1' => $val1,
'value2' => $val2
)
)
)
}
And in the next step, I would make it possible for the user to edit the default options by editing $fileinformation['parameter2']['value']. But how can I do this in QT? I know, there are certain functions in qt like QList and QMap but both are not multidimensional and too unflexible for this case.
What did I miss?
You haven't missed anything. In a statically-typed language, you generally do not have objects of a completely different type all in the same container.
Assuming all of the parameters are defined at compile time, you can define a class or struct that contains those parameters and store it in a list. For brevity, I'm using a raw struct in this example. You may wish to explicitly initialize its members with a constructor and possibly wrap its data into a meaningful class.
struct FileInformation
{
QString filename;
bool doAction1;
bool doAction2;
struct ParameterStruct
{
QString value1; // I'm guessing it's QString, since you don't show what type it was.
QString value2;
} parameter2;
};
int main()
{
QList<FileInformation> myList;
FileInformation info;
info.fileName = "filename.txt";
info.doAction1 = false;
info.doAction2 = true;
info.parameter2.value1 = "someValue";
info.parameter2.value2 = "someOtherValue";
myList.append(info);
//now to change stuff!
myList[0].parameter2.value1 = "a new value";
}
It is possible to do this more akin to your example above (by nesting QVariantMaps), but that would be quite ugly, as C++'s static typing would demand manually casting at every access.
Related
I want to create a json in golang for which I need to first create map of the following:
{"inputs": [{"data": {"image": {"url": "SOME_URL"}}}]}
how to create this map on golang. (for now even hardcoded will also work for me)
In a struct:
type SomeData struct {
Inputs []struct {
Data struct {
Image struct {
URL string `json:"url"`
} `json:"image"`
} `json:"data"`
} `json:"inputs"`
}
But if we wanted to be able to add things individually, AND be more idiomatic, we would do it like this:
type Image struct {
URL string `json:"url"`
}
type Data struct {
Image Image `json:"image"`
}
type Input struct {
Data Data `json:"data"`
}
type SomeData struct {
Inputs []Input `json:"inputs"`
}
Then, of course, we could always just use a map:
someData := map[interface{}]interface{}{}
It really just depends on which route you'd like to go. I suggest the second one as it gives you better fine-grained tooling without any pesky dirty tricks or code-clutter.
Hope this helps!
neededMap := make(map[string][]map[string]map[string]map[string]string)
I have a computed array which is full of tags and updates depending on what selection i make in the select box. I would like to take this array and pass it to a method and then run a method to update what “results” have an active class. Although I get an array saying I can’t run forEach on this element.
Been through a few topics and understand computed properties dont work like that but surely there is a way around this.
https://jsfiddle.net/39jb3fzw/6/
Short Snippet
methods: {
updateOutput() {
var tags = this.tagArray;
tags.forEach(function(tag) {
console.log(tag);
})
}
},
computed: {
concatenated: function () {
var ret = this.selected.concat(this.selected2, this.selected3);
this.tagArray = ret;
//this.updateOutput();
return ret;
}
}
Full Output
https://jsfiddle.net/39jb3fzw/6/
Thanks again :slight_smile:
It looks like the issue is the line:
var ret = this.selected.concat(this.selected2, this.selected3);
That line of code is returning an empty string rather than an array. This is because this.selectedX is a string rather than an Array. This explains why tag.forEach is undefined. forEach doesn't exist on the String prototype.
You can create this an array instead be doing
var ret = [ this.selected, this.selected2, this.selected3 ]
From there you can set this.tagArray to ret
Hope this helps
I created some classes with Ruby's C API. I want to create a function whose behavior will change depending on the class of the Ruby object.
I tried to use is_a? from Ruby, however, I don't think it's the good way to do this. I checked "Creating Extension Libraries for Ruby" without success. The only direct way to check classes is with the default types.
I have my class "Klass" already created:
VALUE rb_cKlass = rb_define_class("Klass", rb_cObject);
And how I wanted to check if the class is the good one:
VALUE my_function(VALUE self, VALUE my_argument) {
if(rb_check_class(my_argument), rb_cKlass)) {
// do something if my_argument is an instance of Klass
} else {
return Qnil;
}
}
Is there a way to do this?
I came across this recently, and used the RBASIC_CLASS macro, but was getting segfaults in certain scenarios for some unexplained reason.
After scanning through ruby.h, I found the CLASS_OF macro, which returns the class as VALUE of a given object.
VALUE obj = INT2NUM(10);
VALUE klass = CLASS_OF(obj); // rb_cInteger
Using Ruby 2.5
Every ruby object is internally represented by RObject struct (I will copy the source here for the sake of future readers):
struct RObject {
struct RBasic basic;
union {
struct {
uint32_t numiv;
VALUE *ivptr;
void *iv_index_tbl; /* shortcut for RCLASS_IV_INDEX_TBL(rb_obj_class(obj)) */
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
The very first member, RBasic, defines the class:
struct RBasic {
VALUE flags;
const VALUE klass;
}
To get an access to RBasic metadata of anything, one might use RBASIC macro:
RBASIC(my_argument)
To get the class directly, one might use RBASIC_CLASS macro:
RBASIC_CLASS(my_argument)
If you want to stay close to the is_a? Ruby fashion (i.e. check if any of the ancestors is the expected class), you could directly use the C implementation of is_a?, rb_obj_is_kind_of:
rb_obj_is_kind_of(my_argument, rb_cKlass) // Qtrue OR Qfalse
And since Qfalse == 0, you can just use that method as a condition:
VALUE my_function(VALUE self, VALUE my_argument) {
if(rb_obj_is_kind_of(my_argument, rb_cKlass)) {
// do something if my_argument is an instance of Klass
} else {
return Qnil;
}
}
To find this method, just check Object#is_a? documentation and click to toggle source, you'll see the C implementation if it is a C function (hence this will work for most of the standard lib).
I'm looking for a way to iterate over Tcl array, specified by it's name in C code.
The problem for which this iteration is needed is a SWIG typemap to convert Tcl array to C++ map. The code should look like this:
%typemap(tcl8, in) map<int,int> * (map<int,int> dltmp) {
map<int,int> *cMap = new map<int,int>;
int key, value;
pair<int,int> mapEnt;
// somehow iterate over Tcl array specified by it's name in $source
// to get key and value
{
mapEnt.first = key;
mapEnt.second = value;
cMap.insert(mapEnt);
}
$target = cMap;
}
I have the following code in specman that I inherited:
some_method() is {
var a: bool;
if (!a) {
a = some_other_method();
};
};
My understanding is that each time some_method() is called, a is generated anew, and there's no sense in checking a's value before it's assigned. However, it may be that I'm missing something here. For instance, if a is static then this code makes sense, which brings me to my question:
Is there any way for a variable to be static in specman?
there are no static variables as in C. A variable in a method has its default value (False in this case) if not initialized, so you should be right if (!a) should always be True.
Things would be different if a was a struct member, then like in other OO languages it would retain there value over several method calls and the check would make more sense:
struct some_struct_s {
a : bool;
some_method() is {
if (!a) {
a = some_other_method();
};
};
};
You can check stuff like this also on the interactive prompt:
Specman> var a : bool;
Specman> print a
a = FALSE
There the interactive help is also nice, for example try:
Specman> help variable
and select the entry (by number) sn_eref: variables : declaring. There you will find all relevant information for your question.
Cheers,
Daniel
Static struct members (events, fields, methods) were added to the language in Specman v15.2. A static field can't be generated, physical(%) or used in when subtypes.
struct some_struct_s {
a : bool;
some_method() is {
if (!a) {
a = some_other_method();
};
};
};
-- Change field 'a' for all instances
on xxx { some_struct_s::a = TRUE; };
Here's some comments from the teamspecman blog : Static members in e