Простенькая реализация строки ввода с кнопками
Этот виджет просто хранит кнопки
C++ (Qt)
class FadLineEdit : public QLineEdit
{
Q_OBJECT
public:
enum Side { None, Left, Right };
public:
FadLineEdit( QWidget * parent = 0 );
void addButton( QAbstractButton * button, Side side = Right );
QAbstractButton * removeButton( QAbstractButton * button, Side side = Right );
QList < QAbstractButton * > * leftSideButtons();
QList < QAbstractButton * > * rightSideButtons();
Side sideButton( QAbstractButton * button );
private:
void refreshTextMargins();
protected:
QList < QAbstractButton * > d_leftSideButtons;
QList < QAbstractButton * > d_rightSideButtons;
QHBoxLayout leftSideLayout;
QHBoxLayout rightSideLayout;
};
C++ (Qt)
/// PUBLIC
FadLineEdit::FadLineEdit( QWidget * parent )
: QLineEdit( parent )
{
QHBoxLayout * mainLayout = new QHBoxLayout();
setLayout( mainLayout );
leftSideLayout.addStretch();
rightSideLayout.addStretch();
leftSideLayout.setSpacing( 0 );
leftSideLayout.setContentsMargins( 0, 0, 0, 0 );
rightSideLayout.setSpacing( 0 );
rightSideLayout.setContentsMargins( 0, 0, 0, 0 );
mainLayout->setSpacing( 0 );
mainLayout->setMargin( 0 );
mainLayout->addLayout( &leftSideLayout );
mainLayout->addLayout( &rightSideLayout );
}
void FadLineEdit::addButton( QAbstractButton * button, Side side )
{
if ( side == Right )
{
d_rightSideButtons << button;
rightSideLayout.insertWidget( d_rightSideButtons.size(), button, 0, Qt::AlignRight | Qt::AlignCenter );
} else
{
d_leftSideButtons << button;
leftSideLayout.insertWidget( 0, button, 0, Qt::AlignLeft | Qt::AlignCenter );
}
refreshTextMargins();
}
QAbstractButton * FadLineEdit::removeButton( QAbstractButton * button, Side side )
{
if ( side == Right )
{
if ( d_rightSideButtons.removeOne( button ) )
{
rightSideLayout.removeWidget( button );
button->setParent( 0 );
} else
return 0;
} else
{
if ( d_rightSideButtons.removeOne( button ) )
{
leftSideLayout.removeWidget( button );
button->setParent( 0 );
} else
return 0;
}
refreshTextMargins();
return button;
}
QList < QAbstractButton * > * FadLineEdit::leftSideButtons()
{
return &d_leftSideButtons;
}
QList < QAbstractButton * > * FadLineEdit::rightSideButtons()
{
return &d_rightSideButtons;
}
FadLineEdit::Side FadLineEdit::sideButton( QAbstractButton * button )
{
Side side = None;
if ( d_leftSideButtons.indexOf( button ) != -1 )
side = Left;
else if ( d_rightSideButtons.indexOf( button ) != -1 )
side = Right;
return side;
}
/// PRIVATE
void FadLineEdit::refreshTextMargins()
{
QMargins margins = textMargins();
if ( !d_leftSideButtons.isEmpty() )
margins.setLeft( leftSideLayout.sizeHint().width() );
if ( !d_rightSideButtons.isEmpty() )
margins.setRight( rightSideLayout.sizeHint().width() );
setTextMargins( margins );
}