-1

I'm trying to make a QSplitter with a QTreeView on the left hand side and on the right hand side, a QGraphicsView. This is super easy and I have this working.

Now my goal is to embed widgets in the scene of the QGraphicsView, using the QGraphicsProxyWidgetto create items for the QGraphicsScene that I assign to my QGraphicsView. This is also super easy and working. There is a small caveat related to how I want these widgets to look and feel. Upon a bit of research, it appears to be impossible based on what I've read and tested myself. I'll post the relevant code from what I currently have to further explain the situation.

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
    ui->setupUi(this);

    QPointer<QSplitter> splitter = new QSplitter;

    QPointer<QTreeView> tree = new QTreeView;
    splitter->addWidget(tree);

    QPointer<QVBoxLayout> layout = new QVBoxLayout;
    layout->addWidget(splitter);

    QPointer<QWidget> blueWidget = new QWidget;
    QPointer<QWidget> redWidget = new QWidget;
    blueWidget->setGeometry(250, 500, 250, 250);
    redWidget->setGeometry(500, 500, 250, 250);

    //This has no effect...
    blueWidget->setWindowFlags(Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
    redWidget->setWindowFlags( Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);

    blueWidget->setWindowTitle("Blue Widget");
    redWidget->setWindowTitle("Red Widget");

    blueWidget->setStyleSheet(QString("background-color: rgb(0,0,255)"));
    redWidget->setStyleSheet(QString("background-color: rgb(255,0,0)"));

    QPointer<QGraphicsProxyWidget> proxy1 = new QGraphicsProxyWidget(nullptr, redWidget->windowFlags());
    proxy1->setWidget(redWidget);
    proxy1->setWindowFlags(redWidget->windowFlags());

    QPointer<QGraphicsProxyWidget> proxy2 = new QGraphicsProxyWidget;
    proxy2->setWidget(blueWidget);
    proxy2->setWindowFlags(blueWidget->windowFlags());

    QPointer<QGraphicsScene> scene = new QGraphicsScene;
    scene->addItem(proxy1);
    scene->addItem(proxy2);
    scene->setSceneRect(0,0,1920,1080);

    QPointer<QGraphicsView> view = new QGraphicsView;
    view->setStyleSheet(QString("background-color: rgb(245,245,220)"));
    view->setScene(scene);
    view->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    view->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );

    splitter->addWidget(view);
    splitter->setStretchFactor( 0, 1 );
    splitter->setStretchFactor( 1, 3 );
    this->centralWidget()->setLayout(layout);
}

So given this code, I am attempting to give the QGraphicsItems in the QGraphicsScene some Qt::WindowFlags. However, it appears that I either have no bar (If I set no flags) or just a bar and a simple close button that is styled differently from the rest of the program (I suppose OS styling dependent). This title bar that is generated does not have a right click context menu pop up, like you would expect with regular QWidget. I'm just wondering if it's possible to get around this limitation that seems to be imposed upon by the OS. No amount of Qt::WindowFlags has the desired effect I've been looking for in any combination. I was hoping Qt::Widget would be all I need, but it does not work, nor does explicitly adding the Qt::WindowMinMaxButtonsHint.

I was thinking that maybe I can create a custom TitleBarWidget/TitleBarItem that takes a parent QGraphicsItem or QWidget that is in the QGraphicsScene and apply it to match the width of its parent at an offset. I could then implement the click and drag events of this TitleBarWidget to move its parent around. Doing this would allow me to add my own custom title positioning, font, or even coloring. Additionally, I could create my own custom styled buttons and connect their clicked signals to do the corresponding correct actions that a regular QWidget would perform.

If this seems complicated, the purpose of doing this is to maintain the property that QGraphicsItems NEVER leave the area of the QGraphicsScene's bounding rectangle in the QSplitter's QGraphicsView. I also have an idea for addressing minimizing and maximizing since there would be no native OS taskbar application to select if you minimize it.

