Qpushbuttos with more robust designs

Asked

Viewed 208 times

0

I’m studying Pyqt5, and I think it’s kind of weird how the Qpushbuttons are designed. you can put the button description in the bottom corner and make it change color when the cursor is on top of it ?

1 answer

0


It is possible yes! For styles, there is the function .setStyleSheet()

For a style on the button, you can write the code this way:

btn.setStyleSheet("font: 15pt Arial;margin: 1px;
border-color: #0c457e; border-style: outset;
border-radius: 1px;border-width: 1px;color: white;
background-color:
qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #2198c0, stop: 1 #0d5ca6);
}QPushButton:pressed
{ background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #0d5ca6, stop: 1 #2198c0);")

Thus, the button has a blue color with gradient when normal and a new style when pressed, with the final part of the code:

QPushButton:pressed
    { background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #0d5ca6, stop: 1 #2198c0);")

For the button to change only with the mouse above, just add a code as follows:

btn.setStyleSheet("QPushButton:hover:!pressed {border: 1px solid red;}")

Remembering that you can change the style as you wish, as in the model:

QPushButton:hover:!pressed
{
  border: 1px solid red;        # Nesse exemplo, as bordas ficam vermelhas
  color: white;                 # O texto fica branco     
  background-color: black       # O fundo do botão fica preto
}

For a text when the mouse is on top of the button, one should add a Tooltip. For this, just the code below:

btn.setToolTip("Aqui vai a descrição do botão")
  • Thank you so much for that full reply, it helps a lot

  • but what command I use to put the text inside the button at the bottom?

  • You can do this: button.setStyleSheet("text-align: bottom") and the text will be at the bottom of the button.

  • Thank you very much William

  • For nothing! Any questions, just comment here again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.