qt: switch receive address selection to dropdown (legacy/p2sh-segwit/bech32/bech32m)

This commit is contained in:
2026-02-07 12:33:00 +01:00
parent 6ceef89956
commit aee82dfad1
2 changed files with 115 additions and 70 deletions

View File

@@ -189,28 +189,40 @@
</widget>
</item>
<item>
<widget class="QCheckBox" name="useBech32">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>1000</width>
<height>16777215</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="toolTip">
<string>Native segwit addresses (aka Bech32 or BIP-173) reduce your transaction fees later on and offer better protection against typos, but old wallets don't support them. When unchecked, an address compatible with older wallets will be created instead.</string>
</property>
<widget class="QLabel" name="addressTypeLabel">
<property name="text">
<string>Generate native segwit (Bech32) address</string>
<string>Address type:</string>
</property>
<property name="buddy">
<cstring>addressType</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="addressType">
<property name="toolTip">
<string>Select the address type to generate for this payment request.</string>
</property>
<item>
<property name="text">
<string>legacy</string>
</property>
</item>
<item>
<property name="text">
<string>p2sh-segwit</string>
</property>
</item>
<item>
<property name="text">
<string>segwit</string>
</property>
</item>
<item>
<property name="text">
<string>taproot</string>
</property>
</item>
</widget>
</item>
<item>
@@ -360,7 +372,7 @@
<tabstops>
<tabstop>reqLabel</tabstop>
<tabstop>reqAmount</tabstop>
<tabstop>useBech32</tabstop>
<tabstop>addressType</tabstop>
<tabstop>reqMessage</tabstop>
<tabstop>receiveButton</tabstop>
<tabstop>clearButton</tabstop>

View File

@@ -4,8 +4,8 @@
#include <wallet/wallet.h>
#include <qt/receivecoinsdialog.h>
#include <qt/forms/ui_receivecoinsdialog.h>
#include <qt/receivecoinsdialog.h>
#include <qt/addresstablemodel.h>
#include <qt/optionsmodel.h>
@@ -14,20 +14,69 @@
#include <qt/recentrequeststablemodel.h>
#include <qt/walletmodel.h>
#include <outputtype.h>
#include <QAction>
#include <QComboBox>
#include <QCursor>
#include <QMessageBox>
#include <QScrollBar>
#include <QTextDocument>
ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
QDialog(parent),
#include <array>
namespace {
const std::array<OutputType, 4> RECEIVE_ADDRESS_TYPES = {
OutputType::LEGACY,
OutputType::P2SH_SEGWIT,
OutputType::BECH32,
OutputType::BECH32M,
};
QString AddressTypeDisplayName(OutputType type)
{
switch (type) {
case OutputType::LEGACY: return QStringLiteral("legacy");
case OutputType::P2SH_SEGWIT: return QStringLiteral("p2sh-segwit");
case OutputType::BECH32: return QStringLiteral("segwit");
case OutputType::BECH32M: return QStringLiteral("traproot");
default: return QStringLiteral("legacy");
}
}
void InitAddressTypeComboBox(QComboBox* combo)
{
combo->clear();
for (const auto type : RECEIVE_ADDRESS_TYPES) {
combo->addItem(AddressTypeDisplayName(type), static_cast<int>(type));
}
}
void SetAddressType(QComboBox* combo, OutputType type, OutputType fallback = OutputType::BECH32)
{
int idx = combo->findData(static_cast<int>(type));
if (idx < 0) idx = combo->findData(static_cast<int>(fallback));
if (idx < 0) idx = 0;
combo->setCurrentIndex(idx);
}
OutputType GetSelectedAddressType(const QComboBox* combo, OutputType fallback)
{
const QVariant type_data = combo->currentData();
if (!type_data.isValid()) return fallback;
return static_cast<OutputType>(type_data.toInt());
}
} // namespace
ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle* _platformStyle, QWidget* parent) : QDialog(parent),
ui(new Ui::ReceiveCoinsDialog),
columnResizingFixer(nullptr),
model(nullptr),
platformStyle(_platformStyle)
{
ui->setupUi(this);
InitAddressTypeComboBox(ui->addressType);
SetAddressType(ui->addressType, OutputType::BECH32);
if (!_platformStyle->getImagesOnButtons()) {
ui->clearButton->setIcon(QIcon());
@@ -68,8 +117,7 @@ void ReceiveCoinsDialog::setModel(WalletModel *_model)
{
this->model = _model;
if(_model && _model->getOptionsModel())
{
if (_model && _model->getOptionsModel()) {
_model->getRecentRequestsTableModel()->sort(RecentRequestsTableModel::Date, Qt::DescendingOrder);
connect(_model->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &ReceiveCoinsDialog::updateDisplayUnit);
updateDisplayUnit();
@@ -92,11 +140,7 @@ void ReceiveCoinsDialog::setModel(WalletModel *_model)
// Last 2 columns are set by the columnResizingFixer, when the table geometry is ready.
columnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(tableView, AMOUNT_MINIMUM_COLUMN_WIDTH, DATE_COLUMN_WIDTH, this);
if (model->wallet().getDefaultAddressType() == OutputType::BECH32) {
ui->useBech32->setCheckState(Qt::Checked);
} else {
ui->useBech32->setCheckState(Qt::Unchecked);
}
SetAddressType(ui->addressType, model->wallet().getDefaultAddressType());
// Set the button to be enabled or disabled based on whether the wallet can give out new addresses.
ui->receiveButton->setEnabled(model->wallet().canGetAddresses());
@@ -133,8 +177,7 @@ void ReceiveCoinsDialog::accept()
void ReceiveCoinsDialog::updateDisplayUnit()
{
if(model && model->getOptionsModel())
{
if (model && model->getOptionsModel()) {
ui->reqAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
}
}
@@ -147,15 +190,7 @@ void ReceiveCoinsDialog::on_receiveButton_clicked()
QString address;
QString label = ui->reqLabel->text();
/* Generate new receiving address */
OutputType address_type;
if (ui->useBech32->isChecked()) {
address_type = OutputType::BECH32;
} else {
address_type = model->wallet().getDefaultAddressType();
if (address_type == OutputType::BECH32) {
address_type = OutputType::P2SH_SEGWIT;
}
}
const OutputType address_type = GetSelectedAddressType(ui->addressType, model->wallet().getDefaultAddressType());
address = model->getAddressTableModel()->addRow(AddressTableModel::Receive, label, "", address_type);
SendCoinsRecipient info(address, label,
ui->reqAmount->value(), ui->reqMessage->text());
@@ -221,11 +256,9 @@ void ReceiveCoinsDialog::resizeEvent(QResizeEvent *event)
void ReceiveCoinsDialog::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Return)
{
if (event->key() == Qt::Key_Return) {
// press return -> submit form
if (ui->reqLabel->hasFocus() || ui->reqAmount->hasFocus() || ui->reqMessage->hasFocus())
{
if (ui->reqLabel->hasFocus() || ui->reqAmount->hasFocus() || ui->reqMessage->hasFocus()) {
event->ignore();
on_receiveButton_clicked();
return;