Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Selected QLabel?
Forum Updated to NodeBB v4.3 + New Features

Selected QLabel?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • apalomerA Offline
    apalomerA Offline
    apalomer
    wrote on last edited by
    #1

    Hello

    I have a gui with several QLabels with the same QPixamp on it. However, one of them (the first one) appears like this:
    Screenshot from 2022-01-31 08-54-13.png
    You can see how the first one is "selected" or something while the second one is not. When I look into tab order in the Qt Designer, the labels have no tab order (as I would expect) and the only line that interacts with the QLabel widget in the entire code is:

    QIcon ok_icon(":/ok_button.png");
    QIcon no_ok_icon(":/cancell_button.png");
    ui->label1->setPixmap(myCondition()? ok_icon.pixmap(ui->survey_info_label->size()) :
                                                             no_ok_icon.pixmap(ui->survey_info_label->size()));
    ui->label2->setPixmap(myCondition()? ok_icon.pixmap(ui->survey_info_label->size()) :
                                                             no_ok_icon.pixmap(ui->survey_info_label->size()));
    

    Be aware that myCondition() is a fully functional function defined and implemented in another part of the code.

    Any idea on why that first QLabel appears different than the others? I have not been able to reproduce this issue with a small example, sorry. If you can tell me what this style change is in normal conditions I will be able to either change it or revert it.

    Thank you very much in advance.

    JonBJ 1 Reply Last reply
    0
    • apalomerA apalomer

      They are informative in each widget. You have to imagine that label1 lives in two different UI files that end up composed in the same GUI. Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:

      QFrame#frame1 QLabel#label1
      {
        background-color: #bebebe;
        border-radius: 5px;
        color: black;
        font-weight: bold;
      }
      

      But it did no do the trick. Am I missing something or is it just not possible? I guess I will have to change the names at the end....

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #9

      @apalomer said in Selected QLabel?:

      You have to imagine that label1 lives in two different UI files that end up composed in the same GUI

      Then you need to use the UI file/class somewhere in the selector to distinguish. (To be clear: you can use your own class names in the selector, not just Qt inbuilt ones like QFrame or QLabel.)

      Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:

      The available facilities are in the reference I gave, and that does not include "ascendant" direction, only "descendent", as per CSS too.

      I guess I will have to change the names at the end....

      I don't see why. Presumably the different .ui's classes are different, and that should be usable in the selector.

      There is also a supplementary way of distinguishing your labels which may suit. In that same reference page see

      Property Selector QPushButton[flat="false"]

      and https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. You can add your own dynamic properties even in Designer on a widget, there is some "add property plus-button" on the properties on any widget.

      1 Reply Last reply
      2
      • apalomerA apalomer

        Hello

        I have a gui with several QLabels with the same QPixamp on it. However, one of them (the first one) appears like this:
        Screenshot from 2022-01-31 08-54-13.png
        You can see how the first one is "selected" or something while the second one is not. When I look into tab order in the Qt Designer, the labels have no tab order (as I would expect) and the only line that interacts with the QLabel widget in the entire code is:

        QIcon ok_icon(":/ok_button.png");
        QIcon no_ok_icon(":/cancell_button.png");
        ui->label1->setPixmap(myCondition()? ok_icon.pixmap(ui->survey_info_label->size()) :
                                                                 no_ok_icon.pixmap(ui->survey_info_label->size()));
        ui->label2->setPixmap(myCondition()? ok_icon.pixmap(ui->survey_info_label->size()) :
                                                                 no_ok_icon.pixmap(ui->survey_info_label->size()));
        

        Be aware that myCondition() is a fully functional function defined and implemented in another part of the code.

        Any idea on why that first QLabel appears different than the others? I have not been able to reproduce this issue with a small example, sorry. If you can tell me what this style change is in normal conditions I will be able to either change it or revert it.

        Thank you very much in advance.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @apalomer
        If you CTRL+click on the label does it deselect/change appearance?

        1 Reply Last reply
        0
        • apalomerA Offline
          apalomerA Offline
          apalomer
          wrote on last edited by
          #3

          No, it does not.

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #4

            since you are working in designer, make sure the properties of label1 and label2 are the same. Also move your size() check outside of the ternary assignments, and into a temporary.

            If you meet the AI on the road, kill it.

            1 Reply Last reply
            0
            • apalomerA Offline
              apalomerA Offline
              apalomer
              wrote on last edited by
              #5

              I did check and both labels have the same properties in the designer. I even replaced the one with the non desired behaviour with a copy of the one that has the good behavior just in case I miss some parameters (obviously changing the hame to its original name). I also changed the code to:

              auto check = myCondition();
              auto sz = ui->label1->size();
              QPixmap px;
              if (check)
                px = ok_icon.pixmap(sz);
              else
                px = no_ok_icon.pixmap(sz);
              ui->label1->setPixmap(px);
              ui->label2->setPixmap(px);
              

              With the same result.
              Any other ideas?

              1 Reply Last reply
              0
              • apalomerA Offline
                apalomerA Offline
                apalomer
                wrote on last edited by
                #6

                I have been able to track down the error. I have a style sheet that sets the properties of label1 widget in the following way:

                QWidget#label1
                {
                  background-color: #bebebe;
                  border-radius: 5px;
                  color: black;
                  font-weight: bold;
                }
                

                The problem is that I have another widget named label1 in one of the widgets that I include in my main GUI. Is there a way to discriminate the widget's parents or anything in the style sheet? I would not like to change the names of the labels as they are informative as they are now (obviously they are not named label1).

                JonBJ 1 Reply Last reply
                0
                • apalomerA apalomer

                  I have been able to track down the error. I have a style sheet that sets the properties of label1 widget in the following way:

                  QWidget#label1
                  {
                    background-color: #bebebe;
                    border-radius: 5px;
                    color: black;
                    font-weight: bold;
                  }
                  

                  The problem is that I have another widget named label1 in one of the widgets that I include in my main GUI. Is there a way to discriminate the widget's parents or anything in the style sheet? I would not like to change the names of the labels as they are informative as they are now (obviously they are not named label1).

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @apalomer
                  They do not seem to be very informative if you have two different labels named "whatever-it-is" yet you want a different style on one of them!

                  Is there a way to discriminate the widget's parents or anything in the style sheet?

                  Yes indeed. You can reference parentage and many other things. (One obvious question is why you address a QLabel via a QWidget rule instead of QLabel. However that will not solve if both your "whatever-it-is" are both Qlabels.) Anyway, read through https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types, e.g.

                  Descendant Selector	QDialog QPushButton	Matches all instances of QPushButton that are descendants (children, grandchildren, etc.) of a QDialog.
                  Child Selector	QDialog > QPushButton	Matches all instances of QPushButton that are direct children of a QDialog.
                  
                  1 Reply Last reply
                  0
                  • apalomerA Offline
                    apalomerA Offline
                    apalomer
                    wrote on last edited by
                    #8

                    They are informative in each widget. You have to imagine that label1 lives in two different UI files that end up composed in the same GUI. Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:

                    QFrame#frame1 QLabel#label1
                    {
                      background-color: #bebebe;
                      border-radius: 5px;
                      color: black;
                      font-weight: bold;
                    }
                    

                    But it did no do the trick. Am I missing something or is it just not possible? I guess I will have to change the names at the end....

                    JonBJ 1 Reply Last reply
                    0
                    • apalomerA apalomer

                      They are informative in each widget. You have to imagine that label1 lives in two different UI files that end up composed in the same GUI. Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:

                      QFrame#frame1 QLabel#label1
                      {
                        background-color: #bebebe;
                        border-radius: 5px;
                        color: black;
                        font-weight: bold;
                      }
                      

                      But it did no do the trick. Am I missing something or is it just not possible? I guess I will have to change the names at the end....

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #9

                      @apalomer said in Selected QLabel?:

                      You have to imagine that label1 lives in two different UI files that end up composed in the same GUI

                      Then you need to use the UI file/class somewhere in the selector to distinguish. (To be clear: you can use your own class names in the selector, not just Qt inbuilt ones like QFrame or QLabel.)

                      Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:

                      The available facilities are in the reference I gave, and that does not include "ascendant" direction, only "descendent", as per CSS too.

                      I guess I will have to change the names at the end....

                      I don't see why. Presumably the different .ui's classes are different, and that should be usable in the selector.

                      There is also a supplementary way of distinguishing your labels which may suit. In that same reference page see

                      Property Selector QPushButton[flat="false"]

                      and https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. You can add your own dynamic properties even in Designer on a widget, there is some "add property plus-button" on the properties on any widget.

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved