UI: Add menu button (split button) class
When you assign a menu to a normal QPushButton, it becomes a button that only allows you to have a menu. This class lets you click the button and have a menu at the same time.
This commit is contained in:
parent
370761ba29
commit
b8547aa811
@ -117,6 +117,7 @@ set(obs_SOURCES
|
||||
window-remux.cpp
|
||||
properties-view.cpp
|
||||
focus-list.cpp
|
||||
menu-button.cpp
|
||||
double-slider.cpp
|
||||
volume-control.cpp
|
||||
adv-audio-control.cpp
|
||||
@ -159,6 +160,7 @@ set(obs_HEADERS
|
||||
display-helpers.hpp
|
||||
double-slider.hpp
|
||||
focus-list.hpp
|
||||
menu-button.hpp
|
||||
mute-checkbox.hpp
|
||||
volume-control.hpp
|
||||
adv-audio-control.hpp
|
||||
|
33
obs/menu-button.cpp
Normal file
33
obs/menu-button.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <QMenu>
|
||||
#include <QKeyEvent>
|
||||
#include <QMouseEvent>
|
||||
#include "menu-button.hpp"
|
||||
|
||||
void MenuButton::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
if (menu()) {
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Enter:
|
||||
case Qt::Key_Return:
|
||||
emit clicked();
|
||||
return;
|
||||
case Qt::Key_Down:
|
||||
showMenu();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QPushButton::keyPressEvent(event);
|
||||
}
|
||||
|
||||
void MenuButton::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (menu()) {
|
||||
if (width() - event->x() <= 15)
|
||||
showMenu();
|
||||
else
|
||||
setDown(true);
|
||||
} else {
|
||||
QPushButton::mousePressEvent(event);
|
||||
}
|
||||
}
|
23
obs/menu-button.hpp
Normal file
23
obs/menu-button.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <QPushButton>
|
||||
|
||||
class MenuButton : public QPushButton {
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
|
||||
public:
|
||||
explicit inline MenuButton(QWidget *parent = nullptr)
|
||||
: QPushButton(parent)
|
||||
{
|
||||
}
|
||||
|
||||
explicit inline MenuButton(const QString &text,
|
||||
QWidget *parent = nullptr)
|
||||
: QPushButton(text, parent)
|
||||
{
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user