PDA

View Full Version : how to draw custom titlebar for QMainWindow ... ?



pradeepreddyg95
17th August 2013, 07:19
i have attached image of tiltlebar. how can i implement that in Qmainwindow ...?



9419

karankumar1609
17th August 2013, 07:45
1 Hide the borders

See Qt::FramelessWindowHint and the other window flags

2 Have a customized title bar with contents(like the title bar of itunes 11 where the player control buttons are packed inside the title bar).

You need to draw one yourself. For instance you could make a custom widget and place it on the top or similar.

3 Retain the drag around default property of the title bar.

And you need to implement this yourself as well. This is actually quite easy -- just make your custom widget react to mouse events. Rough plan:

in the mousePressEvent handler accept the event remember the position of the mouse press
you will then get mouse move events (as you accepted the press), so override mouseMoveEvent and move the window (move, setPos) by the 2D vector (... QPoint) currentPos - savedPos

pradeepreddyg95
18th August 2013, 08:53
thnks karan i understood the concept but if i used Qt::FramelessWindowHint i cant move the widget ... if possible can you post few lines of code for better understanding ..

karankumar1609
19th August 2013, 04:31
try this sample code



...
...
void MyWindow::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
move(event->globalPos() - m_dragPosition);
event->accept();
}
}
void MyWindow::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
...
...


Hope this will help you..
CHEERS

d3fault
24th November 2015, 23:25
karankumar1609, thanks for such a detailed response.

I came across this thread wondering if there was any way to do it while still keeping the underlying platform's window decoration style. Any idea if that's possible?


And just dumping this related Qt feature/idea here for lack of a better place: We could add QWidget::addTitleBarWidget(QWidget* w, Position p = RightOfWindowTitle, Alignment a = Right) and then when m_TitleBarWidgets size goes from 0 to 1 then we setup the Qt::FramelessWindowHint, window moving, and listening to windowTitleChanged like karankumar1609 described; then, when m_TitleBarWidgets size goes back to 0 then undo those changes. But without being able to keep the underlying platform's window decoration style, this change/feature might be rejected :(

tetta
13th May 2017, 09:23
And how to make it so that when you move the window, it decreases? And how do I make it so that only the one button gets caught when I hold the left mouse button from above, and not around the widget?