-
Notifications
You must be signed in to change notification settings - Fork 0
/
combotoolbutton.cpp
executable file
·43 lines (34 loc) · 1.09 KB
/
combotoolbutton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "combotoolbutton.h"
#include <QAction>
#include <QMenu>
#include <QKeyEvent>
ComboToolButton::ComboToolButton(QWidget *parent) : QToolButton(parent) {
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
setPopupMode(QToolButton::MenuButtonPopup);
setAutoRaise(true);
connect(this,SIGNAL(triggered(QAction *)),this,SLOT(onMenuSelected(QAction *)));
isFirstTime = true;
curr_action = NULL;
}
void ComboToolButton::onMenuSelected(QAction * action) {
setText(action->text());
curr_action = action;
}
void ComboToolButton::showEvent(QShowEvent * event) {
QToolButton::showEvent(event);
if (isFirstTime) {
isFirstTime = false;
QMenu * menu = this->menu();
if (menu != NULL) {
QList<QAction *> actions = menu->actions();
if (actions.count() > 0) onMenuSelected(actions.at(0));
}
}
}
QAction * ComboToolButton::currentAction() {
return curr_action;
}
void ComboToolButton::keyPressEvent(QKeyEvent * event) {
QToolButton::keyPressEvent(event);
if (event->key() == Qt::Key_Right) showMenu();
}