Skip to content

Commit

Permalink
Fix: LineEdit problem by ISSUE#126
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiramei committed Jul 29, 2024
1 parent 3fbac19 commit d2b4d04
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions gui/components/expand/featureSwitch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
import time
import traceback
from copy import deepcopy
from datetime import datetime
from functools import partial

from PyQt5.QtCore import Qt
from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QHeaderView, QVBoxLayout
from qfluentwidgets import CheckBox, TableWidget, LineEdit, PushButton, ComboBox, CaptionLabel, MessageBoxBase, \
SubtitleLabel
Expand All @@ -13,6 +14,26 @@
from gui.util.translator import baasTranslator as bt


class ClickFocusLineEdit(LineEdit):
"""
Custom LineEdit that does not focus unless clicked
This is to prevent the LineEdit from stealing focus
from the parent widget when the user strike a hover
on the LineEdit. This is useful when the LineEdit is
used in a TableWidget, and the user wants to click on
the parent widget to select a row.
"""
def __init__(self, parent=None):
super().__init__(parent)
self.setFocusPolicy(Qt.NoFocus)
self.installEventFilter(self)

def eventFilter(self, a0, a1):
if a1.type() == QEvent.MouseButtonPress:
self.setFocus()
return super().eventFilter(a0, a1)


class DetailSettingMessageBox(MessageBoxBase):
def __init__(self, detail_config: dict, all_label_list: list, parent=None, cs=None):
super().__init__(parent)
Expand Down Expand Up @@ -126,7 +147,8 @@ def _init_components(self, config_list):
cbx_layout.setContentsMargins(30, 0, 0, 0)
cbx_wrapper.setLayout(cbx_layout)
t_ccs = CaptionLabel(bt.tr('ConfigTranslation', self.labels[i]))
t_ncs = LineEdit(self)
t_ncs = ClickFocusLineEdit(self)
t_ncs.setClearButtonEnabled(True)
t_ncs.setText(str(datetime.fromtimestamp(self.next_ticks[i])).split('.')[0])
t_ncs.textChanged.connect(self._update_config)
t_cbx.stateChanged.connect(self._update_config)
Expand Down Expand Up @@ -182,7 +204,7 @@ def _sort(self):
self.tableView.setCellWidget(ind, 0, t_ccs)
self.qLabels.append(t_ccs)

t_ncs = LineEdit(self)
t_ncs = ClickFocusLineEdit(self)
t_ncs.setText(str(datetime.fromtimestamp(unit['next_tick'])).split('.')[0])
t_ncs.textChanged.connect(self._update_config)
self.tableView.setCellWidget(ind, 1, t_ncs)
Expand Down Expand Up @@ -268,7 +290,7 @@ def get_next_tick(self, time_str):
try:
return datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S').timestamp()
except Exception as e:
print(e)
traceback.print_exc()
return 0

def _refresh_time(self):
Expand Down

0 comments on commit d2b4d04

Please sign in to comment.