Results 1 to 5 of 5

Thread: QPushButton elide text

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default QPushButton elide text

    I have a QPuhButton of fixed width. I want that whenever the text inside reaches the end with more text to follow, it should automatically add ellipsis, the way we can do in HTML content in web pages. I tried this:

    Qt Code:
    1. tableWidget = new QTableWidget(1,2,this);
    2. tableWidget->setGeometry(15,169,251,232);
    3. tableWidget->setColumnWidth(0,198);
    4. tableWidget->setColumnWidth(1,54);
    5.  
    6. QPushButton *textSnippet = new QPushButton();
    7. textSnippet->setStyleSheet("text-align: left; border: none; padding-left: 10px;");
    8. textSnippet->setCursor(Qt::PointingHandCursor);
    9. QString qText = "Some text";
    10. QFontMetrics metrics(textSnippet->font());
    11. QString elidedText = metrics.elidedText(qText, Qt::ElideRight, textSnippet->width()-5);
    12. textSnippet->setText(elidedText);
    13.  
    14. tableWidget->setCellWidget(0,1, textSnippet);
    To copy to clipboard, switch view to plain text mode 

    This does not work at all, the text continues until the very end and there is no ellipsis. Of course, I tried checking for a fixed number of characters (25), so that if the length of the text is beyond that, slice the first 25 characters and append an ellipsis and show it, but this results in a very non-uniform set of rows with ellipsis appearing at various positions in different rows.

    How do I do it?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton elide text

    Are you passing an appropriate width value?
    I.e. if you call metrics.width(qText), how many pixels does it need vs. what you are giving to elided text as available width?

    Cheers,
    _

  3. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: QPushButton elide text

    Do you mean to say that I should pass the width of the button (198 px here) explicitly instead of textSnippet->width()-5?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPushButton elide text

    Since your textSnippet widget has not yet been displayed it is entirely possible it has no meaningful width of its own.

  5. #5
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: QPushButton elide text

    Hmm, makes sense. Will try to pass the width explicitly and let you guys know..


    Added after 42 minutes:


    Right. It totally works when I call the method after setting the width for the button. Pretty dumb of me to ask.
    Last edited by Cupidvogel; 24th May 2015 at 00:15.

Similar Threads

  1. Elide text
    By brcontainer in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2013, 20:40
  2. QPushButton text in mac os
    By tintu in forum Qwt
    Replies: 0
    Last Post: 29th November 2011, 09:09
  3. How to set Text Elide Mode for QComboBox
    By kalos80 in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 08:42
  4. QListView Text Elide Like QListWidget
    By Kel Solaar in forum Qt Programming
    Replies: 6
    Last Post: 13th May 2010, 05:44
  5. QPushbutton to fit it's text
    By Equilibrium in forum Qt Programming
    Replies: 4
    Last Post: 22nd June 2006, 16:15

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.