【问题标题】:How does one implement a custom title bar and window frame using PyQt5? [duplicate]如何使用 PyQt5 实现自定义标题栏和窗口框架? [复制]
【发布时间】:2016-10-14 05:26:27
【问题描述】:

有没有办法使用 Qt Designer 来设计框架和标题栏?

如果是这样,我怎样才能使它与现有代码一起工作?

这是现有的代码:

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++ 版本。

【问题讨论】:

  • PS:我在链接问题中添加了 PyQt5 版本。

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


【解决方案1】:

你可以使用 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

像这样。

【讨论】:

  • 它似乎没有解决鼠标拖动事件等,但非常感谢。 :)
  • @Daegon Taven 我编辑了它。
猜你喜欢
  • 2017-10-29
  • 1970-01-01
  • 2014-07-05
  • 2012-08-18
  • 2013-09-22
  • 1970-01-01
  • 2010-09-11
相关资源
最近更新 更多