Results 1 to 5 of 5

Thread: QPushbutton to fit it's text

  1. #1
    Join Date
    Jun 2006
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default QPushbutton to fit it's text

    Hi All ,

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

    Can anyone assist?

  2. #2
    Join Date
    Jan 2006
    Location
    India
    Posts
    54
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QPushbutton to fit it's text

    use adjustSize().

  3. #3
    Join Date
    Jun 2006
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: QPushbutton to fit it's text

    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):

    Qt Code:
    1. QPushButton * set,* reset;
    2.  
    3. reset = new QModifiedQPushButton("reset",m_table);
    4. reset->setEnabled(false);
    5. set = new QModifiedQPushButton("set",m_table);
    6. connect(set,SIGNAL(clicked()),reset,SLOT(setEnable()));
    7. m_table->setCellWidget(m_table->numRows()-1,5,set);
    8. m_table->setCellWidget(m_table->numRows()-1,6,reset);
    9.  
    10. set->adjustSize();
    11. reset->adjustSize();
    12. for(int i = 0 ; i< m_table->numCols();i++)
    13. m_table->adjustColumn(i);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2006
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: QPushbutton to fit it's text

    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:
    Qt Code:
    1. Header ->
    2. class QModifiedQPushButton : public QPushButton
    3. {
    4. Q_OBJECT
    5. public:
    6. QModifiedQPushButton(const QString &text ,QWidget * parent = 0, const char * name = 0 );
    7. virtual ~QModifiedQPushButton();
    8. QSize sizeHint() const;
    9. };
    10.  
    11. CPP ->
    12. QModifiedQPushButton::QModifiedQPushButton(const QString &text ,QWidget * parent,const char * name) : QPushButton(text,parent,name)
    13. {
    14.  
    15. }
    16.  
    17. QModifiedQPushButton::~QModifiedQPushButton()
    18. {
    19.  
    20. }
    21.  
    22. QSize QModifiedQPushButton::sizeHint() const
    23. {
    24. return QSize(40,28); // you can return whatever size here
    25. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbutton to fit it's text

    Why not just call setFixedSize?

    Qt Code:
    1. mypushbutton->setFixedSize(40,25);
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. widget for text AND buttons
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 16th June 2006, 13:43
  2. Setting text color on QLabel
    By Yorma in forum Newbie
    Replies: 11
    Last Post: 15th June 2006, 07:25
  3. Finding text on Text edit
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2006, 13:20
  4. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

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.