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..
Code:
#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>
int main(int argc, char *argv[])
{
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();
}
Re: QPushButton with a custom QLabel
HI,
use clear() instead of quit().If the slot is quit your application will terminate.
Re: QPushButton with a custom QLabel
Quote:
Originally Posted by
kavinsiva
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...
Re: QPushButton with a custom QLabel
Hi,
use Following program
Code:
#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>
#include<QtGui>
int main(int argc, char *argv[])
{
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();
}
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)
Code:
myLabel->setText("my label");
myButton->setText("my button");
myContainingLayout->addWidget(myLabel);
myContainingLayout->addWidget(myButton);
myContainerWidget->setLayout(myContainingLayout);
myContainerWidget->show();
Re: QPushButton with a custom QLabel
Quote:
Originally Posted by
tbscope
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)
Code:
myLabel->setText("my label");
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?
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
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.
Re: QPushButton with a custom QLabel
Ok, I researched a bit and got the answer.. The code for what I wanted is:-
Code:
#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>
#include <QtGui>
#include <QTextDocument>
int main(int argc, char *argv[])
{
Text.setHtml("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");
QPixmap pixmap
(Text.
size().
width(), Text.
size().
height());
pixmap.fill( Qt::transparent );
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();
}
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.
Code:
#include <QApplication>
#include <QPushButton>
#include <QLabel>
int main(int argc, char *argv[])
{
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();
}