Now If THAT doesn't work I'll have to create my own scene manager using top level QWidgets and connect every single QWidget's resize event to the emitted QSplitter's drag event... Which I really don't want to have to do...

Please, if I missed something basic in my first attempt's implementation that can get this to work with minimal hassle, it'd be appreciated if someone can point me in the right direction. If my second idea with a TitleBarWidget or TitleBarItem is probably my best bet with minimal subclassing and restructuring, then I'll just go with that. My fear is that will also not work, but I'm optimistic I can get that working if it doesn't run into the same issue as the first implementation.

2
  • Sharing all this information is very kind, however it is not clear how exactly someone could help.
    – scopchanov
    Oct 2, 2018 at 19:13
  • @scopchanov think I've managed to almost get it using this person's post: forum.qt.io/topic/66586/… I'll probably post my final result when I am done. Currently I've made a custom title bar with buttons, text title, and now I'm incorporating this person's post in to the title code. In the end I might have a reimplemented frame that contrasts with how Qt directly uses the OS.
    – Blanky
    Oct 2, 2018 at 20:07

1 Answer 1

1

After many days of trial and error I've accomplished a modified version of the previous users code in the link referenced earlier. This Titlebar class can be attached to any larger QWidget that would act as the area of the total window. You essentially need to create a QWidget with a QVBoxLayout. Then, create another QWidget that will hold the actual contents of the window and just set the whole outside parent QWidget as this Titlebar's parent. This will allows you to freely style the Titlebar and add any buttons, icons, styles that you wish, independent of your Operating System.

#ifndef TITLEBAR_H
#define TITLEBAR_H

#include "ViewerItem.h"
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QPointer>
#include <QStyle>
#include <QLabel>
#include <QPainter>
#include <QPen>
#include <QMouseEvent>
#include <QEvent>
#include <QApplication>
#include <QRubberBand>
#include <QEvent>
#include <QPoint>
#include <QResizeEvent>
#include <QObject>
#include <QDebug>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>

class ViewerItem;

class TitleBar : public QWidget{
    Q_OBJECT
public:
    TitleBar(QString title = "default", QWidget *parent = nullptr);
    void setWindowTitle(const QString& title);

    enum Edge{
        None = 0x0,
        Left = 0x1,
        Top = 0x2,
        Right = 0x4,
        Bottom = 0x8,
        TopLeft = 0x10,
        TopRight = 0x20,
        BottomLeft = 0x40,
        BottomRight = 0x80,
    };
    Q_ENUM(Edge);
    Q_DECLARE_FLAGS(Edges, Edge);

    void setBorderWidth(int);
    int borderWidth() const;

    ViewerItem *_parent = nullptr;
    QRubberBand *_rubberband = nullptr;
    bool _cursorchanged;
    bool _leftButtonPressed;
    Edges* _mousePress;
    Edges* _mouseMove;
    int _borderWidth;
    bool _dragStart = false;
    Qt::CursorShape _prevCursorShape = Qt::ArrowCursor;

protected slots:
    void paintEvent(QPaintEvent* event);
protected:
    bool eventFilter(QObject *o, QEvent *e) override;
    void mouseHover(QHoverEvent*);
    void mouseLeave(QHoverEvent *);
    void mousePress(QMouseEvent*);
    void mouseRelease(QMouseEvent*);
    void mouseMove(QMouseEvent*);
    void updateCursorShape(const QPoint &);
    void calculateCursorPosition(const QPoint &, const QRect &, Edges *);

private:
    QPointer<QStyle> style;

    QIcon closeIcon;
    QIcon maxIcon;
    QIcon minIcon;

    QPointer<QLabel> titleLabel;
    QPointer<QPushButton> minimizeButton;
    QPointer<QPushButton> maximizeButton;
    QPointer<QPushButton> closeButton;

    QPointer<QVBoxLayout> mainVLayout;
    QPointer<QHBoxLayout> mainHLayout;
    QPointer<QHBoxLayout> leftHLayout;
    QPointer<QHBoxLayout> rightHLayout;

