Results 1 to 10 of 10

Thread: QPushButton with a custom QLabel

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

    Default QPushButton with a custom QLabel

    I am an absolute newbie. I am trying to test a simple application that has a pushbutton with colourful label and when I press that button, the application exits. But, its not working as expected.

    I am conceptually wrong somewhere.. Pls help..

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QLabel>
    3. #include <QPushButton>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. QPushButton *button = new QPushButton(NULL);
    9. QLabel *label = new QLabel("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>",button,Qt::Window);
    10. QObject::connect(button, SIGNAL(clicked()),
    11. &a, SLOT(quit()));
    12.  
    13. label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
    14. label->setWindowTitle("HelloWorld Test Program");
    15. label->show();
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QPushButton with a custom QLabel

    HI,
    use clear() instead of quit().If the slot is quit your application will terminate.
    Kavin

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

    Default Re: QPushButton with a custom QLabel

    Quote Originally Posted by kavinsiva View Post
    HI,
    use clear() instead of quit().If the slot is quit your application will terminate.
    Thats the behavior I want. But, I am not getting the PushButton which I can press and exit the application. I am just getting a label which I cant press...

  4. #4
    Join Date
    Aug 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QPushButton with a custom QLabel

    Hi,

    use Following program
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QLabel>
    3. #include <QPushButton>
    4. #include<QtGui>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QPushButton *button = new QPushButton(Main);
    11. QLabel *label = new QLabel(Main);
    12. label->setText("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");
    13. label->setVisible(true);
    14. label->setGeometry(30,30,40,80);
    15. QObject::connect(button, SIGNAL(clicked()),label, SLOT(clear()));
    16. label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
    17. label->setWindowTitle("HelloWorld Test Program");
    18. Main->show();
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Kavin

  5. #5
    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

    That's because you set the button as the parent of the label. What you want doesn't work like you tried it.

    Here's a better solution (don't copy and past, check it first)
    Qt Code:
    1. QWidget *myContainerWidget = new QWidget;
    2. QGridLayout *myContainingLayout = new QGridLayout;
    3.  
    4. QLabel *myLabel = new QLabel;
    5. myLabel->setText("my label");
    6.  
    7. QPushButton *myButton = new QPushButton;
    8. myButton->setText("my button");
    9.  
    10. myContainingLayout->addWidget(myLabel);
    11. myContainingLayout->addWidget(myButton);
    12.  
    13. myContainerWidget->setLayout(myContainingLayout);
    14.  
    15. myContainerWidget->show();
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QPushButton with a custom QLabel

    Quote Originally Posted by tbscope View Post
    That's because you set the button as the parent of the label. What you want doesn't work like you tried it.

    Here's a better solution (don't copy and past, check it first)
    Qt Code:
    1. QWidget *myContainerWidget = new QWidget;
    2. QGridLayout *myContainingLayout = new QGridLayout;
    3.  
    4. QLabel *myLabel = new QLabel;
    5. myLabel->setText("my label");
    6.  
    7. QPushButton *myButton = new QPushButton;
    8. myButton->setText("my button");
    9.  
    10. myContainingLayout->addWidget(myLabel);
    11. myContainingLayout->addWidget(myButton);
    12.  
    13. myContainerWidget->setLayout(myContainingLayout);
    14.  
    15. myContainerWidget->show();
    To copy to clipboard, switch view to plain text mode 

    I am sorry, I think I was not clear. I want a windows that has a pushButton with color text displayed on it. If the press that button, my app exits.
    I cant use the constructor to set text because then I cant use colorful text. Hope I am clear now. Now, what should I do?

  7. #7
    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

  8. #8
    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.

  9. #9
    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 

  10. #10
    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.