PDA

View Full Version : How to know whether a pushbutton is highlighted?



AmulyaT
4th April 2013, 21:28
Hi
Hi,
I have a list of 10 pushbuttons on the screens. I navigate through arrow keys.
I am trying to refresh the text on the pushbuttons once the last pushbutton on the screen is reached. For this I have written the below code.

void testApp::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Down && buttons[9]->hasFocus())
{

refresh();

}
}

But looks like there is something wrong in the if condition. Its not working. Can someone help me on this?

I am trying to do the refresh when the focus is on the last button.

Thanks.

d_stranz
4th April 2013, 23:30
But looks like there is something wrong in the if condition. Its not working.

How do you know it isn't working? Did you put a breakpoint on the line with the refresh() call? Or at least add a qDebug() statement inside the if clause to show it is being entered?

Any changes to the text on the buttons will not occur until after the keyPressEvent() is done and control has returned to the event loop so the buttons can receive their paintEvent() messages.

wysota
4th April 2013, 23:37
What exactly does refresh() do? Why do you need to call it? Why don't you just intercept the focus-in event of the button you want?

AmulyaT
5th April 2013, 05:53
Hi,
I didnt put any break points. Instead of refresh() I put the below code in the if condition to test. But the message box didnt pop up
void testApp::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Down && buttons[9]->hasFocus())
{

QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.exec();

}
}

When the condition is if(event->key() == Qt::Key_Down )
and press down key its working but when I try checking focus on the last button its not.
I set setFocusPolicy(Qt::StrongFocus); in the widget.

Please help .Thank you.

Added after 4 minutes:

Hi ,
In refresh(), I want to update the text of all the 10 pushbuttons. I want to call this function when the 10th button is reached and then down key is pressed. That is the case I tried to put in the if condition.

giblit
5th April 2013, 06:15
you should use ["code"] and [/"code"] btw, and if it works with out
buttons[9]->hasFocus() it seems to me like that button isn't getting focus did you try with out that condition? eg
if(buttons[9]->hasFocus()){
QMessageBox msg(QMessageBox::Information, "Document", "The document has been modified.");
msg.exec();
}

AmulyaT
5th April 2013, 07:13
Thanks. You are right. It is not getting the focus. But then when I use the arrow keys I see that it is going to that button..it is highlighted. Then which property do we need to use ? Or do we need to explicitly set something ?Can anybody suggest ?


Thanks.

wysota
5th April 2013, 07:32
Hi ,
In refresh(), I want to update the text of all the 10 pushbuttons. I want to call this function when the 10th button is reached and then down key is pressed. That is the case I tried to put in the if condition.

But why do you want to do that, it seems a very unusual thing to do.

BalaQT
5th April 2013, 07:51
1.
I am trying to refresh the text on the pushbuttons once the last pushbutton on the screen is reached.
As Wysota mentioned, Why you want that logic?

2.Did you tried what wysota said?

Why don't you just intercept the focus-in event of the button you want?

Key press event is a costly event, since it will trigger at every key press. But what you need is, do something on the last button focus. So just use focus-in event


Hope it helps,
Bala

AmulyaT
5th April 2013, 09:43
Oh is it ? Actually I am new to QT, I could think of only this solution .. The below is what I am trying to implement. Can you please suggest if you have a better way ..

I have a text file which contains audio/video/audio-video files data as below:
Program 1

PMT PID 0022 (34) Program streams:
PID 0020 -> H.264 Encoded Video
PID 0021 -> AAC Encoded Audio
Program 2

PMT PID 0031 (49) Program streams:
PID 002d -> AAC Encoded Audio
PID 002e -> H.264 Encoded Video
Program 3

PMT PID 0027 (39) Program streams:
PID 0026 -> AAC Encoded Audio


Now I need to store this data and diplay it on screeen in batches of 10 at a time ..
And enable scrolling ..
Right key has to display the next batch of 10 and so on ..
Left key the previous batch of 10.
Down key .. one by one scrolling just as in mobile phone contacts
similarly up key ..

And when user selects any one .. the content has to be played ..

For the above problem .. I thought I would put 10 pushbuttons so that when it is clicked ,I can write some code to play the audio/video file..

Please let me know if there is a better way of doing this ..
Thanks.

Added after 1 48 minutes:

Hi wyotsa,
Any inputs on the design ?
Thanks.

d_stranz
5th April 2013, 23:39
Create an event filter and install it on the 10th pushbutton. In the event filter, examine the event to see if it is a "focusIn" event. If so, then emit a signal to change the text of your buttons.

It will work something like this:



// FocusFilter.h

class FocusFilter : public QObject
{
Q_OBJECT

signals:
void focusInReceived();

protected:
bool eventFilter( QObject * pObj, QEvent * pEv );
};

// FocusFilter.cpp

bool FocusFilter::eventFilter( QObject * pEv, QEvent * pEv )
{
if ( pEv->type() == QEvent::FocusIn )
emit focusInReceived();
return QObject::eventFilter( pObj, pEv );
}

// YourGUICode.cpp

FocusFilter * pFF = new FocusFilter( this );
connect( pFF, SIGNAL( focusInReceived() ), this, SLOT( refresh() ) );

pushbutton[ 9 ] = new QPushButton( this );
pushbutton[ 9 ]->installEventFilter( pFF );


Not compiled or tested, but this should do what you want.

wysota
5th April 2013, 23:55
Hi wyotsa,
Any inputs on the design ?
Thanks.

I would definitely not use any buttons. I think I would just use a list view with reimplemented key events to make page stepping work. Everything else should work out of the box and be much more natural in design.