View Full Version : 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.
The screenshot:
http://img368.imageshack.us/img368/8984/focusjp7.jpg
Please help me. Thanks.
aamer4yu
5th April 2008, 14:20
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 :D
Hope am getting you right :)
I want to customize this button as following:
http://img171.imageshack.us/img171/2131/focus2yb8.jpg
When it has focus, it is drawn with Solid-Line rectangle.
Thanks.
aamer4yu
5th April 2008, 15:48
You can override QWidget::paintEvent
Keep 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,
MyButton::paintEvent()
{
QPushbutton::paintEvent();
if(m_isButtonFocussed)
draw solid rectangle
}
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 :)
I believe it belongs widget's style.
Please compile this example:
#include <QApplication>
#include <QToolButton>
#include <QMotifStyle>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget mainWin;
QHBoxLayout *mainLayout = new QHBoxLayout;
mainWin->setLayout(mainLayout);
QToolButton tb;
tb->setText("Testing");
tb->setStyle(new QMotifStyle); // it is important
mainLayout->addWidget(tb);
mainWin.show();
return app.exec();
}
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.
Please tell me how to create any shapes (or fill with a gradient) when it has focus.
Use style sheets and ":focus" pseudo-state.
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.
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?
void MyPushButton::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QStylePainter painter(this);
QStyleOptionButton option;
initStyleOption(&option);
option.state &= ~QStyle::State_HasFocus; // <--
painter.drawControl(QStyle::CE_PushButton, option);
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.