    QPoint currentPos;

    QPointer<QGraphicsView> view;

};

Q_DECLARE_OPERATORS_FOR_FLAGS(TitleBar::Edges);

#endif // TITLEBAR_H

#include "Titlebar.h"

TitleBar::TitleBar(QString title, QWidget *parent) :
    QWidget(parent),
    _parent(qobject_cast<ViewerItem*>(parent)),
    _cursorchanged(false),
    _leftButtonPressed(false),
    _borderWidth(5){

        titleLabel = new QLabel;
        minimizeButton = new QPushButton;
        maximizeButton = new QPushButton;
        closeButton = new QPushButton;

        mainVLayout = new QVBoxLayout;
        mainHLayout = new QHBoxLayout;
        leftHLayout = new QHBoxLayout;
        rightHLayout = new QHBoxLayout;

        leftHLayout->addWidget(titleLabel);

        rightHLayout->addWidget(minimizeButton);
        rightHLayout->addWidget(maximizeButton);
        rightHLayout->addWidget(closeButton);

        mainHLayout->addLayout(leftHLayout);
        mainHLayout->addLayout(rightHLayout);

        mainVLayout->setAlignment(Qt::AlignTop);
        leftHLayout->setAlignment(Qt::AlignLeft);
        rightHLayout->setAlignment(Qt::AlignRight);
        mainVLayout->setContentsMargins(5,2.5,5,0);


        mainVLayout->addLayout(mainHLayout);
        setLayout(mainVLayout);

        setWindowTitle(title);

        style = qApp->style();
        closeIcon = style->standardIcon(QStyle::SP_TitleBarCloseButton);
        maxIcon = style->standardIcon(QStyle::SP_TitleBarMaxButton);
        minIcon = style->standardIcon(QStyle::SP_TitleBarMinButton);

        minimizeButton->setIcon(minIcon);
        maximizeButton->setIcon(maxIcon);
        closeButton->setIcon(closeIcon);

        minimizeButton->setMinimumWidth(30);
        maximizeButton->setMinimumWidth(30);
        closeButton->setMinimumWidth(30);

        titleLabel->setFixedHeight(20);

        minimizeButton->setFixedHeight(20);
        maximizeButton->setFixedHeight(20);
        closeButton->setFixedHeight(20);

        this->setFixedHeight(25);
        this->setMinimumWidth(90);

        //_parent->setMouseTracking(true);
        _parent->setWindowFlags(Qt::FramelessWindowHint);
        _parent->setAttribute(Qt::WA_Hover);
        _parent->installEventFilter(this);

        _rubberband = new QRubberBand(QRubberBand::Rectangle);

        _mousePress = new Edges(Edge::None);
        _mouseMove = new Edges(Edge::None);
}

void TitleBar::paintEvent(QPaintEvent *event){
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    QPainterPath path;
    path.addRect(QRectF(0, 0, width(), height()));
    QPen pen(Qt::black, 1);
    p.setPen(pen);
    p.fillPath(path, Qt::white);
    p.drawPath(path);

    QWidget::paintEvent(event);
}



void TitleBar::setWindowTitle(const QString& title){
    this->titleLabel->setText(title);
    QWidget::setWindowTitle(title);
}

bool TitleBar::eventFilter(QObject *o, QEvent*e) {
    if (e->type() == QEvent::MouseMove ||
        e->type() == QEvent::HoverMove ||
        e->type() == QEvent::Leave ||
        e->type() == QEvent::MouseButtonPress ||
        e->type() == QEvent::MouseButtonRelease) {


        switch (e->type()) {
            case QEvent::MouseMove:
                mouseMove(static_cast<QMouseEvent*>(e));
                return true;
                break;
            case QEvent::HoverMove:
                mouseHover(static_cast<QHoverEvent*>(e));
                return true;
                break;
            case QEvent::Leave: //We're actually leaving the widget after we go out of the geometry + borderWidth
                mouseLeave(static_cast<QHoverEvent*>(e));
                return true;
                break;
            case QEvent::MouseButtonPress:
                mousePress(static_cast<QMouseEvent*>(e));
                return true;
                break;
            case QEvent::MouseButtonRelease:
                mouseRelease(static_cast<QMouseEvent*>(e));
                return true;
                break;
            default:
                //e->accept();
                return true;
                break;
        }
    } else {
        return _parent->eventFilter(o, e);
    }

}

