user-2147482637
user-2147482637

Reputation: 2163

Remove top bar of gui in qt

In either qt designer (preferably) or python, is there a way to remove the standard windows top bar and replace it with my own, or at least format it. This is similar to how the new versions of photoshop, maya, 3ds max, chrome etc.. look.

I found these two articles but besides for being in C++, and I need to work in python, they were never resolved.

http://qt-project.org/forums/viewthread/3331/ http://qt-project.org/forums/viewthread/3847/

What I did get from it is the FramelessWindowHint which does what I want, but as they mention in the article, it eliminates much of windows functionality, such as resizing. Is there a way to reintroduce the corner drag and resizing/closing options manually?

To be more specific, I already have the program designed in qt designer and used pyuic4 to make my python file. This is my main function that loads the gui:

global globalMoAnWindow
app = QtGui.QApplication(sys.argv)
app.setStyle(QtGui.QStyleFactory.create('plastique'))
globalMoAnWindow = MoAnWin()
globalMoAnWindow.setWindowFlags(QtCore.Qt.FramelessWindowHint)
globalMoAnWindow.showMaximized()
sys.exit(app.exec_())

For anyone looking to use python and drag the widget, i found this code:

def mousePressEvent(self, event):
    if event.button() == QtCore.Qt.LeftButton:
        self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
        event.accept()

def mouseMoveEvent(self, event):
    if event.buttons() == QtCore.Qt.LeftButton:
        self.move(event.globalPos() - self.dragPosition)
        event.accept()

That I put in my Class that deals with the main window

Upvotes: 1

Views: 2396

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

You can use QSizeGrip to add resize controls to your window. They act exactly like system behavior. You should create a layout and put QSizeGrip to each corner of your window.

widget = QWidget()
widget.setWindowFlags(Qt.FramelessWindowHint)
layout = QGridLayout(widget)
layout.setContentsMargins(0, 0, 0, 0)
sizeGrip1 = QSizeGrip(widget)
sizeGrip2 = QSizeGrip(widget)
sizeGrip3 = QSizeGrip(widget)
sizeGrip4 = QSizeGrip(widget)
layout.addWidget(sizeGrip1, 0, 0)
layout.addWidget(sizeGrip2, 0, 2)
layout.addWidget(sizeGrip3, 2, 2)
layout.addWidget(sizeGrip4, 2, 0)
label = QLabel("Contents")
layout.addWidget(label, 1, 1)

widget.show()

Variation for QMainWindow:

window = QMainWindow()
widget = QWidget()
window.setCentralWidget(widget)
window.setWindowFlags(Qt.FramelessWindowHint)
layout = QGridLayout(widget)
#...
window.show()

Implementing close button is simple. Just add a QPushButton or QToolButton to your layout and call QWidget::close in its onclick handler.

Dragging is more complicated as there is no standard widget to do this. A solution is described in this answer.

Upvotes: 4

Related Questions