QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgspercentagewidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspercentagewidget.h
3 -----------------
4 Date : January 2024
5 Copyright : (C) 2024 Nyall Dawson
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgspercentagewidget.h"
17#include "moc_qgspercentagewidget.cpp"
18#include "qgsdoublespinbox.h"
19#include "qgis.h"
20#include <QHBoxLayout>
21#include <QSlider>
22
24 : QWidget( parent )
25{
26 QHBoxLayout *layout = new QHBoxLayout();
27 layout->setContentsMargins( 0, 0, 0, 0 );
28 layout->setSpacing( 3 );
29 setLayout( layout );
30
31 mSlider = new QSlider();
32 mSlider->setMinimum( 0 );
33 mSlider->setMaximum( 1000 );
34 mSlider->setSingleStep( 10 );
35 mSlider->setPageStep( 100 );
36 mSlider->setValue( 1000 );
37 mSlider->setOrientation( Qt::Horizontal );
38 layout->addWidget( mSlider, 1 );
39
40 mSpinBox = new QgsDoubleSpinBox();
41 mSpinBox->setMinimum( 0.0 );
42 mSpinBox->setMaximum( 100.0 );
43 mSpinBox->setValue( 100.0 );
44 mSpinBox->setClearValue( 100.0 );
45 mSpinBox->setMinimumSize( QSize( 100, 0 ) );
46 mSpinBox->setDecimals( 1 );
47 mSpinBox->setSuffix( tr( " %" ) );
48 layout->addWidget( mSpinBox, 0 );
49
50 setFocusProxy( mSpinBox );
51
52 connect( mSlider, &QSlider::valueChanged, this, [ = ]( int value ) { mSpinBox->setValue( value / 10.0 ); } );
53 connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, [ = ]( double value ) { whileBlocking( mSlider )->setValue( static_cast< int >( std::lround( value * 10 ) ) ); } );
54 connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsPercentageWidget::spinChanged );
55}
56
58{
59 return mSpinBox->value() / 100.0;
60}
61
63{
64 mSpinBox->setValue( value * 100.0 );
65}
66
67void QgsPercentageWidget::spinChanged( double value )
68{
69 emit valueChanged( value / 100.0 );
70}
71
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
void setClearValue(double customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
QgsPercentageWidget(QWidget *parent=nullptr)
Constructor for QgsPercentageWidget.
void valueChanged(double value)
Emitted when the value is changed in the widget, where value is a factor which ranges from 0....
void setValue(double value)
Sets the current value to show in the widget, where value is a factor which ranges from 0....
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5862