Skip to content

Commit 37809be

Browse files
committed
qt high dpi: fix some text fields
There are probably other DPI related issues though. closes #5471 closes #4597 closes #1927
1 parent e7304ce commit 37809be

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

electrum/gui/qt/amountedit.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from decimal import Decimal
44

55
from PyQt5.QtCore import pyqtSignal, Qt
6-
from PyQt5.QtGui import QPalette, QPainter
6+
from PyQt5.QtGui import QPalette, QPainter, QFontMetrics
77
from PyQt5.QtWidgets import (QLineEdit, QStyle, QStyleOptionFrame)
88

9+
from .util import char_width_in_lineedit
10+
911
from electrum.util import (format_satoshis_plain, decimal_point_to_base_unit_name,
1012
FEERATE_PRECISION, quantize_feerate)
1113

@@ -24,7 +26,7 @@ class AmountEdit(MyLineEdit):
2426
def __init__(self, base_unit, is_int=False, parent=None):
2527
QLineEdit.__init__(self, parent)
2628
# This seems sufficient for hundred-BTC amounts with 8 decimals
27-
self.setFixedWidth(140)
29+
self.setFixedWidth(16 * char_width_in_lineedit())
2830
self.base_unit = base_unit
2931
self.textChanged.connect(self.numbify)
3032
self.is_int = is_int

electrum/gui/qt/installwizard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from .seed_dialog import SeedLayout, KeysLayout
2424
from .network_dialog import NetworkChoiceLayout
2525
from .util import (MessageBoxMixin, Buttons, icon_path, ChoicesLayout, WWLabel,
26-
InfoButton)
26+
InfoButton, char_width_in_lineedit)
2727
from .password_dialog import PasswordLayout, PasswordLayoutForHW, PW_NEW
2828
from electrum.plugin import run_hook
2929

@@ -180,7 +180,7 @@ def select_storage(self, path, get_wallet_from_daemon) -> Tuple[str, Optional[Wa
180180
vbox.addWidget(self.msg_label)
181181
hbox2 = QHBoxLayout()
182182
self.pw_e = QLineEdit('', self)
183-
self.pw_e.setFixedWidth(150)
183+
self.pw_e.setFixedWidth(17 * char_width_in_lineedit())
184184
self.pw_e.setEchoMode(2)
185185
self.pw_label = QLabel(_('Password') + ':')
186186
hbox2.addWidget(self.pw_label)

electrum/gui/qt/main_window.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
OkButton, InfoButton, WWLabel, TaskThread, CancelButton,
8686
CloseButton, HelpButton, MessageBoxMixin, EnterButton, expiration_values,
8787
ButtonsLineEdit, CopyCloseButton, import_meta_gui, export_meta_gui,
88-
filename_field, address_field)
88+
filename_field, address_field, char_width_in_lineedit)
8989
from .installwizard import WIF_HELP_TEXT
9090
from .history_list import HistoryList, HistoryModel
9191
from .update_checker import UpdateCheck, UpdateCheckThread
@@ -1216,7 +1216,7 @@ def create_send_tab(self):
12161216
lambda: self.fiat_send_e.setFrozen(self.amount_e.isReadOnly()))
12171217

12181218
self.max_button = EnterButton(_("Max"), self.spend_max)
1219-
self.max_button.setFixedWidth(140)
1219+
self.max_button.setFixedWidth(self.amount_e.width())
12201220
self.max_button.setCheckable(True)
12211221
grid.addWidget(self.max_button, 4, 3)
12221222
hbox = QHBoxLayout()
@@ -1248,7 +1248,7 @@ def fee_cb(dyn, pos, fee_rate):
12481248
self.spend_max() if self.max_button.isChecked() else self.update_fee()
12491249

12501250
self.fee_slider = FeeSlider(self, self.config, fee_cb)
1251-
self.fee_slider.setFixedWidth(140)
1251+
self.fee_slider.setFixedWidth(self.amount_e.width())
12521252

