简体   繁体   中英

How to achieve no gap between QLineEdit and QLineEdit in pyqt

In the case I added two QLineEdits with a horizontal layout. The style of QLineEdit is set to only the top and bottom borders. But there is a gap between the top and bottom borders of the two qlabels. How can I make the top and bottom borders of the two QLineEdits together without gaps?

import sys
from PyQt4 import QtGui


class Edit(QtGui.QLineEdit):
    def __init__(self, text):
        super(Edit, self).__init__()
        self.setText(text)
        self.setFixedSize(200, 40)
        self.setStyleSheet("""*{border-width: 1px;
                                    border-style: solid;
                                    border-color: red;
                                    border-left:none;
                                    border-right:none;
                                    margin:0px;
                                    padding:0px;
                                    }""")


class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        h = QtGui.QHBoxLayout()
        a = Edit("Hello World")
        b = Edit("Hello World")
        h.addWidget(a)
        h.addWidget(b)
        h.setContentsMargins(0, 0, 0, 0)
        h.addStretch(1)
        self.setLayout(h)
        self.setGeometry(100, 100, 500, 500)
        self.show()


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


if __name__ == '__main__':
    main()

If I'm understanding, you want to effectively make your Edit widgets appear as one with no gap between them?

Your QHBoxLayout has a default value for spacing that is used to add a gap between widgets. You're clearing the contentsMargins (the margin space used around the edge of the layout) but the between-widget spacing will still be there as dictated by your current style.

Add a setSpacing(0) to clear it:

def initUI(self):
    h = QtGui.QHBoxLayout()
    a = Edit("Hello World")
    b = Edit("Hello World")
    h.addWidget(a)
    h.addWidget(b)
    h.setContentsMargins(0, 0, 0, 0)
    h.setSpacing(0)
    h.addStretch(1)
    self.setLayout(h)
    self.setGeometry(100, 100, 500, 500)
    self.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM