如何使用PyQt5实现自定义标题栏和窗口框架?

时间:2016-10-14 05:26:28

标签: python python-3.x pyqt pyqt5 qt-designer

有没有办法可以使用Qt Designer设计Frame和Title Bar?

如果是这样,我如何才能使用现有代码?

以下是现有代码:

import sys

from PyQt5 import QtCore, uic
from PyQt5.QtWidgets import QApplication, QDialog


class Actions(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)

        self.ui = uic.loadUi("console_gui.ui")
        self.ui.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.ui.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Actions()
    sys.exit(app.exec_())

如果没有,是否有链接到成功使用FramelessWindowHint的开源PyQt5项目?

PyQt5没有关于此任何内容的任何文档。它们只链接到C ++版本。

1 个答案:

答案 0 :(得分:0)

您可以使用QPushButtons和QHBoxLayout。

class Actions(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)


        self.resize(500, 600)

        # 
        self.otherFrame = QFrame(self)

        # There are two Buttons and using them custom title bar.
        # We could give them string or pictures.
        self.oneButton = QPushButton('one')
        self.twoButton = QPushButton('two')

        # We must be give them some functions.
        self.oneButton.clicked.connect(self.oneClicked)
        self.twoButton.clicked.connect(self.twoClicked)

        # And some layouts.
        # This is a layout of horizontal.
        # 
        self.layouts = QHBoxLayout()
        # This is a layout of vertical.
        self.layouts2 = QVBoxLayout()

        self.layouts.addWidget(self.oneButton)
        self.layouts.addWidget(self.twoButton)
        self.layouts.addStretch(1)

        self.layouts2.addLayout(self.layouts)
        self.layouts2.addWidget(self.otherFrame)


        self.setLayout(self.layouts2)

        self.show()

    def oneClicked(self):
        print(1)

    def twoClicked(self):
        print(2)



    def mousePressEvent(self, event):

        if event.buttons() == Qt.LeftButton:
            self.m_drag = True
            self.m_DragPosition = event.globalPos()-self.pos()
            event.accept()

    def mouseMoveEvent(self, event):
        try:
            if event.buttons() and Qt.LeftButton:
                self.move(event.globalPos()-self.m_DragPosition)
                event.accept()
        except AttributeError:
            pass

    def mouseReleaseEvent(self, event):
        self.m_drag = False

喜欢这个。

enter image description here