createEditor of a custom widget is not being called. How to debug?
-
wrote on 22 Jun 2024, 14:06 last edited by
To be more precise, the mentioned std::vector<int>, that's somehow empty, is a private field of the corresponding model. The aforementioned star delegate example also makes intensive use of such private fields in its class definitions. Should I consider this bad practice, too?
-
To be more precise, the mentioned std::vector<int>, that's somehow empty, is a private field of the corresponding model. The aforementioned star delegate example also makes intensive use of such private fields in its class definitions. Should I consider this bad practice, too?
wrote on 22 Jun 2024, 14:15 last edited by JonB@andi456 said in createEditor of a custom widget is not being called. How to debug?:
the mentioned std::vector<int>, that's somehow empty, is a private field of the corresponding model.
So isn't that exactly the shadowing I suggested? If you declare a variable in an inner scope (e.g. inside a class when you have a global, or inside a function where you have a global or class member) of the same name as an outer scope you access the inner one, unless you do something to access the outer one (e.g.
this->...
or::...
). This is all basic C++.If star delegate example is using private variables I very much doubt they are using globals or have got their code wrong. If yours is wrong (as it sounds) then you must fix it. Why would you declare a local/member variable with the same name if you have some global you want to access instead? Better yet don't use a global in the first place.
-
@andi456 said in createEditor of a custom widget is not being called. How to debug?:
the mentioned std::vector<int>, that's somehow empty, is a private field of the corresponding model.
So isn't that exactly the shadowing I suggested? If you declare a variable in an inner scope (e.g. inside a class when you have a global, or inside a function where you have a global or class member) of the same name as an outer scope you access the inner one, unless you do something to access the outer one (e.g.
this->...
or::...
). This is all basic C++.If star delegate example is using private variables I very much doubt they are using globals or have got their code wrong. If yours is wrong (as it sounds) then you must fix it. Why would you declare a local/member variable with the same name if you have some global you want to access instead? Better yet don't use a global in the first place.
wrote on 22 Jun 2024, 14:30 last edited by@JonB Maybe, it's a matter of terminology, I'm not sure. If you have a look at starrating.h and starrating.cpp, you'll notice two private fields
QPolygonF starPolygon; QPolygonF diamondPolygon;
both are being populated in the constructor:
StarRating::StarRating(int starCount, int maxStarCount) : myStarCount(starCount), myMaxStarCount(maxStarCount) { starPolygon << QPointF(1.0, 0.5); for (int i = 1; i < 5; ++i) starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14), 0.5 + 0.5 * std::sin(0.8 * i * 3.14)); diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4) << QPointF(0.6, 0.5) << QPointF(0.5, 0.6) << QPointF(0.4, 0.5); }
Is this bad practice? I have done more or less the same in my model constructor...
-
@JonB Maybe, it's a matter of terminology, I'm not sure. If you have a look at starrating.h and starrating.cpp, you'll notice two private fields
QPolygonF starPolygon; QPolygonF diamondPolygon;
both are being populated in the constructor:
StarRating::StarRating(int starCount, int maxStarCount) : myStarCount(starCount), myMaxStarCount(maxStarCount) { starPolygon << QPointF(1.0, 0.5); for (int i = 1; i < 5; ++i) starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14), 0.5 + 0.5 * std::sin(0.8 * i * 3.14)); diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4) << QPointF(0.6, 0.5) << QPointF(0.5, 0.6) << QPointF(0.4, 0.5); }
Is this bad practice? I have done more or less the same in my model constructor...
wrote on 22 Jun 2024, 14:37 last edited by JonB@andi456
I really don't know what you are thinking of. I don't see where terminology comes into anything. The code shown is just fine. Populating private members in constructor is more than usual. And nothing we are discussing has any relevance to whether you have a class to do with a model or anything else.You may assume as a rule of thumb that the Qt examples are reasonable C++ coding.
Your problem is (a) having a global variable in the first place and (b) having a member variable with the same name as the global.
-
@JonB Maybe, it's a matter of terminology, I'm not sure. If you have a look at starrating.h and starrating.cpp, you'll notice two private fields
QPolygonF starPolygon; QPolygonF diamondPolygon;
both are being populated in the constructor:
StarRating::StarRating(int starCount, int maxStarCount) : myStarCount(starCount), myMaxStarCount(maxStarCount) { starPolygon << QPointF(1.0, 0.5); for (int i = 1; i < 5; ++i) starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14), 0.5 + 0.5 * std::sin(0.8 * i * 3.14)); diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4) << QPointF(0.6, 0.5) << QPointF(0.5, 0.6) << QPointF(0.4, 0.5); }
Is this bad practice? I have done more or less the same in my model constructor...
wrote on 22 Jun 2024, 14:44 last edited by Pl45m4@andi456 said in createEditor of a custom widget is not being called. How to debug?:
Is this bad practice? I have done more or less the same in my model constructor...
In your example this
starPolygon << QPointF(1.0, 0.5);
accesses this
QPolygonF starPolygon;
which is not a global variable.
@andi456 said in createEditor of a custom widget is not being called. How to debug?:
Anyway, it's strange that a globally declared vector loses its contents in one particular model and not in others
Here you wrote you use a globally declared vector... which is not the same as used in the example.
Or are you refering to the above code as "globally declared"... then it's wrong terminology because globally declared is something like:// global counter int counter = 0; class A { A(); private: // private counter as a class member int counter; };
writing
counter = 42;
in youa.cpp
would shadow the private membercounter
cause it's ambiguous and it's not clear if you want to set the global counter to 42 or the private member of classA
. -
wrote on 22 Jun 2024, 16:00 last edited by
Mea culpa, mea maxima culpa, I used the wrong notion of "global variable", sorry for that. Private field would have been better, I suppose.
Well, I don't know what exactly interferes with the std::vector<int> field, which is not visible outside the model, but as the star delegate example code is okay, I will stick with std::map solution, which is analogous....
-
Mea culpa, mea maxima culpa, I used the wrong notion of "global variable", sorry for that. Private field would have been better, I suppose.
Well, I don't know what exactly interferes with the std::vector<int> field, which is not visible outside the model, but as the star delegate example code is okay, I will stick with std::map solution, which is analogous....
wrote on 22 Jun 2024, 16:11 last edited by@andi456 said in createEditor of a custom widget is not being called. How to debug?:
Well, I don't know what exactly interferes with the std::vector<int> field
How are you passing the the vector around?
-
wrote on 22 Jun 2024, 17:23 last edited by
No, I just populate it in the model constructor in a somewhat cumbersome manner. After that it is only being used in the flags method of the model. As I said, it is a little strange, because the approach works in the other models of the application...
-
No, I just populate it in the model constructor in a somewhat cumbersome manner. After that it is only being used in the flags method of the model. As I said, it is a little strange, because the approach works in the other models of the application...
-
wrote on 22 Jun 2024, 19:06 last edited by andi456
Actually, I've begun to put the std::map approach to use it in the other models too, because it is much less awkward. When I'm done with that, I will show the code especially, if I encounter any errors. Thanks for your help, anyway.
16/16