0

To describe the situation, I have numerous labels that follow a naming pattern of one11, one12 etc. The way I am attempting to access every label is by creating a QLabel pointer, which seems to be working okay. But then, when I attempt to access the background/button color of that label, my app crashes. Here is my code that I am attempting to use, and the app crashes on the last line in the first loop, am I using improper commands? Or possibly misunderstanding how to use the pointer?

Floor is a global string variable that keeps track of the users current tab in a stacked widget.(e.g. first panel = "one").

for(unsigned int i = 1; i <= 5; i ++){
    for(unsigned int j = 1; j <= 5; j ++){
        QString UI = (Floor + i + j);

        QLabel * lbl = this->findChild<QLabel*>(UI);
        QColor color = lbl->palette().button().color();
Seyr
  • 1
  • 1
  • 1
    What happens if no child was found? Meaning you should check first before dereferecncing a null pointer. – drescherjm Mar 11 '20 at 03:54
  • `QString UI = (Floor + i + j);` Does not seem correct. What is `Floor`? – drescherjm Mar 11 '20 at 03:56
  • @drescherjm floor is maybe the prefix, it looks like OP is doing an hotel management GUI.... – ΦXocę 웃 Пepeúpa ツ Mar 11 '20 at 07:40
  • Presently, it seems to be finding the child, at least from running the debugger. Yes, my apologies, floor is a string variable that says "one" and changes depending on where you are in the stacked widget. (e.g. first panel is "one" second is "two" and so on) so the QString comes out to "one11" on the first pass then "one12" on the second. – Seyr Mar 11 '20 at 18:08
  • @drescherjm you were completely right, I wasn't finding the correct child. Thank you for pointing that out. – Seyr Mar 11 '20 at 18:21

1 Answers1

0

one of the comments made me realize I wasn't actually finding the child at all, as me QString UI was coming out to one/u001/u001 not one11. this is what i have now that works swimmingly.

for(unsigned int i = 1; i <= 5; i ++){
    for(unsigned int j = 1; j <= 5; j ++){
        QString iConverted = QString::number(i);
        QString jConverted = QString::number(j);
        QString UI = (Floor + iConverted + jConverted);

        qDebug() << UI;

        QLabel * lbl = this->findChild<QLabel*>(UI);
        QColor color = lbl->palette().button().color();
Seyr
  • 1
  • 1
  • You may want to use `QString().arg()` for this. There are some examples in the following question: [https://stackoverflow.com/questions/5249598/how-to-deal-with-1-in-the-argument-of-qstringarg](https://stackoverflow.com/questions/5249598/how-to-deal-with-1-in-the-argument-of-qstringarg) – drescherjm Mar 11 '20 at 18:45