Resizing Behavior for Custom Title Bar

  c++, qt, window-resize

I’m trying to make a custom windowframe in Qt and I’m having trouble geting the titlebar to behave the way that I want it to. So what I want is for the title to be centered in the title bar and for the minimize, maximize, and X buttons to be on the layer above the title. (I am also considering adding a hambuger icon to the left side that will be on the same layer that opens a side panel.) But what I want is for this behavior:
here:
here

So this post helped me a lot (How To implementing multi layer frame or widget in Qt?). However, they use Python, and I’m using C++. Still, I understand the concept behind it I think. My layout looks like this: here:
here. And I’m adding a custom titlebar widget that contains two children: a qlabel (the title) and a QFrame widget that holds all three buttons.

This is what I have so far

windowframe_btns.cpp:

#include "windowframe_btns.h"

Windowframe_btns::Windowframe_btns(QFrame *parent) : QFrame(parent)
{
    setStyleSheet("QPushButton{"
                  "border: none;"
                  "background: none;"
                  "}"
                  "QPushButton:hover{"
                  "background: rgba(0, 0, 0, 20);"
                  "}");
    btn_close->setStyleSheet(":hover{"
                             "background: rgb(230, 93, 66);}");
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    setLayout(layout);
    btn_maximize->setIcon(QIcon(QPixmap(":/icons/full_screen.png")));
    btn_minimize->setIcon(QIcon(QPixmap(":/icons/minimize.png")));
    btn_close->setIcon(QIcon(QPixmap(":/icons/close.png")));
    layout->addWidget(btn_minimize);
    layout->addWidget(btn_maximize);
    layout->addWidget(btn_close);
}

titlebar.cpp:

#include "titlebar.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>

TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
{
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    title->setPixmap(QPixmap(":/icons/test_logo.png"));
    title->setGeometry(430,0,40,40);
    title->setAlignment(Qt::AlignCenter);
    title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    title -> setScaledContents(true);
    title->setParent(this);
    btns->setParent(this);
    btns->move(800,0);
}

So you can see that right now I placed everything manually. Whenever I try and use the titlebar widgets dimensions to do what I want it doesn’t seem to be filling up the frame I put it in because I don’t have a layout, but if I add a layout then I can’t layer like I want to.

This is what I get when running:
window

Source: Windows Questions C++

LEAVE A COMMENT