Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically make a horizontal line in Qt

Tags:

python

qt

pyqt

I'm trying to figure out how to make a horizontal line in Qt. This is easy to create in Designer but I want to create one programmatically. I've done some googleing and looked at the xml in a ui file but haven't been able to figure anything out.

This is what the xml from the ui file looks like:

  <widget class="Line" name="line">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>110</y>
     <width>118</width>
     <height>3</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
like image 291
keegan3d Avatar asked Apr 15 '11 00:04

keegan3d


4 Answers

A horizontal or vertical line is just a QFrame with some properties set. In C++, the code that is generated to create a line looks like this:

line = new QFrame(w);
line->setObjectName(QString::fromUtf8("line"));
line->setGeometry(QRect(320, 150, 118, 3));
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
like image 118
shoosh Avatar answered Oct 23 '22 05:10

shoosh


Here's another solution using PySide:

from PySide.QtGui import QFrame


class QHLine(QFrame):
    def __init__(self):
        super(QHLine, self).__init__()
        self.setFrameShape(QFrame.HLine)
        self.setFrameShadow(QFrame.Sunken)


class QVLine(QFrame):
    def __init__(self):
        super(QVLine, self).__init__()
        self.setFrameShape(QFrame.VLine)
        self.setFrameShadow(QFrame.Sunken)

Which can then be used as (for example):

from PySide.QtGui import QApplication, QWidget, QGridLayout, QLabel, QComboBox


if __name__ == "__main__":
    app = QApplication([])
    widget = QWidget()
    layout = QGridLayout()

    layout.addWidget(QLabel("Test 1"), 0, 0, 1, 1)
    layout.addWidget(QComboBox(), 0, 1, 1, 1)
    layout.addWidget(QHLine(), 1, 0, 1, 2)
    layout.addWidget(QLabel("Test 2"), 2, 0, 1, 1)
    layout.addWidget(QComboBox(), 2, 1, 1, 1)

    widget.setLayout(layout)
    widget.show()
    app.exec_()

Which results in the following:

Example of QHLine on Windows 10

like image 42
Michael Leonard Avatar answered Oct 23 '22 07:10

Michael Leonard


Here is a solution using standard PyQt5 that I derived from shoosh's answer:

from PyQt5 import QtWidgets

class QHSeperationLine(QtWidgets.QFrame):
  '''
  a horizontal seperation line\n
  '''
  def __init__(self):
    super().__init__()
    self.setMinimumWidth(1)
    self.setFixedHeight(20)
    self.setFrameShape(QtWidgets.QFrame.HLine)
    self.setFrameShadow(QtWidgets.QFrame.Sunken)
    self.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
    return

class QVSeperationLine(QtWidgets.QFrame):
  '''
  a vertical seperation line\n
  '''
  def __init__(self):
    super().__init__()
    self.setFixedWidth(20)
    self.setMinimumHeight(1)
    self.setFrameShape(QtWidgets.QFrame.VLine)
    self.setFrameShadow(QtWidgets.QFrame.Sunken)
    self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
    return

And if you want to add it (for example to a grid):

seperator_vertical = seperation_lines.QVSeperationLine()
seperator_horizontal = seperation_lines.QHSeperationLine()

grid = QtWidgets.QGridLayout()

grid.addWidget(your_widget_left_from_vertical_seperator, 0, 0, 1, 1,)
grid.addWidget(seperator_vertical, 0, 1, 1, 1)
grid.addWidget(your_widget_right_from_vertical_seperator, 0, 2, 1, 1,)
grid.addWidget(seperator_horizontal, 1, 0, 1, 2)
grid.addWidget(your_widget_below_horizontal_spacer, 2, 0, 1, 2)

Make sure to never use alignment on the seperators, otherwise it will probably screw you over because they will not scale properly.

Just to show everything here is how to add it to your window:

import sys
if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    widget = QtWidgets.QWidget()
    widget.setLayout(grid)
    widget.show()
    sys.exit(app.exec())
like image 28
LegendaryCodingNoob Avatar answered Oct 23 '22 05:10

LegendaryCodingNoob


You can use this

self.line = QFrame()
self.line.setGeometry(QRect(60, 110, 751, 20))
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)

self.layout.addWidget(self.line)
like image 1
Abdo Abdelaziz Avatar answered Oct 23 '22 07:10

Abdo Abdelaziz