void TitleBar::mouseHover(QHoverEvent *e) {
    //if this parent object is in a container we need to mapToParent, not mapToGlobal like the original implementer's code
    updateCursorShape(_parent->mapToParent(e->pos()));

    //We need to make the bar transparent because it messes with the leave event of the widget "behind" it for the top, top left, and top right cursors
    //Only re-enable it when we want to for example, double click maximize or click the buttons
    //This will be determined if we're within the the bar's bounding rect minus the border value, in other words the cursor should be defaulted
    //This will prevent accidental drag events also
    if(this->geometry().marginsRemoved(QMargins(borderWidth(),borderWidth(),borderWidth(), 0)).contains(e->pos())){
        setAttribute(Qt::WA_TransparentForMouseEvents, false);
    }else{
        setAttribute(Qt::WA_TransparentForMouseEvents, true);
    }
    e->accept();
}

void TitleBar::mouseLeave(QHoverEvent *e) {
    if (!_leftButtonPressed) {
        *_mousePress = None;
        *_mouseMove = None;
        QApplication::restoreOverrideCursor();
        _cursorchanged = false;
        _prevCursorShape = Qt::ArrowCursor;
    }
    QWidget::leaveEvent(e);
}

void TitleBar::mousePress(QMouseEvent *e) {
    if(e->button() & Qt::LeftButton){
        _leftButtonPressed = true;
        currentPos = e->pos();
        calculateCursorPosition(e->pos(), _parent->geometry().marginsRemoved(QMargins(_borderWidth, _borderWidth, _borderWidth, _borderWidth)), _mousePress);
        if (!_mouseMove->testFlag(Edge::None)) {
            _rubberband->setGeometry(_parent->geometry());
        }

        //This area is ONLY on the title bar itself and determines repositioning of the parent
        //We're actually selecting the parent, not the bar, but it looks and feels like we are using the bar
        if(this->geometry().marginsRemoved(QMargins(borderWidth(),borderWidth() + 1,borderWidth(),0)).contains(e->pos())){
            _dragStart = true;
            _cursorchanged = false;
        }
    }
    e->accept();
}

void TitleBar::mouseRelease(QMouseEvent *e) {
    if (e->button() & Qt::LeftButton) {
        _leftButtonPressed = false;
        _dragStart = false;
    }
    e->accept();
}

