Results 1 to 11 of 11

Thread: How to know whether a pushbutton is highlighted?

  1. #1
    Join Date
    Apr 2013
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default How to know whether a pushbutton is highlighted?

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to know whether a pushbutton is highlighted?

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to know whether a pushbutton is highlighted?

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Apr 2013
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to know whether a pushbutton is highlighted?

    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.
    Last edited by AmulyaT; 5th April 2013 at 04:53.

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: How to know whether a pushbutton is highlighted?

    you should use ["code"] and [/"code"] btw, and if it works with out
    Qt Code:
    1. buttons[9]->hasFocus()
    To copy to clipboard, switch view to plain text mode 
    it seems to me like that button isn't getting focus did you try with out that condition? eg
    Qt Code:
    1. if(buttons[9]->hasFocus()){
    2. QMessageBox msg(QMessageBox::Information, "Document", "The document has been modified.");
    3. msg.exec();
    4. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Apr 2013
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to know whether a pushbutton is highlighted?

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to know whether a pushbutton is highlighted?

    Quote Originally Posted by AmulyaT View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to know whether a pushbutton is highlighted?

    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

  9. #9
    Join Date
    Apr 2013
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to know whether a pushbutton is highlighted?

    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.
    Last edited by AmulyaT; 5th April 2013 at 08:43.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to know whether a pushbutton is highlighted?

    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:

    Qt Code:
    1. // FocusFilter.h
    2.  
    3. class FocusFilter : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. signals:
    8. void focusInReceived();
    9.  
    10. protected:
    11. bool eventFilter( QObject * pObj, QEvent * pEv );
    12. };
    13.  
    14. // FocusFilter.cpp
    15.  
    16. bool FocusFilter::eventFilter( QObject * pEv, QEvent * pEv )
    17. {
    18. if ( pEv->type() == QEvent::FocusIn )
    19. emit focusInReceived();
    20. return QObject::eventFilter( pObj, pEv );
    21. }
    22.  
    23. // YourGUICode.cpp
    24.  
    25. FocusFilter * pFF = new FocusFilter( this );
    26. connect( pFF, SIGNAL( focusInReceived() ), this, SLOT( refresh() ) );
    27.  
    28. pushbutton[ 9 ] = new QPushButton( this );
    29. pushbutton[ 9 ]->installEventFilter( pFF );
    To copy to clipboard, switch view to plain text mode 

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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to know whether a pushbutton is highlighted?

    Quote Originally Posted by AmulyaT View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 29th April 2011, 08:54
  2. How to prevent an icon being highlighted?
    By Stobie in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2010, 09:24
  3. Replies: 1
    Last Post: 5th August 2008, 13:53
  4. QomboBox - activated(), highlighted()
    By Salazaar in forum Newbie
    Replies: 10
    Last Post: 14th June 2007, 11:50
  5. Qtreeview selection highlighted problem?
    By thefisher in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2006, 09:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.