PDA

View Full Version : shape of push button



Seema Rao
18th April 2006, 10:12
How to set the shape of push button /tool button. ? say to oval or round shape

Thanks in advance

wysota
18th April 2006, 14:56
Subclass and reimplement the paint event.

Brandybuck
18th April 2006, 20:44
Another way is to write a new QStyle that styles only pushbuttons, and set set that style for the pushbuttons in question.

georgie
11th May 2006, 02:11
this is the way that I chose to subclass my buttons to get them to look like little green round "electrodes" in an electrode array


void ElectrodeButton::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);

QRadialGradient grad(15, 15, 15, 0, 0);

grad.setColorAt(0.0, Qt::white);
grad.setColorAt(0.1, Qt::green);
grad.setColorAt(0.7, Qt::black);
QBrush brush(grad);
painter.setBrush(brush);
painter.drawEllipse(2,2,20,20);
}

ElectrodeButton is a subclass of QPushButton

MarkoSan
21st January 2008, 06:58
Is it possible to adapt button shape dynamically according to loaded icon? What I want to achieve is when button icon is loaded, it's size and shape is reset to size and shape of overlaping icon. How do I do it?

wysota
21st January 2008, 12:17
It might be simplest to reimplement the paintEvent from the button. An alternative is to create a custom widget and provide an icon setting method that will also change the mask of the widget.

MarkoSan
21st January 2008, 12:34
Ok, but how do I extract icon shape? For size, I can use QIcon::actualSize?

wysota
21st January 2008, 13:22
QPixmap::createHeuristicMask().

MarkoSan
21st January 2008, 19:04
Can you provide me with simple example, please?

wysota
21st January 2008, 22:25
An example of what? Calling createHeuristicMask?

MarkoSan
21st January 2008, 22:35
I did not get to step where I will use Calling createHeuristicMask yet. Right now, I have a big pixmap on disk (512x512) an when I set it in QPushButton it is so tiny it can be barely seen. I would like to resize it. How do I do that? I trided rescled, does not work. I tried to put some huge text near this pixmap in button, text is shown ok, pixmap is still tiny.

Shit, I've mixed up threads. I've posted new thread regarding how to resize icon in subclassed qpushbutton. Can you move it?

MarkoSan
21st January 2008, 22:39
An example of what? Calling createHeuristicMask?

Yes, how is heuristic mask used, becuase I have no idea how to adept push button shape to loaded pixmap.

wysota
21st January 2008, 22:49
It returns a mask which you should then pass to QWidget::setMask.

MarkoSan
21st January 2008, 22:59
Well, this is what I've done:
CFlagButton::CFlagButton(QWidget* pParent, const QString flagPicture)
{
// sets up button font
m_ButtonFont.setFamily("1942 Report"); // sets font name
m_ButtonFont.setPointSize(36); // sets font size
m_ButtonFont.setBold(true); // sets bold on
this->setFont(m_ButtonFont); // sets font
// sets button palette (background and text color)
this->setAutoFillBackground(true); // sets auto fill background feature
m_ButtonPalette=this->palette(); // reads current palette
m_ButtonPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
this->setPalette(m_ButtonPalette); // sets modfied palette
this->setFocusPolicy(Qt::NoFocus); // disables focus
m_ButtonIcon.load(flagPicture); // creates new icon based on icon
/*
m_ButtonIcon.scaled(256, 256,
Qt::KeepAspectRatioByExpanding,
Qt::SmoothTransformation); // tries to resize icon
*/
setIcon(m_ButtonIcon); // sets icon
setMask(m_ButtonIcon.createHeuristicMask()); // sets mask
}

And the buttons are not drawn at all, neither are icons in buttons.

wysota
22nd January 2008, 01:00
Try reimplementing the paint event and draw the pixmap yourself if you can't handle this approach.

MarkoSan
22nd January 2008, 01:10
Hmm, there is no need for insulting me that I am incompetent, but I am doing this thing for the first time and I am really lost.

MarkoSan
22nd January 2008, 01:39
Whoa!!!

Now it workd perfectly!!!