12531253
def on_fee_or_feerate(edit_changed, editing_finished):
12541254
edit_other = self.feerate_e if edit_changed == self.fee_e else self.fee_e
@@ -1271,7 +1271,7 @@ def setAmount(self, byte_size):
12711271
self.size_e = TxSizeLabel()
12721272
self.size_e.setAlignment(Qt.AlignCenter)
12731273
self.size_e.setAmount(0)
1274-
self.size_e.setFixedWidth(140)
1274+
self.size_e.setFixedWidth(self.amount_e.width())
12751275
self.size_e.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
12761276

12771277
self.feerate_e = FeerateEdit(lambda: 0)
@@ -1293,7 +1293,7 @@ def feerounding_onclick():
12931293
self.show_message(title=_('Fee rounding'), msg=text)
12941294

12951295
self.feerounding_icon = QPushButton(read_QIcon('info.png'), '')
1296-
self.feerounding_icon.setFixedWidth(20)
1296+
self.feerounding_icon.setFixedWidth(round(2.2 * char_width_in_lineedit()))
12971297
self.feerounding_icon.setFlat(True)
12981298
self.feerounding_icon.clicked.connect(feerounding_onclick)
12991299
self.feerounding_icon.setVisible(False)
@@ -2198,9 +2198,9 @@ def new_contact_dialog(self):
21982198
vbox.addWidget(QLabel(_('New Contact') + ':'))
21992199
grid = QGridLayout()
22002200
line1 = QLineEdit()
2201-
line1.setFixedWidth(280)
2201+
line1.setFixedWidth(32 * char_width_in_lineedit())
22022202
line2 = QLineEdit()
2203-
line2.setFixedWidth(280)
2203+
line2.setFixedWidth(32 * char_width_in_lineedit())
22042204
grid.addWidget(QLabel(_("Address")), 1, 0)
22052205
grid.addWidget(line1, 1, 1)
22062206
grid.addWidget(QLabel(_("Name")), 2, 0)

electrum/gui/qt/network_dialog.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
from PyQt5.QtWidgets import (QTreeWidget, QTreeWidgetItem, QMenu, QGridLayout, QComboBox,
3333
QLineEdit, QDialog, QVBoxLayout, QHeaderView, QCheckBox,
3434
QTabWidget, QWidget, QLabel)
35+
from PyQt5.QtGui import QFontMetrics
3536

3637
from electrum.i18n import _
3738
from electrum import constants, blockchain
3839
from electrum.interface import serialize_server, deserialize_server
3940
from electrum.network import Network
4041
from electrum.logging import get_logger
4142

42-
from .util import Buttons, CloseButton, HelpButton, read_QIcon
43+
from .util import Buttons, CloseButton, HelpButton, read_QIcon, char_width_in_lineedit
4344

4445

4546
_logger = get_logger(__name__)
@@ -214,14 +215,17 @@ def __init__(self, network: Network, config, wizard=False):
214215
tabs.addTab(server_tab, _('Server'))
215216
tabs.addTab(proxy_tab, _('Proxy'))
216217

218+
fixed_width_hostname = 24 * char_width_in_lineedit()
219+
fixed_width_port = 6 * char_width_in_lineedit()
220+
217221
# server tab
218222
grid = QGridLayout(server_tab)
219223
grid.setSpacing(8)
220224

221225
self.server_host = QLineEdit()
222-
self.server_host.setFixedWidth(200)
226+
self.server_host.setFixedWidth(fixed_width_hostname)
223227
self.server_port = QLineEdit()
224-
self.server_port.setFixedWidth(60)
228+
self.server_port.setFixedWidth(fixed_width_port)
225229
self.autoconnect_cb = QCheckBox(_('Select server automatically'))
226230
self.autoconnect_cb.setEnabled(self.config.is_modifiable('auto_connect'))
227231

