SwiftUI conditional .buttonStyle - swiftui

I am trying to change the style of a button based on a condition like this:
.buttonStyle((selection == index) ? .borderedProminent : .bordered)
Strangely it throws this error:
Type 'ButtonStyle' has no member 'borderedProminent'
I suppose I am making a syntax mistake?

Related

c++ variadic template into AND statement

I am trying use a variadic template in an && statement, but i dont know how to actually do it. Can somebody explain to me how i can programm a function like this one:
using EntitySet = std::vector<Entity>;
template<typename... TArgs>
EntitySet getEntitesWith()
{
EntitySet entitySet;
for(const Entity& entity : m_entitySet)
{
if (entity.hasComponent<T1>() && entity.hasComponent<T2>() && ...)
{
entitySet.push_back(entity);
}
}
return entitySet;
}
entity.hasComponent<>() returns true if the entity has a Component of the type that is passed into the function
Edit:
i just tried the Fold Expression in c++17 and when i tried to do it like #Igor Tandetnik and #oisyn told me to i got an Unexpected ... Token Error.
But when i wrote the code like this:
if (( ... && entity.hasComponent<TArgs>()))
With an extra () i got an internal compiler error and have no idea on what im doing wrong.
anybody have an idea on why this is happening?

Chapel eltType methods

