PyQt5 heading and subheading

28 views Asked by At

I'm trying to recreate this enter image description here in PyQt5. The problem is the space between the 2 labels (the heading and the subheading).

This is my current code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt


class NoResultsWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # "No results found" -> heading
        self.noResultsLabel = QLabel("No results found", self)
        self.noResultsLabel.setAlignment(Qt.AlignCenter)
        self.noResultsLabel.setStyleSheet("font-size: 24px;")

        # "Try different keywords or remove search filters" -> subheading
        self.instructionLabel = QLabel(
            "Try different keywords or remove search filters", self
        )
        self.instructionLabel.setAlignment(Qt.AlignCenter)
        self.instructionLabel.setStyleSheet("font-size: 14px; color: gray;")

        layout = QVBoxLayout()
        layout.addWidget(self.noResultsLabel)
        layout.addWidget(self.instructionLabel)
        layout.setSpacing(10)

        self.setLayout(layout)

        self.setWindowTitle("No Results")
        self.setGeometry(300, 300, 350, 200)
        self.show()


def main():
    app = QApplication(sys.argv)
    ex = NoResultsWidget()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

that outputs this: enter image description here

Does anyone know how can I get rid of the space between the two labels? Or have in mind an alternative way of doing this?

1

There are 1 answers

1
supr10 On

I think you can use the label.setGeometry function to put it where you want. I don't know if it also works in Python, so let me know if it works.

In C++ (maybe Python) we do:

YourLabel.setGeometry(posX,posY,height,width)