Results 1 to 10 of 10

Thread: Customize on focus widget

  1. #1
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Customize on focus widget

    Almost window' controls have focus will be drawn with Dashed-Line rectangle. But if a widget have a focus, it will be drawn with Solid-Line rectangle.

    How to customize a widget so that it will be drawn with Dashed-Line rectangle or not draw when it has focus?

    How to limit focus on a widget?

    In a application, how to always focus on specified widget although user press key Tab?

    Thanks.

  2. #2
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize on focus widget

    The screenshot:


    Please help me. Thanks.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize on focus widget

    Why do you want to always keep the application in focus ??

    If thats so, you can derive from QDialog and show your widget as a modal dialog. simple
    Hope am getting you right

  4. #4
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize on focus widget

    I want to customize this button as following:


    When it has focus, it is drawn with Solid-Line rectangle.

    Thanks.

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize on focus widget

    You can override QWidget::paintEventKeep a member variable to keep track of the button is in focus or not. And in the paintEvent, if it is in focus draw a rectangle.

    for example,
    Qt Code:
    1. MyButton::paintEvent()
    2. {
    3. QPushbutton::paintEvent();
    4. if(m_isButtonFocussed)
    5. draw solid rectangle
    6. }
    To copy to clipboard, switch view to plain text mode 

    You can set the value of m_isButtonFocussed from focusInEvent and focusOutEvent

    I dont know if thers direct function to achieve the functionality u want to get. But this is all I can think of now. Hope it helps

  6. The following user says thank you to aamer4yu for this useful post:

    vql (6th April 2008)

  7. #6
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize on focus widget

    I believe it belongs widget's style.

    Please compile this example:

    Qt Code:
    1. #include <QApplication>
    2. #include <QToolButton>
    3. #include <QMotifStyle>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QWidget mainWin;
    9. QHBoxLayout *mainLayout = new QHBoxLayout;
    10. mainWin->setLayout(mainLayout);
    11.  
    12. tb->setText("Testing");
    13. tb->setStyle(new QMotifStyle); // it is important
    14. mainLayout->addWidget(tb);
    15.  
    16. mainWin.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Compile and run it, you will see a small rectangle (cover the text) when it has focus.

    If you replace another style:
    tb->setStyle(new QWindowsXPStyle);
    -> don't draw anything when it has focus.
    tb->setStyle(new QCommonStyle);
    -> draw a big rectangle (cover the control) when it has focus.
    Please tell me how to create any shapes (or fill with a gradient) when it has focus.
    Thanks.

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Customize on focus widget

    Quote Originally Posted by vql View Post
    Please tell me how to create any shapes (or fill with a gradient) when it has focus.
    Use style sheets and ":focus" pseudo-state.
    J-P Nurmi

  9. #8
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize on focus widget

    I tried to implement focusInEvent, focusOutEvent but not successful.
    Even if I implement paintEvent but do not anything in this function, it still draw a rectangle when has focus.

    Please any solution don't use style sheets. Thanks.

  10. #9
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize on focus widget

    Anyone help me???

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Customize on focus widget

    Just be aware that you're breaking the desktop integration. Buttons are supposed to draw a focus rectangle like that on your platform. How does one using keyboard navigation (tab/shift+tab) know which button is focused when no focus is drawn?
    Qt Code:
    1. void MyPushButton::paintEvent(QPaintEvent* event)
    2. {
    3. Q_UNUSED(event);
    4. QStylePainter painter(this);
    5. initStyleOption(&option);
    6. option.state &= ~QStyle::State_HasFocus; // <--
    7. painter.drawControl(QStyle::CE_PushButton, option);
    8. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  12. The following user says thank you to jpn for this useful post:

    vql (10th April 2008)

Similar Threads

  1. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 11:35
  2. Managing widget focus behaviour
    By mnemonic_fx in forum Qt Programming
    Replies: 3
    Last Post: 7th February 2008, 11:27
  3. Force focus to a QTabWidget page's widget
    By thomaspu in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 07:54
  4. How to get focus event in child widget
    By YuriyRusinov in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2006, 08:53
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 15:16

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.