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. QPushbutton add text over button without using stylesheet & image
QtWS25 Last Chance

QPushbutton add text over button without using stylesheet & image

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 6.5k Views
  • 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.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by
    #1

    Hello,
    actually I add my custom text over my QPushbutton using stylesheet property "image".

    There are some trik for write a text over QPushbutton, ex: "press here" black when normal state and red when checked state?

    I have a lot of QPushbutton in my gui and I'm planning to use qtransalte so the use of prefix "tr" in front of the customized text for each button would be desirable.

    regards
    Giorgio

    bkt

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by mostefa
      #2

      Hi @gfxx

      Not sure i understand what do you want , but normally you can add text to qpushbutton using the setText function

      Let's say you have created a QPushButton named pushButton with Qt Designer:

      To set your text you have to do something like this:

      ui->pushButton->setText("Press Here");

      So i am not able to understand what is the purpose of using image property ?

      ui->pushButton->setText(tr("Press Here"));
      

      Here is a minimal sample code to do what you want:

      widget contain your QPushButton:

      widget.cpp

          ui->pushButton->setCheckable(true);
          ui->pushButton->setText(tr("Press Here"));
      
          this->setStyleSheet("QPushButton::checked{background-color: red;color: white;} "
                              "\n "
                              "QPushButton{background-color:  black;color: white;}");
      

      alt text

      gfxxG 1 Reply Last reply
      0
      • M mostefa

        Hi @gfxx

        Not sure i understand what do you want , but normally you can add text to qpushbutton using the setText function

        Let's say you have created a QPushButton named pushButton with Qt Designer:

        To set your text you have to do something like this:

        ui->pushButton->setText("Press Here");

        So i am not able to understand what is the purpose of using image property ?

        ui->pushButton->setText(tr("Press Here"));
        

        Here is a minimal sample code to do what you want:

        widget contain your QPushButton:

        widget.cpp

            ui->pushButton->setCheckable(true);
            ui->pushButton->setText(tr("Press Here"));
        
            this->setStyleSheet("QPushButton::checked{background-color: red;color: white;} "
                                "\n "
                                "QPushButton{background-color:  black;color: white;}");
        

        alt text

        gfxxG Offline
        gfxxG Offline
        gfxx
        wrote on last edited by gfxx
        #3

        @mostefa very thanks for your reply .... this morning a fresh mind, I realized that they have already in my pocket answer ... in fact believed that the property "color" would change border color too .... but it is not true, the property change only text color .... so I change my code of QtCreator from so ....

        QPushButton {font-weight: bold;color: #5E749C; border: 3px solid #5E749C;border-radius: 5px;background: rgb(242, 242, 242, 150); background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #FFFFFD, stop: 0.3 #96ADB2);min-width: 80px;}
        QPushButton:checked {font-weight: bold;color: cyan;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #6F7E81, stop: 0.3 #DCF6FC);border-color: cyan; border-width: 3px;}
        QPushButton:flat {border: none;}
        QPushButton:default { border-color: cyan;}
        

        to so ...

        QPushButton {font-weight: bold;color: black; border: 3px solid #5E749C;border-radius: 5px;background: rgb(242, 242, 242, 150); background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #FFFFFD, stop: 0.3 #96ADB2);min-width: 80px;}
        QPushButton:checked {font-weight: bold;color: blue;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #6F7E81, stop: 0.3 #DCF6FC);border-color: cyan; border-width: 3px;}
        QPushButton:flat {border: none;}
        QPushButton:default { border-color: cyan;}
        

        and then to the color problem is solved (obviusly the online stylesheet code is the same).

        In reality, your reply has also solved the problem of knowing how to use the prefix "tr" .... but I would not know how to use it on qt creator ... is it possible?

        check "translatable" option is the solution?

        regards
        Giorgio

        bkt

        J.HilkJ 1 Reply Last reply
        0
        • gfxxG gfxx

          @mostefa very thanks for your reply .... this morning a fresh mind, I realized that they have already in my pocket answer ... in fact believed that the property "color" would change border color too .... but it is not true, the property change only text color .... so I change my code of QtCreator from so ....

          QPushButton {font-weight: bold;color: #5E749C; border: 3px solid #5E749C;border-radius: 5px;background: rgb(242, 242, 242, 150); background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #FFFFFD, stop: 0.3 #96ADB2);min-width: 80px;}
          QPushButton:checked {font-weight: bold;color: cyan;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #6F7E81, stop: 0.3 #DCF6FC);border-color: cyan; border-width: 3px;}
          QPushButton:flat {border: none;}
          QPushButton:default { border-color: cyan;}
          

          to so ...

          QPushButton {font-weight: bold;color: black; border: 3px solid #5E749C;border-radius: 5px;background: rgb(242, 242, 242, 150); background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #FFFFFD, stop: 0.3 #96ADB2);min-width: 80px;}
          QPushButton:checked {font-weight: bold;color: blue;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #6F7E81, stop: 0.3 #DCF6FC);border-color: cyan; border-width: 3px;}
          QPushButton:flat {border: none;}
          QPushButton:default { border-color: cyan;}
          

          and then to the color problem is solved (obviusly the online stylesheet code is the same).

          In reality, your reply has also solved the problem of knowing how to use the prefix "tr" .... but I would not know how to use it on qt creator ... is it possible?

          check "translatable" option is the solution?

          regards
          Giorgio

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @gfxx said in QPushbutton add text over button without using stylesheet & image:

          In reality, your reply has also solved the problem of knowing how to use the prefix "tr" .... but I would not know how to use it on qt creator ... is it possible?

          check "translatable" option is the solution?

          regards
          Giorgio

          Yes it is, In the creator you can expand the text-property. Doing that you'll find the chechbox translation - name may vary, my Creator is not in english - It is per default set to checked.

          running lupdate will fetch all texts in the creator with the checkbox set and extract them for the lunquist to be translated.

          You can translate the text, after successfully loading a translation, by calling ui->retranslateUi(this);

          This will change automatically all texts set in the creator, marked for translation.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mostefa
            wrote on last edited by
            #5

            In addition to what @J-Hilk said

            I think that all widgets that contains text in qt designer are translatable by default,

            And you can change it if you want

            alt text

            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