tags:

views:

179

answers:

2

Basically, I want a simple pushButton with a colorful text which when pressed exits the application.

Why cant I press PushButton in this simple program. I am using QT 4.6 on Arch x86_64.

#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);
    QObject::connect(button, SIGNAL(clicked()),label, SLOT(close()));
    label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
    label->setWindowTitle("HelloWorld Test Program");
    Main->show();
    return a.exec();
}
A: 

Take a look here. Widget called QwwRichTextButton.

The QwwRichTextButton widget provides a button that can display rich text.

mosg
But, isnt there a way to do it with standard QT without using any third-party libraries.
shadyabhi
@shadyabhi You may go another way: from *wwWidgets* package find *QwwRichTextButton* implementation code and use it in your project.
mosg
+2  A: 

Beside the use of a button class that will allow you to display rich text, you also need to make sure your connections are correct.

In your example, you're connecting the clicked signal of the button to the clear() slot of the label, which is non-sense.

To exit your app when the button is clicked, you need to close the main window. Here is the code to get the right connection :

QObject::connect(button, SIGNAL(clicked()),Main, SLOT(close()));

Changing this single line of code in your example is not enough, because your label is drawn on top of your button, so it's not possible to graphically click on it. You need to hide your label and put some text into your button :

button->setText("Hello");
label->setVisible(false);

Regarding the rich text feature in a QPushButton, AFAIK it is not possible to do it with a QPushButton.

UPDATE : Here is a way to put some richtext on a QPushButton. It uses the solution described by my comment : painting a QTextDocument onto a pixmap and setting this pixmap as the button's icon.

#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();
}
Jérôme
That was a silly mistake of close().. But, still this way I will not be able to display colorful text on button.. So, whats the way of doing it?
shadyabhi
You could use QwwRichTextButton (see mosg answer). I've never used it.Or you could maybe use a QTextDocument and draw it into a QPixmap, then put this pixmap onto your button. instead of text.
Jérôme
@shadyabhi : see the update of my answer, I changed your code to produce what you want.
Jérôme
Thanks Jerome for your answer :) . I got exactly what I wanted..
shadyabhi