PDA

View Full Version : QPushButton with a custom QLabel



shadyabhi
27th May 2010, 12:02
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..


#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton *button = new QPushButton(NULL);
QLabel *label = new QLabel("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>",button,Qt::Window);
QObject::connect(button, SIGNAL(clicked()),
&a, SLOT(quit()));

label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
label->setWindowTitle("HelloWorld Test Program");
label->show();
return a.exec();
}

kavinsiva
27th May 2010, 12:20
HI,
use clear() instead of quit().If the slot is quit your application will terminate.

shadyabhi
27th May 2010, 12:24
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...

kavinsiva
27th May 2010, 12:41
Hi,

use Following program


#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>
#include<QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *Main=new QMainWindow;

QPushButton *button = new QPushButton(Main);
QLabel *label = new QLabel(Main);
label->setText("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");
label->setVisible(true);
label->setGeometry(30,30,40,80);
QObject::connect(button, SIGNAL(clicked()),label, SLOT(clear()));
label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
label->setWindowTitle("HelloWorld Test Program");
Main->show();
return a.exec();
}

tbscope
27th May 2010, 12:41
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)


QWidget *myContainerWidget = new QWidget;
QGridLayout *myContainingLayout = new QGridLayout;

QLabel *myLabel = new QLabel;
myLabel->setText("my label");

QPushButton *myButton = new QPushButton;
myButton->setText("my button");

myContainingLayout->addWidget(myLabel);
myContainingLayout->addWidget(myButton);

myContainerWidget->setLayout(myContainingLayout);

myContainerWidget->show();

shadyabhi
27th May 2010, 13:15
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)


QWidget *myContainerWidget = new QWidget;
QGridLayout *myContainingLayout = new QGridLayout;

QLabel *myLabel = new QLabel;
myLabel->setText("my label");

QPushButton *myButton = new QPushButton;
myButton->setText("my button");

myContainingLayout->addWidget(myLabel);
myContainingLayout->addWidget(myButton);

myContainerWidget->setLayout(myContainingLayout);

myContainerWidget->show();



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?

shadyabhi
27th May 2010, 13:21
@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

tbscope
27th May 2010, 13:42
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.

shadyabhi
27th May 2010, 15:06
Ok, I researched a bit and got the answer.. The code for what I wanted is:-


#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>
#include <QtGui>
#include <QTextDocument>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *Main=new QMainWindow;

QPushButton *button = new QPushButton(Main);
QTextDocument Text;
Text.setHtml("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");

QPixmap pixmap(Text.size().width(), Text.size().height());
pixmap.fill( Qt::transparent );
QPainter painter( &pixmap );
Text.drawContents(&painter, pixmap.rect());

QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());
QObject::connect(button, SIGNAL(clicked()),Main, SLOT(close()));
Main->show();
return a.exec();
}

markanth
13th August 2015, 03:56
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.



#include <QApplication>
#include <QPushButton>
#include <QLabel>



int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QPushButton *button = new QPushButton(NULL);
QLabel *label = new QLabel("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>",button);
QObject::connect(button, SIGNAL(clicked()), &a, SLOT(quit()));
button->show();
button->setWindowTitle("HelloWorld Test Program");

label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
label->show();

return a.exec();
}