Results 1 to 10 of 10

Thread: QPushButton with a custom QLabel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPushButton with a custom QLabel

    @kavinsiva

    I cant press the button.. Thats what the problem is.. My code does what your code does.. But, dont know why but, I cant press the pushButton

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPushButton with a custom QLabel

    Then rich text isn't possible with QPushButton.
    But... you can set the color and style of the complete text in a pushbutton via a stylesheet.

  3. #3
    Join Date
    Sep 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPushButton with a custom QLabel

    Ok, I researched a bit and got the answer.. The code for what I wanted is:-

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QLabel>
    3. #include <QPushButton>
    4. #include <QtGui>
    5. #include <QTextDocument>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. QPushButton *button = new QPushButton(Main);
    12. Text.setHtml("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");
    13.  
    14. QPixmap pixmap(Text.size().width(), Text.size().height());
    15. pixmap.fill( Qt::transparent );
    16. QPainter painter( &pixmap );
    17. Text.drawContents(&painter, pixmap.rect());
    18.  
    19. QIcon ButtonIcon(pixmap);
    20. button->setIcon(ButtonIcon);
    21. button->setIconSize(pixmap.rect().size());
    22. QObject::connect(button, SIGNAL(clicked()),Main, SLOT(close()));
    23. Main->show();
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2013
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPushButton with a custom QLabel

    Setting the label as a child of the button seems like a good way to do it . The button still works as expected and has its appearance defined by the label.
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLabel>
    4.  
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. QPushButton *button = new QPushButton(NULL);
    12. QLabel *label = new QLabel("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>",button);
    13. QObject::connect(button, SIGNAL(clicked()), &a, SLOT(quit()));
    14. button->show();
    15. button->setWindowTitle("HelloWorld Test Program");
    16.  
    17. label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
    18. label->show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 29th September 2009, 19:44
  2. Replies: 2
    Last Post: 30th July 2009, 04:58
  3. Need Graphical Text on QPushButton/QLabel
    By JimDaniel in forum Qt Programming
    Replies: 6
    Last Post: 9th June 2008, 08:12
  4. QPushbutton + QLabel text color
    By Ashish in forum Qt Programming
    Replies: 5
    Last Post: 3rd February 2007, 08:13
  5. QScrollArea display custom QLabel
    By spawnwj in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2006, 03:38

Tags for this Thread

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.