PDA

View Full Version : Customize on focus widget



vql
5th April 2008, 03:24
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.

vql
5th April 2008, 14:16
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 :)

vql
5th April 2008, 14:29
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 :)

vql
7th April 2008, 04:53
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.

jpn
7th April 2008, 07:37
Please tell me how to create any shapes (or fill with a gradient) when it has focus.
Use style sheets and ":focus" pseudo-state.

vql
7th April 2008, 14:14
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.

vql
9th April 2008, 16:15
Anyone help me???

jpn
9th April 2008, 18:58
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);
}