Commit b9ddedd5 authored by captainwong's avatar captainwong

jlibqt/CheckBtn

parent 0c479ac8
#include "CheckBtn.h"
#include <qevent.h>
#include "../QtUtils.h"
#include "../QtStylesheet.h"
using namespace jlib::qt;
CheckBtn::CheckBtn(QWidget *parent, int tag)
: QLabel(parent)
, tag_(tag)
{
setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
}
CheckBtn::~CheckBtn()
{
}
void CheckBtn::set_font_size(int fsz)
{
font_sz_ = fsz;
refresh();
}
void CheckBtn::refresh()
{
if (pressed_) {
setStyleSheet(build_style(Qt::darkGray, font_sz_));
} else if (hover_) {
setStyleSheet(build_style(Qt::lightGray, font_sz_));
} else if (checked_) {
setStyleSheet(build_style(Qt::white, font_sz_));
} else {
setStyleSheet(build_style(Qt::darkGray, font_sz_));
}
adjustSize();
update();
}
void CheckBtn::set_check(bool check)
{
MYQDEBUG << check;
checked_ = check;
refresh();
}
void CheckBtn::enterEvent(QEvent * e)
{
setCursor(QCursor(Qt::PointingHandCursor));
hover_ = true;
refresh();
emit sig_focus_on(tag_, true);
}
void CheckBtn::leaveEvent(QEvent * e)
{
setCursor(QCursor(Qt::ArrowCursor));
hover_ = false;
refresh();
}
void CheckBtn::mousePressEvent(QMouseEvent * e)
{
if (e->button() == Qt::LeftButton) {
pressed_ = true;
refresh();
}
}
void CheckBtn::mouseReleaseEvent(QMouseEvent * e)
{
if (pressed_ && e->button() == Qt::LeftButton) {
pressed_ = false;
checked_ = !checked_;
refresh();
MYQDEBUG << tag_ << checked_;
emit sig_check(tag_, checked_);
}
}
#pragma once
#include <QLabel>
class CheckBtn : public QLabel
{
Q_OBJECT
public:
CheckBtn(QWidget *parent, int tag = -1);
~CheckBtn();
void set_font_size(int fsz);
void refresh();
void set_check(bool check = true);
bool is_check() const { return checked_; }
void set_tag(int tag) { tag_ = tag; }
int tag() const { return tag_; }
protected:
virtual void enterEvent(QEvent* e) override;
virtual void leaveEvent(QEvent* e) override;
virtual void mousePressEvent(QMouseEvent* e) override;
virtual void mouseReleaseEvent(QMouseEvent* e) override;
signals:
void sig_check(int tag, bool is_check);
void sig_focus_on(int tag, bool on);
private:
bool pressed_ = false;
bool hover_ = false;
bool checked_ = false;
int tag_ = -1;
int font_sz_ = 18;
};
......@@ -95,12 +95,14 @@
<ClCompile Include="qt.cpp" />
<ClCompile Include="View\BaseScrollView.cpp" />
<ClCompile Include="View\BgColorBtn.cpp" />
<ClCompile Include="View\CheckBtn.cpp" />
<ClCompile Include="View\IconBtn.cpp" />
<ClCompile Include="View\PageTitle.cpp" />
<ClCompile Include="View\RndButton.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<QtMoc Include="View\CheckBtn.h" />
<QtMoc Include="View\BaseScrollView.h" />
<QtMoc Include="View\RndButton.h" />
<QtMoc Include="View\PageTitle.h" />
......
......@@ -48,6 +48,9 @@
<ClCompile Include="View\BaseScrollView.cpp">
<Filter>View</Filter>
</ClCompile>
<ClCompile Include="View\CheckBtn.cpp">
<Filter>View</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="qt.h">
......@@ -102,5 +105,8 @@
<QtMoc Include="View\BaseScrollView.h">
<Filter>View</Filter>
</QtMoc>
<QtMoc Include="View\CheckBtn.h">
<Filter>View</Filter>
</QtMoc>
</ItemGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment