TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttktabbutton.cpp
Go to the documentation of this file.
1 #include "ttktabbutton.h"
2 
3 #include <QHBoxLayout>
4 
5 #define TAB_BUTTON_ON "QPushButton{ border-radius:0px; background-color:#158FE1; color:white; }"
6 #define TAB_BUTTON_OFF "QPushButton{ border-radius:0px; background-color:#BFBFBF; color:white; }"
7 
8 TTKTabButton::TTKTabButton(QWidget *parent)
9  : QWidget(parent),
10  m_currentIndex(0)
11 {
12  QHBoxLayout *layout = new QHBoxLayout(this);
13  layout->setContentsMargins(0, 0, 0, 0);
14  layout->setSpacing(0);
15  setLayout(layout);
16 
17  m_buttonGroup = new QButtonGroup(this);
19 }
20 
22 {
23  delete m_buttonGroup;
24 }
25 
26 void TTKTabButton::addButtons(const QStringList &titles)
27 {
28  int index = count();
29  QHBoxLayout *ly = TTKObjectCast(QHBoxLayout*, layout());
30  if(ly->count() > 1)
31  {
32  ly->removeItem(ly->itemAt(ly->count() - 1));
33  }
34 
35  for(const QString &v : qAsConst(titles))
36  {
37  QPushButton *button = new QPushButton(v, this);
38  button->setFixedSize(100, 30);
39  button->setStyleSheet(TAB_BUTTON_OFF);
40  button->setCursor(QCursor(Qt::PointingHandCursor));
41 #ifdef Q_OS_UNIX
42  button->setFocusPolicy(Qt::NoFocus);
43 #endif
44  ly->addWidget(button);
45  m_buttonGroup->addButton(button, index++);
46  }
47 
48  ly->addStretch(1);
49  buttonClicked(0);
50 }
51 
53 {
54  for(QAbstractButton *button : m_buttonGroup->buttons())
55  {
56  button->setEnabled(enabled);
57  }
58 }
59 
61 {
62  for(int i = 0; i < count(); ++i)
63  {
64  m_buttonGroup->button(i)->setStyleSheet(i == index ? TAB_BUTTON_ON : TAB_BUTTON_OFF);
65  }
66 
67  m_currentIndex = index;
68  Q_EMIT clicked(index);
69 }
void setButtonEnabled(bool enabled)
TTKTabButton(QWidget *parent=nullptr)
Definition: ttktabbutton.cpp:8
void addButtons(const QStringList &titles)
int m_currentIndex
Definition: ttktabbutton.h:74
#define TAB_BUTTON_OFF
Definition: ttktabbutton.cpp:6
void clicked(int index)
#define qAsConst
Definition: ttkqtglobal.h:53
void buttonClicked(int index)
#define TTK_SLOT
Definition: ttkqtglobal.h:165
#define QtButtonGroupConnect(p, q, f, s)
Button group connect.
Definition: ttkqtcompat.h:101
#define TAB_BUTTON_ON
Definition: ttktabbutton.cpp:5
QButtonGroup * m_buttonGroup
Definition: ttktabbutton.h:75
int count() const
Definition: ttktabbutton.h:59
#define TTKObjectCast(x, y)
Definition: ttkqtglobal.h:60