@@ -258,15 +262,15 @@ def __init__(self, network: Network, config, wizard=False):
258262
self.proxy_mode = QComboBox()
259263
self.proxy_mode.addItems(['SOCKS4', 'SOCKS5'])
260264
self.proxy_host = QLineEdit()
261-
self.proxy_host.setFixedWidth(200)
265+
self.proxy_host.setFixedWidth(fixed_width_hostname)
262266
self.proxy_port = QLineEdit()
263-
self.proxy_port.setFixedWidth(60)
267+
self.proxy_port.setFixedWidth(fixed_width_port)
264268
self.proxy_user = QLineEdit()
265269
self.proxy_user.setPlaceholderText(_("Proxy user"))
266270
self.proxy_password = QLineEdit()
267271
self.proxy_password.setPlaceholderText(_("Password"))
268272
self.proxy_password.setEchoMode(QLineEdit.Password)
269-
self.proxy_password.setFixedWidth(60)
273+
self.proxy_password.setFixedWidth(fixed_width_port)
270274

271275
self.proxy_mode.currentIndexChanged.connect(self.set_proxy)
272276
self.proxy_host.editingFinished.connect(self.set_proxy)

electrum/gui/qt/util.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import NamedTuple, Callable, Optional, TYPE_CHECKING, Union, List, Dict
1111

1212
from PyQt5.QtGui import (QFont, QColor, QCursor, QPixmap, QStandardItem,
13-
QPalette, QIcon)
13+
QPalette, QIcon, QFontMetrics)
1414
from PyQt5.QtCore import (Qt, QPersistentModelIndex, QModelIndex, pyqtSignal,
1515
QCoreApplication, QItemSelectionModel, QThread,
1616
QSortFilterProxyModel, QSize, QLocale)
@@ -127,7 +127,7 @@ def __init__(self, text):
127127
QPushButton.__init__(self, '?')
128128
self.help_text = text
129129
self.setFocusPolicy(Qt.NoFocus)
130-
self.setFixedWidth(20)
130+
self.setFixedWidth(round(2.2 * char_width_in_lineedit()))
131131
self.clicked.connect(self.onclick)
132132

133133
def onclick(self):
@@ -143,7 +143,7 @@ def __init__(self, text):
143143
QPushButton.__init__(self, 'Info')
144144
self.help_text = text
145145
self.setFocusPolicy(Qt.NoFocus)
146-
self.setFixedWidth(60)
146+
self.setFixedWidth(6 * char_width_in_lineedit())
147147
self.clicked.connect(self.onclick)
148148

149149
def onclick(self):
@@ -872,6 +872,12 @@ def __init__(self, parent, create_menu):
872872
self.header().setSectionResizeMode(1, sm)
873873

874874

875+
def char_width_in_lineedit() -> int:
876+
char_width = QFontMetrics(QLineEdit().font()).averageCharWidth()
877+
# 'averageCharWidth' seems to underestimate on Windows, hence 'max()'
878+
return max(9, char_width)
879+
880+
875881
if __name__ == "__main__":
876882
app = QApplication([])
877883
t = WaitingDialog(None, 'testing ...', lambda: [time.sleep(1)], lambda x: QMessageBox.information(None, 'done', "done"))

electrum/plugins/hw_wallet/qt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
from electrum.gui.qt.password_dialog import PasswordLayout, PW_PASSPHRASE
3434
from electrum.gui.qt.util import (read_QIcon, WWLabel, OkButton, WindowModalDialog,
35-
Buttons, CancelButton, TaskThread)
35+
Buttons, CancelButton, TaskThread, char_width_in_lineedit)
3636

3737
from electrum.i18n import _
3838
from electrum.logging import Logger
@@ -149,7 +149,7 @@ def word_dialog(self, msg):
149149
hbox = QHBoxLayout(dialog)
150150
hbox.addWidget(QLabel(msg))
151151
text = QLineEdit()
152-
text.setMaximumWidth(100)
152+
text.setMaximumWidth(12 * char_width_in_lineedit())
153153
text.returnPressed.connect(dialog.accept)
154154
hbox.addWidget(text)
155155
hbox.addStretch(1)

0 commit comments

Comments
 (0)