简体   繁体   中英

Cannot press QPushButton in a simple program

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();
}

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();
}

Take a look here . Widget called QwwRichTextButton .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM