std vector, QVector and fields..
-
@Chris-Kawa
Just as briefly as I can, because I cannot abide any harm to kittens....- In my current project, I have a principal form which has 37 individual
QSpinBox
es for individual, distinct, Qt Creator-designed numerics. - I am not prepared to subclass
QSpinBox
for use in the Designer either via Promotion or via Custom Widget. Let's just take that as a given, please. - I need to apply some initial changes to all of them, to do with aligning, coloring. delegating & connecting slots.
- Following the discussion, I can do this in one of 3 ways:
-
Explicitly call code on each individual
ui->spin...
variable. Fastest. Lots & lots of lines of code, doesn't notice if I add an extra spinbox. -
Explicitly put those pointers into an array/list/vector by referencing each one at compile-time. Doesn't matter which container, differences are below negligible, compared to what is done on each spin box. (I am not saying this would be the case if I were doing a non-UI, time-critical operation.) Reduces lines of code a lot, still doesn't help with maintaining the list if I add an extra one.
-
Use
QObject::findChildren<QSpinBox*>
at run-time to change all such spinboxes, which is exactly what I want, and do. Slowest. Small code, no worries about maintaining a changing list.
The really important thing about solution #3 is that no kittens, fluffy or otherwise, are harmed in the course of running my application. I would guess the overhead is in milliseconds. Certainly way faster than my eyes can look at the screen or my hands can try to interact with the application. Because that's what humans & computer UIs are like.
I say this hopefully not to fight with you but because it may relate the situation the OP is asking about.
@JonB said:
I would guess the overhead is in milliseconds
I work in game dev, where we have 16ms (or 8, depending on an FPS target) to do friggin' everything, so with a statement like that you wouldn't get very popular here :) Not every field has those restrictions of course, but just because you don't have to doesn't mean you shouldn't self-impose those restrictions on yourself even in a more relaxed environment.
My point is that it's not about what you or the user can see or touch. We're waaaay past that perception point in simple uis. For all a user cares you can use a QMap with QString keys and tripple pointer indirection and no one would be able to tell (that's kinda what findChildren does actually + some casts and branches). But the CPU does the work none the less. It spins those cycles, it takes that electricity, it eats that battery away, it uses up that cache causing evictions to other processes and causing them to use just a little bit more resources. It makes everything else on the system just a little bit more sluggish every time. Given that a random PC is running dozens or hundreds of processes and threads at any given time if everyone coded like this we would be in a very bad place. I hate that every little update and every little app installed makes my PC just a tiny bit slower. It amounts. Nothing runs in isolation.
no kittens, fluffy or otherwise, are harmed in the course of running my application
that you know of, but if what you're saying is true... they die man, they die ;)
I say this hopefully not to fight with you but because it may relate the situation the OP is asking about.
I hope what I'm saying doesn't come across as fighting. All good here. Just trying to raise awareness of those things because with the rise of the various VMs and interpreted languages performance and battery life are getting destroyed across the board the last few years and it just kills me (along with the kittens) to witness it. I just can't stand that "it doesn't matter, hardware will pick up the slack" programming culture that's on the rise. Don't want to be a silent bystander so I'm bitchn' a bit.
- In my current project, I have a principal form which has 37 individual
-
@Chris-Kawa
Just as briefly as I can, because I cannot abide any harm to kittens....- In my current project, I have a principal form which has 37 individual
QSpinBox
es for individual, distinct, Qt Creator-designed numerics. - I am not prepared to subclass
QSpinBox
for use in the Designer either via Promotion or via Custom Widget. Let's just take that as a given, please. - I need to apply some initial changes to all of them, to do with aligning, coloring. delegating & connecting slots.
- Following the discussion, I can do this in one of 3 ways:
-
Explicitly call code on each individual
ui->spin...
variable. Fastest. Lots & lots of lines of code, doesn't notice if I add an extra spinbox. -
Explicitly put those pointers into an array/list/vector by referencing each one at compile-time. Doesn't matter which container, differences are below negligible, compared to what is done on each spin box. (I am not saying this would be the case if I were doing a non-UI, time-critical operation.) Reduces lines of code a lot, still doesn't help with maintaining the list if I add an extra one.
-
Use
QObject::findChildren<QSpinBox*>
at run-time to change all such spinboxes, which is exactly what I want, and do. Slowest. Small code, no worries about maintaining a changing list.
The really important thing about solution #3 is that no kittens, fluffy or otherwise, are harmed in the course of running my application. I would guess the overhead is in milliseconds. Certainly way faster than my eyes can look at the screen or my hands can try to interact with the application. Because that's what humans & computer UIs are like.
I say this hopefully not to fight with you but because it may relate the situation the OP is asking about.
@JonB said in std vector, QVector and fields..:
I need to apply some initial changes to all of them, to do with aligning, coloring. delegating & connecting slots.
You know, if all those operations are constexpr-able you could do them during compile time!
Ha dreams 😥 - In my current project, I have a principal form which has 37 individual
-
@JonB said in std vector, QVector and fields..:
I need to apply some initial changes to all of them, to do with aligning, coloring. delegating & connecting slots.
You know, if all those operations are constexpr-able you could do them during compile time!
Ha dreams 😥wrote on 29 Oct 2020, 07:01 last edited by JonB@J-Hilk
Not sure what you mean. Even if they are, the question we are debating is: how do you apply them across 47 design-time (non-subclassed) separateQSpinBox
es? And maintain for future ones which might be added? I'm not prepared to write it against 47 member variables, if I put them into a list/array/vector I have to write/maintain that which equally I don't want to do, so leaves me withQObject::findChildren<QCheckBox *>
as the easiest way to implement. Which @Chris-Kawa has informed me will kill my beloved pussy cat, so I feel terrible :( -
@J-Hilk
Not sure what you mean. Even if they are, the question we are debating is: how do you apply them across 47 design-time (non-subclassed) separateQSpinBox
es? And maintain for future ones which might be added? I'm not prepared to write it against 47 member variables, if I put them into a list/array/vector I have to write/maintain that which equally I don't want to do, so leaves me withQObject::findChildren<QCheckBox *>
as the easiest way to implement. Which @Chris-Kawa has informed me will kill my beloved pussy cat, so I feel terrible :(@JonB
you saidI need to apply some initial changes to all of them
which means, you know (theoretically) during compile time what your values should be.
Now, if all your functions on QCheckBox were constexpr and find children would be as well, you could create a small constexpr function of your own, that finds all QCheckBox and sets the
initial
values and call that somewhere early on. The compiler would evaluate those calls during compile time and set the initial values accordingly without runtime overhead.
no kittens required to die, in fact you would have more time, due to longer compile times, to pet them
-
@JonB
you saidI need to apply some initial changes to all of them
which means, you know (theoretically) during compile time what your values should be.
Now, if all your functions on QCheckBox were constexpr and find children would be as well, you could create a small constexpr function of your own, that finds all QCheckBox and sets the
initial
values and call that somewhere early on. The compiler would evaluate those calls during compile time and set the initial values accordingly without runtime overhead.
no kittens required to die, in fact you would have more time, due to longer compile times, to pet them
wrote on 29 Oct 2020, 07:21 last edited by JonB@J-Hilk said in std vector, QVector and fields..:
you could create a small constexpr function of your own, that finds all QCheckBox
Not that I can imagine doing this, but could you give an example of how I would do this, at compile-time? Or is that based on a hypothetical "if all your functions on QCheckBox were constexpr and find children would be as well"? I don't follow how you would get a list at compile-time of all
QCheckBox
es I have created in Designer (without my having to type that into the source code manually)? -
@J-Hilk said in std vector, QVector and fields..:
you could create a small constexpr function of your own, that finds all QCheckBox
Not that I can imagine doing this, but could you give an example of how I would do this, at compile-time? Or is that based on a hypothetical "if all your functions on QCheckBox were constexpr and find children would be as well"? I don't follow how you would get a list at compile-time of all
QCheckBox
es I have created in Designer (without my having to type that into the source code manually)?@JonB it is hypothetical, because I'm pretty sure setupUi is not constexpr
but as example, take this compiletime calculation of fibonacci
constexpr int64_t fibonacci(const int64_t n) { return n < 1 ? -1 : (n == 1 || n == 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2)); } int main (int argc, char *argv[]) { constexpr auto evaled = fibonacci(25); return evaled; }
actually had to modify the example to force the compiletime evaluation , a little bit finicky the whole system (still)
-
@JonB it is hypothetical, because I'm pretty sure setupUi is not constexpr
but as example, take this compiletime calculation of fibonacci
constexpr int64_t fibonacci(const int64_t n) { return n < 1 ? -1 : (n == 1 || n == 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2)); } int main (int argc, char *argv[]) { constexpr auto evaled = fibonacci(25); return evaled; }
actually had to modify the example to force the compiletime evaluation , a little bit finicky the whole system (still)
wrote on 29 Oct 2020, 07:39 last edited by JonB@J-Hilk
OK, yes, I know about turning the C++ compiler into an interpreter withconstexpr
:) But thatfibonacci
function is self-contained. Compiler is not going to be able to execute anything likeconstexpr QObject::findChildren<QCheckBox *>
, it's runtime-only.Purely BTW. I'm not a great programmer (nor mathematician), so while I develop my code I have a go at:
constexpr int64_t fibonacci(const int64_t n) { int64_t result = 0; while (n > 0) result += n; return result; }
Now I compile my code. Does the compilation simply hang??
-
@J-Hilk
OK, yes, I know about turning the C++ compiler into an interpreter withconstexpr
:) But thatfibonacci
function is self-contained. Compiler is not going to be able to execute anything likeconstexpr QObject::findChildren<QCheckBox *>
, it's runtime-only.Purely BTW. I'm not a great programmer (nor mathematician), so while I develop my code I have a go at:
constexpr int64_t fibonacci(const int64_t n) { int64_t result = 0; while (n > 0) result += n; return result; }
Now I compile my code. Does the compilation simply hang??
@JonB said in std vector, QVector and fields..:
Now I compile my code. Does the compilation simply hang??
No, because you're not calling it :-)
And it would not hang if you would call it with any number as parameter. It could probably slow down compilation if you would pass a huge number :-) -
@JonB said in std vector, QVector and fields..:
Now I compile my code. Does the compilation simply hang??
No, because you're not calling it :-)
And it would not hang if you would call it with any number as parameter. It could probably slow down compilation if you would pass a huge number :-)wrote on 29 Oct 2020, 07:43 last edited by JonB@jsulm
Oh yes I am! In @J-Hilk's code (at least originally) hismain()
callsfibonacci(10)
, so assume that, and then tell me what happens while I sit waiting for the compilation to finish?And it would not hang if you would call it with any number as parameter. It could probably slow down compilation if you would pass a huge number :-)
I think you should look at the code again... :)
-
@jsulm
Oh yes I am! In @J-Hilk's code (at least originally) hismain()
callsfibonacci(10)
, so assume that, and then tell me what happens while I sit waiting for the compilation to finish?And it would not hang if you would call it with any number as parameter. It could probably slow down compilation if you would pass a huge number :-)
I think you should look at the code again... :)
@JonB OK, I see n > 0. You should try :-)
-
@jsulm
Oh yes I am! In @J-Hilk's code (at least originally) hismain()
callsfibonacci(10)
, so assume that, and then tell me what happens while I sit waiting for the compilation to finish?And it would not hang if you would call it with any number as parameter. It could probably slow down compilation if you would pass a huge number :-)
I think you should look at the code again... :)
@JonB Add at least a function call to your calculation, then you get an stack overflow(eventually)
-
You should try :-)
I'm terrified of seizing up my Linux VM :) It already does that if I accidentally debug from Creator when inside a
QComboBox
clicked slot, and I have to hard-switch-off the whole VM... :(main.cpp:158:18: error: expression is not an integral constant expression main.cpp:152:13: note: constexpr evaluation hit maximum step limit; possible infinite loop? main.cpp:158:18: note: in call to 'fibonacci(25)'
smart things these compilers
-
@JonB Add at least a function call to your calculation, then you get an stack overflow(eventually)
wrote on 29 Oct 2020, 07:49 last edited by@J-Hilk said in std vector, QVector and fields..:
@JonB Add at least a function call to your calculation, then you get an stack overflow(eventually)
But I don't want a stackoverflow to terminate! I want to know whether the compiler sits there forever! Which is why I wrote as I did.
OK, I'm off to try....
-
main.cpp:158:18: error: expression is not an integral constant expression main.cpp:152:13: note: constexpr evaluation hit maximum step limit; possible infinite loop? main.cpp:158:18: note: in call to 'fibonacci(25)'
smart things these compilers
wrote on 29 Oct 2020, 07:50 last edited by@J-Hilk said in std vector, QVector and fields..:
note: constexpr evaluation hit maximum step limit; possible infinite loop?
Ah ha! So..... this C++
constexpr
has what "maximum step limit`? Where is that in the spec? :) -
@J-Hilk said in std vector, QVector and fields..:
note: constexpr evaluation hit maximum step limit; possible infinite loop?
Ah ha! So..... this C++
constexpr
has what "maximum step limit`? Where is that in the spec? :)@JonB up to you with the compiler option
-fconstexpr-steps
-
wrote on 29 Oct 2020, 07:56 last edited by
@J-Hilk
Wow! They think of everything! I wonder if that would accept-fconstexpr-steps fibonacci(25)
;-)OK, enough now, thanks!
22/29