fix: 🐛 fix context menu

This commit is contained in:
ivaquero
2023-08-30 21:41:40 +08:00
parent eea300900a
commit fdc3ee27b0
4 changed files with 56 additions and 46 deletions
+4
View File
@@ -1,3 +1,7 @@
*
!*.*
!*/
### C ### ### C ###
# Prerequisites # Prerequisites
*.d *.d
BIN
View File
Binary file not shown.
+50 -43
View File
@@ -1,49 +1,52 @@
#include "clock.h" #include "clock.h"
Clock::Clock(QWidget *parent) Clock::Clock(QWidget *parent) : QWidget{parent}, pressed(false) {
: QWidget{parent} // geometry of Main Window (x, y, width, height)
, pressed(false) setGeometry(1100, 800, 180, 60);
{
// geometry of Main Window (x, y, width, height)
setGeometry(1100, 800, 180, 60);
// hide frame // hide frame
setWindowFlag(Qt::WindowStaysOnTopHint, true); setWindowFlag(Qt::WindowStaysOnTopHint, true);
setWindowFlag(Qt::FramelessWindowHint, true); setWindowFlag(Qt::FramelessWindowHint, true);
setAttribute(Qt::WA_TranslucentBackground); setAttribute(Qt::WA_TranslucentBackground);
// clock object // setFixedSize(this->width(), this->height());
QTimer *clock = new QTimer(this); setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint &
connect(clock, &QTimer::timeout, this, &Clock::showTime); ~Qt::WindowMinimizeButtonHint);
clock->start(1000); // update the clock per second
show();
settings = new QSettings(QString("%1/config.ini").arg(QCoreApplication::applicationDirPath()), // clock object
QSettings::IniFormat); QTimer *clock = new QTimer(this);
connect(clock, &QTimer::timeout, this, &Clock::showTime);
clock->start(1000); // update the clock per second
show();
font = QFont(); settings = new QSettings(
font.setFamily(settings->value("USER/FONT_FAMILY").toString()); QString("%1/config.ini").arg(QCoreApplication::applicationDirPath()),
font.setPointSize(settings->value("USER/FONT_SIZE").toInt()); QSettings::IniFormat);
// load settings font = QFont();
settings = new QSettings(QString("%1/config.ini").arg(QCoreApplication::applicationDirPath()), font.setFamily(settings->value("USER/FONT_FAMILY").toString());
QSettings::IniFormat); font.setPointSize(settings->value("USER/FONT_SIZE").toInt());
// font object // load settings
font = QFont(); settings = new QSettings(
font.setFamily(settings->value("DEFAULT/FONT_FAMILY").toString()); QString("%1/config.ini").arg(QCoreApplication::applicationDirPath()),
font_size_step = settings->value("USER/FONT_SIZE_STEP").toInt(); QSettings::IniFormat);
// label object // font object
label = new QLabel(); font = QFont();
label->setAlignment(Qt::AlignCenter); font.setFamily(settings->value("DEFAULT/FONT_FAMILY").toString());
font_size_step = settings->value("USER/FONT_SIZE_STEP").toInt();
// layout // label object
QVBoxLayout *layout = new QVBoxLayout(this); label = new QLabel();
layout->addWidget(label); label->setAlignment(Qt::AlignCenter);
setLayout(layout);
reloadUI(); // layout
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(label);
setLayout(layout);
reloadUI();
} }
void Clock::showTime() { void Clock::showTime() {
@@ -116,11 +119,13 @@ void Clock::mouseReleaseEvent(QMouseEvent *event) {
QWidget::mouseReleaseEvent(event); QWidget::mouseReleaseEvent(event);
} }
void Clock::mouseRightMenu(const QPoint &pos) { void Clock::contextMenuEvent(QContextMenuEvent *event) {
QMenu menu(this); QMenu *menu = new QMenu();
// menu->setFixedWidth(100);
// font size // font size
QMenu *font_size_menu = menu.addMenu("font size"); QMenu *font_size_menu = menu->addMenu("font size");
QAction *larger_action = new QAction("larger", this); QAction *larger_action = new QAction("larger", this);
QAction *smaller_action = new QAction("smaller", this); QAction *smaller_action = new QAction("smaller", this);
@@ -136,7 +141,7 @@ void Clock::mouseRightMenu(const QPoint &pos) {
"red", "yellow", "white"}; "red", "yellow", "white"};
// font color // font color
QMenu *font_color_menu = menu.addMenu("font color"); QMenu *font_color_menu = menu->addMenu("font color");
QList<QAction *> font_color_actions; QList<QAction *> font_color_actions;
for (const QString &color : ft_colors) { for (const QString &color : ft_colors) {
QAction *action = new QAction(color, this); QAction *action = new QAction(color, this);
@@ -146,7 +151,7 @@ void Clock::mouseRightMenu(const QPoint &pos) {
font_color_menu->addActions(font_color_actions); font_color_menu->addActions(font_color_actions);
// background color // background color
QMenu *bg_color_menu = menu.addMenu("bg color"); QMenu *bg_color_menu = menu->addMenu("bg color");
QList<QAction *> bg_color_actions; QList<QAction *> bg_color_actions;
for (const QString &color : bg_colors) { for (const QString &color : bg_colors) {
QAction *action = new QAction(color, this); QAction *action = new QAction(color, this);
@@ -158,12 +163,14 @@ void Clock::mouseRightMenu(const QPoint &pos) {
// reset // reset
QAction *reset_action = new QAction("reset", this); QAction *reset_action = new QAction("reset", this);
connect(reset_action, &QAction::triggered, this, &Clock::resetUI); connect(reset_action, &QAction::triggered, this, &Clock::resetUI);
menu.addAction(reset_action); menu->addAction(reset_action);
// quit // quit
QAction *quit_action = new QAction("quit", this); QAction *quit_action = new QAction("quit", this);
connect(quit_action, &QAction::triggered, this, &Clock::close); connect(quit_action, &QAction::triggered, this, &Clock::close);
menu.addAction(quit_action); menu->addAction(quit_action);
menu.exec(mapToGlobal(pos)); // menu.exec(mapToGlobal(pos));
menu->exec(event->globalPos());
delete menu;
} }
+2 -3
View File
@@ -28,11 +28,8 @@ public:
protected: protected:
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
// void mousePressEvent(QMouseEvent *event) ;
void mouseMoveEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);
// void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);
// void mouseReleaseEvent(QMouseEvent *event) ;
void mouseRightMenu(const QPoint &pos); void mouseRightMenu(const QPoint &pos);
signals: signals:
@@ -48,6 +45,8 @@ private:
int font_size_step; int font_size_step;
int font_size; int font_size;
QLabel *label; QLabel *label;
void contextMenuEvent(QContextMenuEvent *e);
}; };
#endif // CLOCK_H #endif // CLOCK_H