How to programmatically make a horizontal line in Qt

How to programmatically make a horizontal line in Qt

In Qt, you can programmatically create a horizontal line using a QFrame widget or by drawing it directly onto a custom widget. Here are two approaches:

1. Using a QFrame Widget:

You can use a QFrame widget with a frame shape set to QFrame.HLine to create a horizontal line.

import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame class HorizontalLineWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): line = QFrame(self) line.setFrameShape(QFrame.HLine) line.setGeometry(20, 50, 200, 2) # Adjust position and size as needed self.setGeometry(100, 100, 300, 200) self.setWindowTitle('Horizontal Line Example') self.show() def main(): app = QApplication(sys.argv) window = HorizontalLineWindow() sys.exit(app.exec_()) if __name__ == '__main__': main() 

In this example, we create a QFrame widget with the QFrame.HLine frame shape and set its position and size. You can adjust the setGeometry values as needed to position and size the horizontal line within your QMainWindow.

2. Drawing Directly on a Custom Widget:

You can also draw a horizontal line directly onto a custom widget by subclassing QWidget and implementing the paintEvent method.

import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget from PyQt5.QtGui import QPainter, QPen class HorizontalLineWidget(QWidget): def paintEvent(self, event): painter = QPainter(self) pen = QPen() pen.setColor(Qt.black) # Set line color pen.setWidth(2) # Set line width painter.setPen(pen) painter.drawLine(20, 20, 200, 20) # Adjust line coordinates as needed class HorizontalLineWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): line = HorizontalLineWidget(self) line.setGeometry(20, 50, 200, 2) # Adjust position and size as needed self.setGeometry(100, 100, 300, 200) self.setWindowTitle('Horizontal Line Example') self.show() def main(): app = QApplication(sys.argv) window = HorizontalLineWindow() sys.exit(app.exec_()) if __name__ == '__main__': main() 

In this example, we create a custom widget called HorizontalLineWidget that overrides the paintEvent method to draw a horizontal line using QPainter. You can customize the line's color, width, and position by adjusting the relevant parameters in the paintEvent method.

Examples

  1. How to create a horizontal line in Qt programmatically using Python?

    • Description: This query seeks to understand how to generate a horizontal line dynamically within a Qt application using Python.
    • Code:
      from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QVBoxLayout, QFrame import sys class LineExample(QMainWindow): def __init__(self): super().__init__() central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) # Adding horizontal line line = QFrame() line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) layout.addWidget(line) # Additional widgets can be added below the line label = QLabel("Below the horizontal line") layout.addWidget(label) if __name__ == '__main__': app = QApplication(sys.argv) ex = LineExample() ex.show() sys.exit(app.exec_()) 
  2. How to draw a horizontal line dynamically in a Qt window using C++?

    • Description: This query focuses on dynamically drawing a horizontal line within a Qt window using C++ code.
    • Code:
      #include <QMainWindow> #include <QFrame> #include <QVBoxLayout> #include <QLabel> class LineExample : public QMainWindow { public: LineExample(QWidget *parent = nullptr) : QMainWindow(parent) { QWidget *centralWidget = new QWidget(this); setCentralWidget(centralWidget); QVBoxLayout *layout = new QVBoxLayout(centralWidget); // Adding horizontal line QFrame *line = new QFrame(); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); layout->addWidget(line); // Additional widgets can be added below the line QLabel *label = new QLabel("Below the horizontal line"); layout->addWidget(label); } }; #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); LineExample ex; ex.show(); return app.exec(); } 
  3. How to programmatically insert a horizontal separator line in a Qt GUI application?

    • Description: This query aims to learn how to insert a horizontal separator line programmatically within a Qt GUI application.
    • Code:
      from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QFrame, QLabel import sys class LineExample(QMainWindow): def __init__(self): super().__init__() central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) # Adding horizontal line line = QFrame() line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) layout.addWidget(line) # Additional widgets can be added below the line label = QLabel("Below the horizontal line") layout.addWidget(label) if __name__ == '__main__': app = QApplication(sys.argv) ex = LineExample() ex.show() sys.exit(app.exec_()) 
  4. How to draw a horizontal line separator programmatically in Qt using C++?

    • Description: This query focuses on drawing a horizontal line separator programmatically within a Qt application using C++.
    • Code:
      #include <QMainWindow> #include <QFrame> #include <QVBoxLayout> #include <QLabel> class LineExample : public QMainWindow { public: LineExample(QWidget *parent = nullptr) : QMainWindow(parent) { QWidget *centralWidget = new QWidget(this); setCentralWidget(centralWidget); QVBoxLayout *layout = new QVBoxLayout(centralWidget); // Adding horizontal line QFrame *line = new QFrame(); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); layout->addWidget(line); // Additional widgets can be added below the line QLabel *label = new QLabel("Below the horizontal line"); layout->addWidget(label); } }; #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); LineExample ex; ex.show(); return app.exec(); } 
  5. How to programmatically add a horizontal line in a Qt window using Python?

    • Description: This query seeks to understand how to add a horizontal line dynamically within a Qt window using Python.
    • Code:
      from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QWidget, QFrame, QLabel import sys class LineExample(QMainWindow): def __init__(self): super().__init__() central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) # Adding horizontal line line = QFrame() line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) layout.addWidget(line) # Additional widgets can be added below the line label = QLabel("Below the horizontal line") layout.addWidget(label) if __name__ == '__main__': app = QApplication(sys.argv) ex = LineExample() ex.show() sys.exit(app.exec_()) 
  6. How to draw a horizontal separator line dynamically in a Qt application using C++?

    • Description: This query focuses on dynamically drawing a horizontal separator line within a Qt application using C++ code.
    • Code:
      #include <QMainWindow> #include <QFrame> #include <QVBoxLayout> #include <QLabel> class LineExample : public QMainWindow { public: LineExample(QWidget *parent = nullptr) : QMainWindow(parent) { QWidget *centralWidget = new QWidget(this); setCentralWidget(centralWidget); QVBoxLayout *layout = new QVBoxLayout(centralWidget); // Adding horizontal line QFrame *line = new QFrame(); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); layout->addWidget(line); // Additional widgets can be added below the line QLabel *label = new QLabel("Below the horizontal line"); layout->addWidget(label); } }; #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); LineExample ex; ex.show(); return app.exec(); } 
  7. How to programmatically insert a horizontal line separator in a Qt GUI application?

    • Description: This query aims to learn how to insert a horizontal line separator programmatically within a Qt GUI application.
    • Code:
      from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QFrame, QLabel import sys class LineExample(QMainWindow): def __init__(self): super().__init__() central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) # Adding horizontal line line = QFrame() line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) layout.addWidget(line) # Additional widgets can be added below the line label = QLabel("Below the horizontal line") layout.addWidget(label) if __name__ == '__main__': app = QApplication(sys.argv) ex = LineExample() ex.show() sys.exit(app.exec_()) 
  8. How to draw a horizontal line separator programmatically in Qt using C++?

    • Description: This query focuses on drawing a horizontal line separator programmatically within a Qt application using C++.
    • Code:
      #include <QMainWindow> #include <QFrame> #include <QVBoxLayout> #include <QLabel> class LineExample : public QMainWindow { public: LineExample(QWidget *parent = nullptr) : QMainWindow(parent) { QWidget *centralWidget = new QWidget(this); setCentralWidget(centralWidget); QVBoxLayout *layout = new QVBoxLayout(centralWidget); // Adding horizontal line QFrame *line = new QFrame(); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); layout->addWidget(line); // Additional widgets can be added below the line QLabel *label = new QLabel("Below the horizontal line"); layout->addWidget(label); } }; #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); LineExample ex; ex.show(); return app.exec(); } 
  9. How to programmatically add a horizontal line separator in a Qt window using Python?

    • Description: This query seeks to understand how to add a horizontal line separator dynamically within a Qt window using Python.
    • Code:
      from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QWidget, QFrame, QLabel import sys class LineExample(QMainWindow): def __init__(self): super().__init__() central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) # Adding horizontal line line = QFrame() line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) layout.addWidget(line) # Additional widgets can be added below the line label = QLabel("Below the horizontal line") layout.addWidget(label) if __name__ == '__main__': app = QApplication(sys.argv) ex = LineExample() ex.show() sys.exit(app.exec_()) 
  10. How to draw a horizontal separator line dynamically in a Qt application using C++?

    • Description: This query focuses on dynamically drawing a horizontal separator line within a Qt application using C++ code.
    • Code:
      #include <QMainWindow> #include <QFrame> #include <QVBoxLayout> #include <QLabel> class LineExample : public QMainWindow { public: LineExample(QWidget *parent = nullptr) : QMainWindow(parent) { QWidget *centralWidget = new QWidget(this); setCentralWidget(centralWidget); QVBoxLayout *layout = new QVBoxLayout(centralWidget); // Adding horizontal line QFrame *line = new QFrame(); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); layout->addWidget(line); // Additional widgets can be added below the line QLabel *label = new QLabel("Below the horizontal line"); layout->addWidget(label); } }; #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); LineExample ex; ex.show(); return app.exec(); } 

More Tags

git-status maven-failsafe-plugin angular-ng-class dynamo-local maven-nar-plugin laravel-echo prometheus-operator prism contrast operators

More Python Questions

More Investment Calculators

More Statistics Calculators

More Chemical thermodynamics Calculators

More Retirement Calculators