void TitleBar::mouseMove(QMouseEvent *e) {
    if (_leftButtonPressed) {
        //For dragging the parent by its titlebar
        if (_dragStart) {
            QPoint diff = e->pos() - currentPos;
            _parent->move(_parent->pos() + diff);
            return;
        }

        //For determining how to resize if we have an edge
        if (!_mouseMove->testFlag(Edge::None)) {
            int left = _rubberband->geometry().left();
            int top = _rubberband->geometry().top();
            int right = _rubberband->geometry().right();
            int bottom = _rubberband->geometry().bottom();

            switch (*_mouseMove) {

                case Edge::Top:
                    top = e->pos().y();                 
                    if(_parent->height() - top <= _parent->minimumHeight()) top = 0;
                    _parent->resize(QSize(_parent->width(), _parent->height() - top));
                    _rubberband->resize(QSize(_parent->width(), _parent->height() - top));                    
                    _parent->move(QPoint(_parent->x(), _parent->y() + top));
                    _rubberband->move(QPoint(_parent->x(), _parent->y() + top));
                    break;

                case Edge::Bottom: //Good
                    bottom = e->pos().y();
                    if(bottom > _parent->height()){ //growing down
                        _parent->resize(QSize(_parent->width(), _parent->height() + (bottom - _parent->height())));
                        _rubberband->resize(QSize(_parent->width(), _parent->height() + (bottom - _parent->height())));
                    }else{ //shrinking up
                        _parent->resize(QSize(_parent->width(), _parent->height() - (_parent->height() - bottom)));
                        _rubberband->resize(QSize(_parent->width(), _parent->height() - (_parent->height() - bottom)));
                    }
                    break;

                case Edge::Left: //Good
                    left = e->pos().x();
                    if(left <= 0){ //Dragging left
                        if(_parent->width() - left <= _parent->minimumWidth()) left = 0;
                        _parent->resize(QSize(_parent->width() - left, _parent->height()));
                        _rubberband->resize(QSize(_parent->width() - left, _parent->height()));
                        _parent->move(QPoint(_parent->x() + left, _parent->y()));
                        _rubberband->move(QPoint(_parent->x() + left, _parent->y()));
                    }else{ //dragging right
                        if(_parent->width() - left <= _parent->minimumWidth()) left = 0;
                        _parent->resize(QSize(_parent->width() - left, _parent->height()));
                        _rubberband->resize(QSize(_parent->width() - left, _parent->height()));
                        _parent->move(QPoint(_parent->x() + left, _parent->y()));
                        _rubberband->move(QPoint(_parent->x() + left, _parent->y()));
                    }
                    break;

                case Edge::Right:
                    right = e->pos().x();
                    if(right >= 0){ //Dragging right
                        _parent->resize(QSize(right, _parent->height()));
                        _rubberband->resize(QSize(right, _parent->height()));
                        if(_parent->width() - right <= _parent->minimumWidth()) break;
                        _parent->move(QPoint(_parent->x() + (_parent->width() - right), _parent->y()));
                        _rubberband->move(QPoint(_parent->x() + (_parent->width() - right), _parent->y()));
                    }
                    break;

                case Edge::TopLeft:
                    top = e->pos().y();
                    left = e->pos().x();
                    if(_parent->height() - top <= _parent->minimumHeight()) top = 0;
                    if(_parent->width() - left <= _parent->minimumWidth()) left = 0;
                    _parent->resize(QSize(_parent->width() - left, _parent->height() - top));
                    _rubberband->resize(QSize(_parent->width() - left, _parent->height() - top));
                    _parent->move(QPoint(_parent->x() + left, _parent->y() + top));
                    _rubberband->move(QPoint(_parent->x() + left, _parent->y() + top));
                    break;

                case Edge::TopRight:
                    right = e->pos().x();
                    top = e->pos().y();
                    if(_parent->height() - top <= _parent->minimumHeight()) top = 0;
                    _parent->resize(QSize(_parent->width(), _parent->height() - top));
                    _rubberband->resize(QSize(_parent->width(), _parent->height() - top));
                    _parent->move(QPoint(_parent->x(), _parent->y() + top));
                    _rubberband->move(QPoint(_parent->x(), _parent->y() + top));
                    if(right >= 0){ //Dragging right
                        _parent->resize(QSize(right, _parent->height()));
                        _rubberband->resize(QSize(right, _parent->height()));
                        if(_parent->width() - right <= _parent->minimumWidth()) break;
                        _parent->move(QPoint(_parent->x() + (_parent->width() - right), _parent->y()));
                        _rubberband->move(QPoint(_parent->x() + (_parent->width() - right), _parent->y()));
                    }
                    break;
                case Edge::BottomLeft:
                    bottom = e->pos().y();
                    left = e->pos().x();
                    if(bottom > _parent->height()){ //growing down
                        _parent->resize(QSize(_parent->width(), _parent->height() + (bottom - _parent->height())));
                        _rubberband->resize(QSize(_parent->width(), _parent->height() + (bottom - _parent->height())));
                    }else{ //shrinking up
                        _parent->resize(QSize(_parent->width(), _parent->height() - (_parent->height() - bottom)));
                        _rubberband->resize(QSize(_parent->width(), _parent->height() - (_parent->height() - bottom)));
                    }
                    if(left <= 0){ //Dragging left
                        if(_parent->width() - left <= _parent->minimumWidth()) left = 0;
                        _parent->resize(QSize(_parent->width() - left, _parent->height()));
                        _rubberband->resize(QSize(_parent->width() - left, _parent->height()));
                        _parent->move(QPoint(_parent->x() + left, _parent->y()));
                        _rubberband->move(QPoint(_parent->x() + left, _parent->y()));
                    }else{ //dragging right
                        if(_parent->width() - left <= _parent->minimumWidth()) left = 0;
                        _parent->resize(QSize(_parent->width() - left, _parent->height()));
                        _rubberband->resize(QSize(_parent->width() - left, _parent->height()));
                        _parent->move(QPoint(_parent->x() + left, _parent->y()));
                        _rubberband->move(QPoint(_parent->x() + left, _parent->y()));
                    }
                    break;
                case Edge::BottomRight:
                    bottom = e->pos().y();
                    right = e->pos().x();
                    if(bottom > _parent->height()){ //growing down
                        _parent->resize(QSize(_parent->width(), _parent->height() + (bottom - _parent->height())));
                        _rubberband->resize(QSize(_parent->width(), _parent->height() + (bottom - _parent->height())));
                    }else{ //shrinking up
                        _parent->resize(QSize(_parent->width(), _parent->height() - (_parent->height() - bottom)));
                        _rubberband->resize(QSize(_parent->width(), _parent->height() - (_parent->height() - bottom)));
                    }
                    if(right >= 0){ //Dragging right
                        _parent->resize(QSize(right, _parent->height()));
                        _rubberband->resize(QSize(right, _parent->height()));
                        if(_parent->width() - right <= _parent->minimumWidth()) break;
                        _parent->move(QPoint(_parent->x() + (_parent->width() - right), _parent->y()));
                        _rubberband->move(QPoint(_parent->x() + (_parent->width() - right), _parent->y()));
                    }
                    break;
                default:
                    break;
            }
        }
    }
    e->accept();
}