Here is working code:
CFlagButton::CFlagButton(QWidget* pParent, const QString flagPicture)
{
// sets up button font
m_ButtonFont.setFamily("1942 Report"); // sets font name
m_ButtonFont.setPointSize(36); // sets font size
m_ButtonFont.setBold(true); // sets bold on
this->setFont(m_ButtonFont); // sets font
// sets button palette (background and text color)
this->setAutoFillBackground(true); // sets auto fill background feature
m_ButtonPalette=this->palette(); // reads current palette
m_ButtonPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
this->setPalette(m_ButtonPalette); // sets modfied palette
this->setFocusPolicy(Qt::NoFocus); // disables focus
m_ButtonIcon.load(flagPicture); // creates new icon based on icon
setIcon(m_ButtonIcon); // sets icon
setIconSize(QSize(256, 256)); // sets icon size
setMask(m_ButtonIcon.createHeuristicMask()); // sets mask
}

wysota
22nd January 2008, 06:54
Hmm, there is no need for insulting me that I am incompetent, but I am doing this thing for the first time and I am really lost.

I didn't say anything like that. I just said that if you can't make something work, try a different approach.

ashukla
6th February 2008, 09:39
Dear MarkoSan!

Whoa!!!

Now it workd perfectly!!!

Here is working code:
CFlagButton::CFlagButton(QWidget* pParent, const QString flagPicture)
{
// sets up button font
m_ButtonFont.setFamily("1942 Report"); // sets font name
m_ButtonFont.setPointSize(36); // sets font size
m_ButtonFont.setBold(true); // sets bold on
this->setFont(m_ButtonFont); // sets font
// sets button palette (background and text color)
this->setAutoFillBackground(true); // sets auto fill background feature
m_ButtonPalette=this->palette(); // reads current palette
m_ButtonPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
this->setPalette(m_ButtonPalette); // sets modfied palette
this->setFocusPolicy(Qt::NoFocus); // disables focus
m_ButtonIcon.load(flagPicture); // creates new icon based on icon
setIcon(m_ButtonIcon); // sets icon
setIconSize(QSize(256, 256)); // sets icon size
setMask(m_ButtonIcon.createHeuristicMask()); // sets mask
}

Can I know without passing pParent into a QPushButon how a way your CFlagButton display?
When my button is appeared on the screen some of the blackish area appeared up & down corner of the push button?
Can you put your complete code for clear understanding or change in my code which I am attaching?

#include<QtGui>
class CFlagButton:QPushButton
{
public:
CFlagButton(QWidget* parent, const QString flagPicture):QPushButton(parent)
{
QFont m_ButtonFont;
// sets up button font
//m_ButtonFont.setFamily("1942 Report"); // sets font name
m_ButtonFont.setPointSize(36); // sets font size
m_ButtonFont.setBold(true); // sets bold on
this->setFont(m_ButtonFont); // sets font
// sets button palette (background and text color)
this->setAutoFillBackground(true); // sets auto fill background feature
QPalette m_ButtonPalette;
m_ButtonPalette=this->palette();// reads current palette
m_ButtonPalette.setColor(QPalette::Button, Qt::black);//sets up new palette componenet
m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white);//sets up new palette componenet
this->setPalette(m_ButtonPalette);//sets modfied palette
this->setFocusPolicy(Qt::NoFocus);//disables focus
QPixmap m_ButtonIcon;
m_ButtonIcon.load(flagPicture);//creates new icon based on icon
setIcon(m_ButtonIcon);//sets icon
setIconSize(QSize(256, 256));// sets icon size
setMask(m_ButtonIcon.createHeuristicMask());//sets mask

}
};

int main (int argc, char **argv)
{

QApplication app(argc,argv);
QWidget w;
CFlagButton btn(&w,"plane.jpg");
w.show();
return app.exec();
}

MarkoSan
6th February 2008, 10:36
I can show the code to you next thurdsay, until then I cannot ....

ashukla
6th February 2008, 10:46
I can show the code to you next thurdsay, until then I cannot ....
Dear MarkoSan!
Thanks for your response! Ok! I wait for your code.

ashukla
6th February 2008, 10:46
Can You tell me whats wrong in my implementation?

luf
6th February 2008, 14:23
I would use a style sheet. almost same as CSS, and it makes it possible to have several styles...

But if your shape is very irregular, the paint might be an easier approach.

For info on style sheets:
http://foss.in/2007/register/slides/Styling_Qt_Using_Style_Sheets_484.pdf
http://labs.trolltech.com/blogs/category/labs/styles/

and of course the style sheets examples provided in the docs.

Several of the comments on the blogs in the labs have solutions to some styling as well.

MarkoSan
2nd April 2008, 01:05
How do I resize push button?