Skip to main content

Modify Styles

Jia-YinLess than 1 minutecomm

The styles of the buttons above are set individually in QtDesigner. If you observe carefully, you will notice that the height of ICRT is slightly smaller, this is because the heights of English characters and Chinese characters are different. We can set the height of the minimumSize and maximumSize of each button to the same value, such as 36, so that the height of each button will be consistent.

Additionally, we can also set styles in the program. Below we modify the main program and add the following code to set the button styles:

  1. Modify the key event connection function in __init__ and add the key style setting function:

    self.bICRT.clicked.connect(lambda: self.setFreq(100.7, self.bICRT))
    self.bFengCheng.clicked.connect(lambda: self.setFreq(98.7, self.bFengCheng))
    self.bGuoJi.clicked.connect(lambda: self.setFreq(101.1, self.bGuoJi))
    self.setButtonsStyle()
    
  2. Add the following methods in the class:

    def setButtonsStyle(self):
        for button in [self.bICRT, self.bFengCheng, self.bGuoJi, self.bClose]:
            self.setButtonInActive(button)
    
    def setButtonActive(self, button):
        button.setStyleSheet("""
            QPushButton {
                background-color: orange;
                color: black;
                border: none;
                padding: 10px;
                border-radius: 12px;
            }
            QPushButton:hover {
                background-color: red;
            }
        """)
    
    def setButtonInActive(self, button):
        button.setStyleSheet("""
            QPushButton {
                background-color: #64B5F6;
                color: white;
                border: none;
                padding: 10px;
                border-radius: 12px;
            }
            QPushButton:hover {
                background-color: red;
            }
        """)
    
  3. Then modify the setFreq method, add a new parameter, and add the code to set styles:

    def setFreq(self, freq, button):
        self.grc.analog_sig_source_x_0.set_frequency(100e6-freq*1e6)
        for but in [self.bICRT, self.bFengCheng, self.bGuoJi]:
            if but == button: self.setButtonActive(but)
            else: self.setButtonInActive(but)
    

The final execution result is as shown in the image below:

練習 4

Based on the above instructions, design a radio receiver that you like.