void TitleBar::updateCursorShape(const QPoint &pos) {
    if (_parent->isFullScreen() || _parent->isMaximized()) {
        if (_cursorchanged) {
            QApplication::restoreOverrideCursor();
        }
        return;
    }
    if (!_leftButtonPressed) {
        calculateCursorPosition(pos, _parent->geometry(), _mouseMove);
        _cursorchanged = true;
        if (_mouseMove->testFlag(Edge::Top) || _mouseMove->testFlag(Edge::Bottom)) {
            if(_prevCursorShape != Qt::SizeVerCursor){
                QApplication::setOverrideCursor(Qt::SizeVerCursor);
            }
            _prevCursorShape = Qt::SizeVerCursor;
        } else if (_mouseMove->testFlag(Edge::Left) || _mouseMove->testFlag(Edge::Right)) {
            if(_prevCursorShape != Qt::SizeHorCursor){
                QApplication::setOverrideCursor(Qt::SizeHorCursor);
            }
            _prevCursorShape = Qt::SizeHorCursor;
        } else if (_mouseMove->testFlag(Edge::TopLeft) || _mouseMove->testFlag(Edge::BottomRight)) {
            if(_prevCursorShape != Qt::SizeFDiagCursor){
                QApplication::setOverrideCursor(Qt::SizeFDiagCursor);
            }
            _prevCursorShape = Qt::SizeFDiagCursor;
        } else if (_mouseMove->testFlag(Edge::TopRight) || _mouseMove->testFlag(Edge::BottomLeft)) {
            if(_prevCursorShape != Qt::SizeBDiagCursor){
                QApplication::setOverrideCursor(Qt::SizeBDiagCursor);
            }
            _prevCursorShape = Qt::SizeBDiagCursor;
        } else if (_cursorchanged && _mouseMove->testFlag(Edge::None)) {
            QApplication::restoreOverrideCursor();
            _cursorchanged = false;
            _prevCursorShape = Qt::ArrowCursor;
        }
    }
}