I have a class which contains a field that has an array of eltTypes. This looks like this:
type eltType;
var size = 5;
var elementsDomain: domain(1) = {0..size-1};
var elements: [elementsDomain] eltType;
Later on, I have a method that utilizes this (still inside of the class) like this:
proc add(eltType el){
elements[0] = el;
}
I get a compiler error that says it has a syntax error at the add method signature. I thought eltTypes worked like generic types in java. How exactly am I doing eltTypes wrong, and how are they supposed to work?
You need to declare the type of el in the following format:
proc add(el : eltType) {
Chapel does a good job of figuring out types (except for fruitful recursive functions) so it should also work if you leave the type out and say:
proc add(el) {

C++ Changing the values of members of a const struct [duplicate]

This question already has answers here:
C/C++ changing the value of a const
(18 answers)
Closed 8 years ago.
I have a struct which is defined in types.h with the following code:
struct data_Variant {
FlightPlanSteeringDataRecord steeringData;
FlightPlanType flightPlan : 8;
MinitoteLegDataType legDataType : 8; // discriminent, either current or amplified
unsigned spare : 16;
union {
// currentLeg =>
CurrentLegDataRecord currentLegData;
// amplifiedLeg =>
AmplifiedLegDataRecord amplifiedLegData;
} u;
};
I am then trying to pass an instance of that struct as a parameter to a function in a C++ source file called dialogue.cpp:
void dialogue::update( const types::data_Variant& perfData){
...
}
I now want to change the value of some of the members of that struct inside this update() function. However, if I try doing this as I usually would, i.e.
perfData.etaValid = true;
I get a compile error which says: "C2166: l-value specifies const object". As I understand, this is because perfData has been declared as a constant variable. Am I correct in thinking this?
Since I didn't write this part of the code, but only want to use it to update the value displayed on the GUI, I don't really want to change the perfData variable by removing the const keyword, in case I break something else. Is there any way to change the value of a variable that has been declared as const?
I have tried declaring the same struct variable in another part of the code, without using the const keyword, to see if I can change the values of some of its members there... i.e. in Interface.cpp, I have added the following code to a function called sendData():
types::data_Variant& perfData;
perfData.steering.etaValid = true;
perfData.steering.ttgValid = true;
However, I now get the following compile errors on these lines:
error C2653: 'types' is not a class or namespace name
error C2065: data_Variant: undeclared identifier
error C2065: 'perfData': undeclared identifier
error C2228: left of '.steering' must have class/ struct/ union
Is there a way of updating the values of this struct? If so, how should I do it, and what am I doing wrong here?
I have added the following function to the dialogue.cpp source file, as suggested in the answer:
void dialogue::setFPTTGandETAValidityTrue(
FlightPlanMinitoteTypes::FlightPlanMinitoteData_Variant& perfData)
{
SESL_FUNCTION_BEGIN(setFPTTGandETAValidityTrue)
perfData.steeringData.fpETAValid = true;
perfData.steeringData.fpTTGValid = true;
SESL_FUNCTION_END()
}
You could add a wrapper for yourself.
void myupdate(dialogue& dia, types::data_Variant& perfData)
{
perfData.etaValid = true;
dia.update(perfData);
}
Then call myupdate() instead of dialogue::update().
You declare
void dialogue::update( const types::data_Variant& perfData){
...
}
that const is a declaration of you saying: "I won't modify the referenced object in this function". If you want to modify it in dialogue::update you have to remove the const keyword. Wrapping is not a solution, in my opinion, makes the code harder to maintain. Also I vote against remove const with const_cast.
The correct solution is to remove const from method declaration if you want to modify the referenced object inside that function.

How to iterate through every QSpinBox in QGridLayout

I have the following problem. I'm using Qt in the newest version 5.2 and in my code I have 11QSpinBox. Every in QGridLayout. I want to iterate through every QSpinBox in this layout and set value that I read from file. When I'm using:
ui.QSpinBox->setValue()
is everything ok, but the code looks too long.
I tried using:
static_cast<QSpinBox*>(ui.gridLayout->itemAtPosition(1,1)->widget())->setValue(100);
but I get exception Access violation and MS VS2010 show me this function in qatomic_mscv.h:
inline bool QAtomicOpsBySize<4>::deref(long &_q_value) Q_DECL_NOTHROW
{
return QT_INTERLOCKED_DECREMENT(&_q_value) != 0;
}
So i thought about not using pointer to QSpinBox but an object:
static_cast<QSpinBox>(ui.gridLayout->itemAtPosition(1,1)->widget()).setValue(100);
but it didn't change the value. Any exception didn't occur.
OK, I used even dynamic_cast with pointer type and I got again access Violation but this time VS2010
point me to qscopedpointer.h:
inline T *data() const
{
return d;
}
It happens for qobject_cast too.
I'm sure that position point to QSpinBox. What I'm doing wrong?
Consider using qFindChildren instead:
QList<QSpinBox*> spinBoxen = ui.findChildren<QSpinBox*>();
Q_FOREACH(QSpinBox *spinBox, spinBoxen) {
// do something to do the spinBox
}

jsoncpp how to check if tag is null .isNull() throw assertion

Im using jsoncpp , its great but when i need to check if json structure contains tag
when i do it with :
UserRoot0["error"].isNull()
its throws me assert from json_value.cpp line 1025
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
i want to check if response im getting is from this type:
{
"error" : {
"message" : "Error validating application.",
"type" : "OAuthException",
"code" : 190
}
}
The [] operator is only valid for JsonValue objects that are of type Object or null. All others (Int, Bool, Array, etc.) will assert.
If your UserRoot0 object is an Array or some other non-Object type, you have some more work to do (like iterating into sub-nodes) to find your target node that may or may not contain the error. Print UserRoot0.toStyledString() to see what your JSON looks like, and make sure it looks like a JSON Object (see json.org for a nice overview of what that is).
A "ToDo" comment at the top of the json_value.cpp source file (where JSON_ASSERT is defined) implies that the developers may be planning more robust error handling instead of these asserts in future versions, but in the meantime, you can check yourself, like this:
if(UserRoot0.isObject() && UserRoot0.isMember("error"))
// Process error node
else
// This node isn't an Object node or doesn't contain the "error" key
The isMember() check will also assert for non-Object nodes, so be sure to check isObject() before checking isMember() if UserRoot0 isn't guaranteed to be an Object.
I stumbled on this too. As Ennael says you need to make sure you are dealing with an object type. FWIW my problem was caused by using JSON like this:
"error" : {
"code" : 190
}
... instead of what I intended:
{
"error" : {
"code" : 190
}
}
Excluding the outer set of parenthesis will cause the value type to become string instead of object.