PDA

View Full Version : QPushbutton to fit it's text



Equilibrium
22nd June 2006, 15:20
Hi All ,

I've been tring to fit a QPushbutton according to it's text , but was unable to do so.

Can anyone assist?

sumsin
22nd June 2006, 15:36
use adjustSize().

Equilibrium
22nd June 2006, 15:46
Give me some credit i've already tried this ;) , but it was not successfull.
Let me explain abit , I'm using this pushbutton as awidget in Qtable.

Here's the code (I've left some parts):



QPushButton * set,* reset;

reset = new QModifiedQPushButton("reset",m_table);
reset->setEnabled(false);
set = new QModifiedQPushButton("set",m_table);
connect(set,SIGNAL(clicked()),reset,SLOT(setEnable ()));
m_table->setCellWidget(m_table->numRows()-1,5,set);
m_table->setCellWidget(m_table->numRows()-1,6,reset);

set->adjustSize();
reset->adjustSize();
for(int i = 0 ; i< m_table->numCols();i++)
m_table->adjustColumn(i);

Equilibrium
22nd June 2006, 16:09
manged to Fix the problem!! :)

Maybe there are better ways but this is how i did it:
1. subclass QPushButton.
2. overwrite the virtual sizeHint() function
3. In this function return the prefered sizeHint (QPushButton sizeHint().width=80)
4. call adjustSize() on your subclassed QPushButton.

Code:


Header ->
class QModifiedQPushButton : public QPushButton
{
Q_OBJECT
public:
QModifiedQPushButton(const QString &text ,QWidget * parent = 0, const char * name = 0 );
virtual ~QModifiedQPushButton();
QSize sizeHint() const;
};

CPP ->
QModifiedQPushButton::QModifiedQPushButton(const QString &text ,QWidget * parent,const char * name) : QPushButton(text,parent,name)
{

}

QModifiedQPushButton::~QModifiedQPushButton()
{

}

QSize QModifiedQPushButton::sizeHint() const
{
return QSize(40,28); // you can return whatever size here
}

wysota
22nd June 2006, 17:15
Why not just call setFixedSize?


mypushbutton->setFixedSize(40,25);

BTW. You don't need the Q_OBJECT macro in the class you have shown here.