void TitleBar::calculateCursorPosition(const QPoint &pos, const QRect &framerect, Edges* _edge) {

    bool onLeft = pos.x() >= framerect.x() + _borderWidth && pos.x() <= framerect.x() + (_borderWidth * 2) &&
        pos.y() <= framerect.y() + framerect.height() - (_borderWidth * 4) && pos.y() >= framerect.y() + (_borderWidth * 4);

    bool onRight = pos.x() >= framerect.x() + framerect.width() - (_borderWidth * 2) && pos.x() <= framerect.x() + framerect.width() - (_borderWidth) &&
        pos.y() >= framerect.y() + (_borderWidth * 4) && pos.y() <= framerect.y() + framerect.height() - (_borderWidth * 4);

    bool onBottom = pos.x() >= framerect.x() + (_borderWidth * 4) && pos.x() <= framerect.x() + framerect.width() - (_borderWidth * 4) &&
        pos.y() >= framerect.y() + framerect.height() - (_borderWidth * 2) && pos.y() <= framerect.y() + framerect.height() - _borderWidth;

    bool onTop = pos.x() >= framerect.x() + (_borderWidth * 4) && pos.x() <= framerect.x() + framerect.width() - (_borderWidth  * 4) &&
        pos.y() >= framerect.y() + _borderWidth && pos.y() <= framerect.y() + (_borderWidth * 2);

    bool onBottomLeft = pos.x() <= framerect.x() + (_borderWidth * 3) && pos.x() >= framerect.x() + _borderWidth &&
        pos.y() <= framerect.y() + framerect.height() -_borderWidth && pos.y() >= framerect.y() + framerect.height() - (_borderWidth * 4);

    bool onBottomRight = pos.x() >= framerect.x() + framerect.width() - (_borderWidth * 4) && pos.x() <= framerect.x() + framerect.width() - _borderWidth &&
        pos.y() >= framerect.y() + framerect.height() - (_borderWidth * 4) && pos.y() <= framerect.y() + framerect.height() - _borderWidth;

    bool onTopRight = pos.x() >= framerect.x() + framerect.width() - (_borderWidth * 4) && pos.x() <= framerect.x() + framerect.width() - _borderWidth &&
        pos.y() >= framerect.y() + _borderWidth && pos.y() <= framerect.y() + (_borderWidth * 4);

    bool onTopLeft = pos.x() >= framerect.x() - _borderWidth && pos.x() <= framerect.x() + (_borderWidth * 4) &&
        pos.y() >= framerect.y() + _borderWidth && pos.y() <= framerect.y() + (_borderWidth * 4);

    if (onLeft) {
        *_edge = Left;
        //qDebug() << "Left";
    } else if (onRight) {
        *_edge = Right;
        //qDebug() << "Right";
    } else if (onBottom) {
        *_edge = Bottom;
        //qDebug() << "bottom";
    } else if (onTop) {
        *_edge = Top;
        //qDebug() << "top";
    } else if (onBottomLeft) {
        *_edge = BottomLeft;
        //qDebug() << "bottom left";
    } else if (onBottomRight) {
        *_edge = BottomRight;
        //qDebug() << "Bottom right";
    } else if (onTopRight) {
        *_edge = TopRight;
        //qDebug() << "Top right";
    } else if (onTopLeft) {
        *_edge = TopLeft;
        //qDebug() << "Top left";
    } else {
        *_edge = None;
        //qDebug() << "None";
    }

}

void TitleBar::setBorderWidth(int borderWidth) {
    _borderWidth = borderWidth;
}

int TitleBar::borderWidth() const {
    return _borderWidth;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.