Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, January 2007
Doing a selectAll() when a QComboBox receives focus


Message 1 in thread

I want to select all the text in a combo box whenever it's activated.  I
don't care whether the user clicks on it or tabs into it--either way I want
to select the current text in it.

I tried to do this by inheriting from QComboBox and overriding
focusInEvent(), but I flubbed it.  It now selects the text when I tab into
the combo box, but not when I click on it.  The code below DOES call
selectAll() when I click on it, but nothing gets selected.

Can anybody show me my mistake?

Thanks,

Michael

Here's the relevant code:

#include <QComboBox>
class QFocusEvent;

class DirectoryComboBox : public QComboBox
{
    Q_OBJECT
public:
    DirectoryComboBox(QWidget *parent, const QString &text);
    ~DirectoryComboBox();
    void focusInEvent( QFocusEvent* event );
};

DirectoryComboBox::DirectoryComboBox(QWidget *parent, const QString &text) :
QComboBox(parent)
{
    setEditable(true);
    addItem(text);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    setInsertPolicy( QComboBox::InsertAlphabetically );
    setDuplicatesEnabled( false );
}

void DirectoryComboBox::focusInEvent( QFocusEvent* event )
{
    if( !(this->lineEdit()->hasSelectedText()) )
    {
        this->lineEdit()->selectAll();
    }
}

Message 2 in thread

Michael Hardt schrieb:
> I want to select all the text in a combo box whenever it's activated.  I
> don't care whether the user clicks on it or tabs into it--either way I
> want to select the current text in it.
> 
> I tried to do this by inheriting from QComboBox and overriding
> focusInEvent(), but I flubbed it.  It now selects the text when I tab
> into the combo box, but not when I click on it.  The code below DOES
> call selectAll() when I click on it, but nothing gets selected.

Just a wild guess, but maybe the QComboBox FIRST gets the focus in
event, so the text is indeed selected for a short time - UNTIL
internally (in the QComboBox) a mouse click event is processed (which
supposedly comers after the focus in event) which sets the cursor
position. Setting the cursor position then disables the selection again.

Maybe you also need to override the mouse events as to prohibit setting
the cursor position (but you might get other unwanted side-effects ;).
But maybe this would work:

// select all when user clicks into combo box
MyComboBox::mouseEvent (QMouseEvent *e)
{
  // let the base class process it as normal
  QComboBox::mouseEvent(e);

  // now select the entire text
  this->selectAll()
}

// select all when user gives focus to this widget with TAB
MyComboBox::focusInEvent (QFocusEvent *e)
{
  // let the base class process it as normal
  QComboBox::focusInEvent(e);

  // now select the entire text
  this->selectAll()
}

Not sure about the actual method/argument names, but you get the idea...


Cheers, Oliver

--
 [ signature omitted ] 

Message 3 in thread

Thanks for your tips, Oliver.  They pointed me the right way.

The important thing about a QComboBox is that it has a QLineEdit in it.
That QLineEdit receives its own events and parses them independently. I came
up with a 2-step solution.  It doesn't seem ideal to me, but I share it
below in case it's useful to anyone.

Thanks again,

Michael


1.  I was able to get the Combo Box to respond to getting the focus from a
Tab by inheriting my own class from QComboBox and overriding
focusInEvent().  Like this:

void MyComboBox::focusInEvent( QFocusEvent* event )
{
    QComboBox::focusInEvent( event );
    if( !(this->lineEdit()->hasSelectedText()) )
    {
        this->lineEdit()->selectAll();
    }
}

2.  I was also able to catch the QComboBox.QLineEdit's MouseRelease event by
installing an event filter on my form (QDialog).

//Declaration
class MyForm : public QDialog
{
   . . .
       bool eventFilter( QObject *target, QEvent *event );
   . . .
}

//Install the filter in MyForm's constructor:
    myComboBox->lineEdit()->installEventFilter( this );

//Implementation
bool MyForm::eventFilter( QObject *target, QEvent *event )
{
    if( target == myComboBox->lineEdit() && event->type() ==
QEvent::MouseButtonRelease )
    {
            if( !myComboBox->lineEdit()->hasSelectedText() )
            {
                myComboBox->lineEdit()->selectAll();
                return true;
            }
    }
    return false;
}

-----------------------------------------------------




On 1/23/07, Till Oliver Knoll <oliver.knoll@xxxxxxxxxxx> wrote:
>
> Michael Hardt schrieb:
> > I want to select all the text in a combo box whenever it's activated.  I
> > don't care whether the user clicks on it or tabs into it--either way I
> > want to select the current text in it.
> >
> > I tried to do this by inheriting from QComboBox and overriding
> > focusInEvent(), but I flubbed it.  It now selects the text when I tab
> > into the combo box, but not when I click on it.  The code below DOES
> > call selectAll() when I click on it, but nothing gets selected.
>
> Just a wild guess, but maybe the QComboBox FIRST gets the focus in
> event, so the text is indeed selected for a short time - UNTIL
> internally (in the QComboBox) a mouse click event is processed (which
> supposedly comers after the focus in event) which sets the cursor
> position. Setting the cursor position then disables the selection again.
>
> Maybe you also need to override the mouse events as to prohibit setting
> the cursor position (but you might get other unwanted side-effects ;).
> But maybe this would work:
>
> // select all when user clicks into combo box
> MyComboBox::mouseEvent (QMouseEvent *e)
> {
>   // let the base class process it as normal
>   QComboBox::mouseEvent(e);
>
>   // now select the entire text
>   this->selectAll()
> }
>
> // select all when user gives focus to this widget with TAB
> MyComboBox::focusInEvent (QFocusEvent *e)
> {
>   // let the base class process it as normal
>   QComboBox::focusInEvent(e);
>
>   // now select the entire text
>   this->selectAll()
> }
>
> Not sure about the actual method/argument names, but you get the idea...
>
>
> Cheers, Oliver
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>