QPushbutton add text over button without using stylesheet & image



  • 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



  • 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



  • @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



  • @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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.